|
This is a standard way to obtain information from dialogs (modal forms):
<br />
<br />
void ButtonClick(object oj, EventArgs ea) {<br />
<br />
val = "";<br />
<br />
if (form2.ShowDialog() == DialogResult.OK) {<br />
<br />
val = form2.Val;
}<br />
}<br />
<br />
Be careful to call Form.ShowDialog() ! If you call Form.Show(), it will be unmodal and your code passes before user types anything.
|
|
|
|
|
How do I instantiate a Color in C# using a red-green-blue value? In Java, I am used to:
Color c = new Color(255,128,0);
What is the equivalent in C#?
Thanks!
Mark
|
|
|
|
|
|
In other words,
Color c = Color.FromARGB(255, 255, 255);
|
|
|
|
|
Hi,
I'm trying to make a panel scrollable when it's smaller than a bitmap that's drawn on it, but the scrollbars don't show up... When I place a control, such as a button, out of the panel boundaries and set the AutoScroll property of the panel to true, I do see scroll bars, but not when the bitmap is larger than the panel. Hope I made it clear. Any ideas?
Yigal
|
|
|
|
|
whenever you want to update scrollbars, call:
myPanel.OnResize(EventArgs.Empty);
|
|
|
|
|
Thanks, but I don't think that would help. The panel simply ignores the bitmap when calculating the scroll margins.
Yigal
|
|
|
|
|
I have app working with MDIs - there are also panels with bitmaps and AutoScroll on, but it works fine.
My panel contains PictureBox and this PictureBox has fixed size (same as bitmap) and location (0, 0).
Maybe you just setting Panel.BackgroundImage property (??). This will not work. Panel 's scroll bars depends on size of controls it contains.
|
|
|
|
|
I've created some control that browses directories using TreeView on the left side and shows image thumbnails on the right side.
This control contains thumbnail management. It works this way:
Create hash string from image file address: C:\hello.png -> e8f838d8283kn4.jpg
Does e8f838d8283kn4.jpg exist?
Yes: load this file
No: 1) shrink source image and save as e8f838d8283kn4.jpg
2) dispose source image
But when I browse gallery of 100 3.0 mpx images, my app creates about half of them and then system tells me, there is not enough virtual memory. Next message comes from mscorlib.dll (Out of memory exception) and program falls down.
But thumbnails are small enough. If there are 100 128x128 thumbs, it takes up to:
128x128x3x100 = +- 4.6 MB
Every 3 mpx source image is disposed (using Image.Dispose() call) as well as thumbnails, when directory changes. So why is there lack of memory (my PC has about 240 MB RAM, this may not be a problem).
GC must clear all resources since system needs memory occupied by disposed images, doesn't?
|
|
|
|
|
I'd have to question how your making the thumbnail images and making absolutely sure of what your doing. It sounds like your not aactually creating the thumbnail, but making a copy of the original image and just showing it scaled down in a picturebox.
Without seeing the relevant code, it's impossible to tell you what's going on.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
No, I'm not so silly.
This is the pseudo-code (loading and saving thumb) of my Thumbnail class:
<br />
Image source = Image.FromFile(sourcePath);<br />
Image img = new Bitmap(128, 128);<br />
<br />
Graphics grfx = Graphics.FromImage(img);<br />
grfx.DrawImage(img, ...)<br />
<br />
grfx.Dispose();<br />
source.Dispose();<br />
<br />
this.image = img;<br />
The thumbnail creation process runs on background and Thumbnail instance draws its image in overriden OnPaint method (not in PictureBox).
|
|
|
|
|
oh, sorry - this is on the one line:
<br />
grfx.DrawImage(source, new Rectangle(..., 128, 128), new Rectangle(... source.Size), GraphicsUnit.Pixel);<br />
I draw the large image onto small image. Then I dispose Graphics object and source image.
Thumbnail control disposes the small 128x128 image in the Dispose() method.
|
|
|
|
|
Have you tried calling (in order)
GC.Collect(); // collect unused memory
GC.WaitForPendingFinalizers(); // wait for any pending objects
GC.Collect(); // free any pending objects
Often it's not recommended, but sometimes the GC needs a little kick in the rear to start cleaning things up. This is especially true in pre-.NET 2.0 frameworks, where the GC has no knowledge of how "heavy" a particular unmanaged resource is.
Tech, life, family, faith: Give me a visit.
I'm currently blogging about: Homosexuality in Christianity
Judah Himango
|
|
|
|
|
It seems to work fine!
Eh - Image object in unmanaged?
|
|
|
|
|
Well, in essence an Image is an 'unmanaged' resource, because the CLR and the GC do not manage the raw resources (in this case, a bunch of bytes representing a file) held by the object.
FYI, in .NET 2.0, you can call GC.AddMemoryPressure(long numberOfBytes) to let the GC know that you're holding on to some big resource like an image.
Tech, life, family, faith: Give me a visit.
I'm currently blogging about: Homosexuality in Christianity
Judah Himango
|
|
|
|
|
Thx again. Important thing to remember.
|
|
|
|
|
You probably want to profile your application while you are at it to see if you have any other memory issues.
How To: Use CLR Profiler[^]
|
|
|
|
|
This is a very fundamental question about the use of enumerations in C#. I want to write out the value of an enumeration like System.Drawing.FontStyle to a file like this:
Font f...<br />
StreamWriter sw...<br />
<br />
:<br />
<br />
sw.WriteLine(f.FontStyle);
But then I want to read it in without having to use an if statement to decode it. This is what I want to avoid doing:
String myFontStyleReadIn...<br />
<br />
if (myFontStyleReadIn == "Bold") f = new Font("Arial",8,FontStyle.Bold)<br />
else<br />
if (myFontStyleReadIn == "Italic") f = new Font("Arial",8,FontStyle.Italic)
...
How do I write out the value of the enumeration, then read in the value and instantiate a new font using it?
Thanks!
Mark
|
|
|
|
|
Don't save enumeration members as strings. Save them as integers:
int index = (int)f.FontStyle;
Whenewer you want a string, use:
string style = ((FontStyle)index).ToString();
|
|
|
|
|
Do you mean something like this?
Array folders = Enum.GetValues( typeof(System.Environment.SpecialFolder) );
System.Console.WriteLine( "Special Folders:" );
System.Console.WriteLine( "----------------" );
int longest = 0;
foreach( System.Environment.SpecialFolder folder in folders )
if( folder.ToString().Length > longest )
longest = folder.ToString().Length;
foreach( System.Environment.SpecialFolder folder in folders )
{
System.Console.Write( folder.ToString() + ": " );
for( int i = 0; i < longest - folder.ToString().Length; i++ )
System.Console.Write( " " );
System.Console.WriteLine( System.Environment.GetFolderPath(folder) );
}
Matt Gerrans
|
|
|
|
|
Ah I knew I had done something more similar that this to exactly what you're asking before. Here it is:
private void LoadOptions()
{
string [] keys = new string[currentOptions.Keys.Count];
currentOptions.Keys.CopyTo( keys, 0 );
foreach( string optionName in keys )
{
System.Type optionType = currentOptions[optionName].GetType();
string iniSetting = GetSetting( optionName );
Array enumValues = Enum.GetValues( optionType );
foreach( object enumValue in enumValues )
{
if( enumValue.ToString() == iniSetting )
{
Utils.Trace(null,"Option {0} changed from {1} to {2}.", optionName,
currentOptions[optionName].ToString(), iniSetting );
currentOptions[optionName] = enumValue;
}
}
}
}
Matt Gerrans
|
|
|
|
|
Thanks for the responses. Itinka understands what I want to do. But how do I convert an integer value back into a FontStyle that I can pass to the Font class constructor?
Mark
|
|
|
|
|
I think I wrote:
(FontStyle)(int)
...just convert it (every enumeration member naturally has an index).
|
|
|
|
|
Hi All,
I have a WinForm that has a static panel on it. Everytime I go to add something to my WinForm the IDE is trying to outsmart me and keeps on deleting that static panel along with all the code for it. Is there a way to stop the IDE from doing this ?
-Pete
|
|
|
|
|
Once upon a time, I was working on custom Windows Forms Designer. It's based on some kind of "roundtrip", which parses your code into DOM tree and then recreates the code back. If you've written something what doesn't smell much good to parser, it may not create right representation in DOM and your code is lost.
This was a problem in VS .NET 2003, I don't know if it's better on new VS Beta, but I solve these problems creating regular panel in designer and then adding my own controls in constructor:
myControl.Parent = panel1;
And you can also learn how to work with IExtenderProvider.
I don't know if I helped you much.
|
|
|
|