Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Showing an image in PictureBox
Message
From
24/11/2009 12:25:39
Jerry Tovar
Dana Corporation Dana It
Maumee, Ohio, United States
 
 
To
24/11/2009 01:10:43
General information
Forum:
ASP.NET
Category:
Forms
Environment versions
Environment:
C# 3.0
Miscellaneous
Thread ID:
01436254
Message ID:
01436394
Views:
41
Both of these solutions causes an error in my Windows form. The error is 'parameter is not valid' and points to my programs.cs file.

I have broken my form down to just a PictureBox and a CommandButton object.

The click event of my CommandButton is:
            string myImage = @"C:\mydir\myImage.jpg";
            using (System.Drawing.Bitmap o = new Bitmap(myImage))
            {
                myPictureBox.Image = o;
            }
The above causes an error and the image does not display. My goal is to display the desired image but not have the image locked. What am I missing?

Thanks,

Jerry



>Hi Jerry,
>
>What you need to do is dispose of the Bitmap object after you set the image. That frees up the file resource. There are two ways to do this:
>
>
>using (System.Drawing.Bitmap o = new Bitmap(myImage))
>{
>    myPictureBox.Image = o;
>}
>
>
>or
>
>System.Drawing.Bitmap o = new Bitmap(myImage);
>myPictureBox.Image = o;
>o.Dispose();
>
>
>~~Bonnie
>
>
>
>>I have a VS2008 Winform. I am trying to display an image in a PictureBox at runtime. I use the below code to display the selected image. This works and the image is displayed on the form, however, the PictureBox object keeps the file opened. So if I try to rename or delete the file from Windows explorer, Windows tells me that the files is in use.
>>
>>If I call the below code multiple times and pass in a different image name each time, the PictureBox control keeps a file handle open for each image file until I close my form.
>>
>>How do I release any file handle before displaying the next image in the PictureBox? Or, is there a better way to display an image in a Winform?
>>
>>
>>System.Drawing.Bitmap o = new Bitmap(myImage);
>>myPictureBox.Image = o;
>>
>>
>>Thanks,
>>
>>Jerry
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform