• 0

[c#] HELP!


Question

Ok, so I have a PictureBox on my form named pctInternet. It has the following code associated with it:

string imageLocation = @"C:\mypicture.jpg";
Bitmap bmp = new Bitmap(imageLocation);
pctInternet.Image = bmp;

Now, I just want my PictureBox to display the picture and CLOSE the actual "mypicture.jpg" file, but for some reason whenever I try to delete the "mypicture.jpg" file while my app is running, it errors and says "File is in use."

How do I fix this? What line of code am I missing?

Link to comment
Share on other sites

6 answers to this question

Recommended Posts

  • 0

This is how I do it :)

?	using (Bitmap bmp = new Bitmap(@"C:\mypicture.jpg"))
 ?	{
 ? ?pctInternet.Image = bmp.Clone();
 ?	}

Dan

EDIT: Though I think you should only use PictureBox's if you actually need them.. Usually I just draw the images in the Paint event. (I still Clone the images thou:)). :)

Link to comment
Share on other sites

  • 0
This is how I do it :)

?	using (Bitmap bmp = new Bitmap(@"C:\mypicture.jpg"))
 ?	{
 ? ?pctInternet.Image = bmp.Clone();
 ?	}

Dan

EDIT:  Though I think you should only use PictureBox's if you actually need them.. Usually I just draw the images in the Paint event.  (I still Clone the images thou:)). :)

584802137[/snapback]

What's the difference? Can you give me some sample code on the Paint event?

Link to comment
Share on other sites

  • 0

In the paint event, you manually draw the image onto the form/control:

void Form_Paint(object sender, PaintEventArgs e)
{
   e.Graphics.DrawImage(bmpMyImage,0,0,32,32);
}

Link to comment
Share on other sites

  • 0

Yeah thats how you do it.. :) The difference is with a picturebox a whole Window has to be created. With a normal form this isnt a huge deal, but as your app's get larger it will start to make a noticable difference :) Also you can save some code by overriding OnPaint instead of hooking up an event handler:

 protected override void OnPaint(PaintEventArgs e)
  {
 	 base.OnPaint (e);
 	 
 	 //make sure the image isnt null for error handling.
 	 if (myPicture != null)
 	 {
    //the two zeros are a point relative to the top of the form you're drawing on.
    e.Graphics.DrawImage(myPicture, 0, 0, myPicture.Width, myPicture.Height);
 	 }
  }

Link to comment
Share on other sites

  • 0

Figured out a great way to do it....

System.IO.FileStream fs = new FileStream(imageLocation, System.IO.FileMode.Open);
pctInternet.Image = Image.FromStream(fs);
fs.Close();

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.