Hello Friends,
I've started working on a game, and I'm a newbie programmer, that means I'm still learning lots of new things. That's right, I've started working on my first game, but I'm just stuck at the beginning. :S
I want to create splash screen in full screen. I've found some written codes about how to create image opacity on the net and modified them as my needs.
public static void SetImgOpacity(Image imgPic, float imgOpac, PictureBox PictureBox_Hd)
{
Bitmap bmpPic = new Bitmap(imgPic.Width, imgPic.Height);
Graphics gfxPic = Graphics.FromImage(bmpPic);
ColorMatrix cmxPic = new ColorMatrix();
cmxPic.Matrix33 = imgOpac;
ImageAttributes iaPic = new ImageAttributes();
iaPic.SetColorMatrix(cmxPic, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
gfxPic.DrawImage(imgPic, new Rectangle(0, 0, bmpPic.Width, bmpPic.Height), 0, 0, imgPic.Width, imgPic.Height, GraphicsUnit.Pixel, iaPic);
gfxPic.Dispose();
PictureBox_Hd.Image = bmpPic;
bmpPic.Dispose(); ---> This code causes the problem...
}
I want to explain a lil' in details about my problem. I set the opacity of the image with a timer, then load the new image into the specified pixturebox. But After I load the new image (bmpPic), i can't dispose it.
without bmpPic.Dispose() code, everything works fine, but when i add that, it always give me an error. But as you know Dispose() is a must here, otherwise, memory would be full of junks. :)
Error in Details:
//
System.ArgumentException: Parameter is not valid.
at System.Drawing.Image.get_RawFormat()
at System.Drawing.Graphics.IgnoreMetafileErrors(Image image, Int32& errorStatus)
at System.Drawing.Graphics.DrawImage(Image image, Int32 x, Int32 y, Int32 width, Int32 height)
at System.Drawing.Graphics.DrawImage(Image image, Rectangle rect)
at System.Windows.Forms.PictureBox.OnPaint(PaintEventArgs pe)
at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs)
at System.Windows.Forms.Control.WmPaint(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
//
In addition to this; Dispose method works fine in debug mode.
I don't know what causes this problem. I hope you help me. Thanks in advance.
Loading
Sam HobbsPosted Feb 20, 2010, 7:06 PM
Windows uses a type of garbage colletion also, in the sense that it allocates pages for applications and keeps them allocated until either the application finishes or more memory is needed. Since physical memory is so cheap now, your system likely has ample memory. Windows won't bother to free pages that have been allocated if there is no need to do so.
So I am not sure what is happening for you application, but it is likely that you can trust Windows and .Net to take care of things for you. Any effort to fix it is more likely to make it worse, unless you spend a month or more learning about how Windows and .Net use memory. If you do study the subject, you are likely to learn that you cannot do it better.
FarukPosted Feb 20, 2010, 2:16 AM
But When I make the project run my code successfully in debug mode, everything is okey.
As you say, C# has garbage collection to manage the memory. But I'm stuck... I don't understand why garbage collection doesn't do its work. :)
Sam HobbsPosted Feb 19, 2010, 6:51 PM
What says that you must do the dispose yourself explicitly?