Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. PictureBox help!

PictureBox help!

Scheduled Pinned Locked Moved C#
helpquestiongraphics
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    ethanwa
    wrote on last edited by
    #1

    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?

    H 1 Reply Last reply
    0
    • E ethanwa

      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?

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      Because the file is in use. What you need to do is clonse the Bitmap and then dispose it:

      using (Bitmap bmp = new Bitmap(imageLocation))
      {
      pctInternet.Image = (Bitmap)bmp.Clone();
      }

      It's good to use the using block statement for IDisposable implementation because it ensures that objects are disposed even if an exception occurs (it compiles to a try-finally block). This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog]

      N 1 Reply Last reply
      0
      • H Heath Stewart

        Because the file is in use. What you need to do is clonse the Bitmap and then dispose it:

        using (Bitmap bmp = new Bitmap(imageLocation))
        {
        pctInternet.Image = (Bitmap)bmp.Clone();
        }

        It's good to use the using block statement for IDisposable implementation because it ensures that objects are disposed even if an exception occurs (it compiles to a try-finally block). This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog]

        N Offline
        N Offline
        Nick Parker
        wrote on last edited by
        #3

        That doesn't work though. Another work around might be the following:

        string loc = @"C:\\image1.gif";		
        using(Bitmap bmp = new Bitmap(loc))
        {
        	using(Graphics g = Graphics.FromHwnd(this.Handle))
        	{
        		g.DrawImage(bmp, 5, 5);
        	}
        }
        

        - Nick Parker
        My Blog | My Articles

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups