Image Resource - not released

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • WayDownUnder

    #1

    Image Resource - not released

    I am loading a image into a picture box using this line of code

    _objPic.Image = System.Drawing. Image.FromFile( _fileName)

    The picture box is then displayed on a form.
    The file in _fileName is a temperary file.

    After the form is closed down, I go back into the form and try and delete
    the tempary file used in the previous display. the application errors with
    cant delete rescource as it is used by another process.
    On closing the first form I call the dispose method.
    Nothing works until I close the application and start the process again.

    any ideas


    --
    Life in the sun
  • Gary Chang[MSFT]

    #2
    RE: Image Resource - not released

    Hi,

    Thank you posting!

    I am afraid this is a known issue. When you use the FromFile method to load
    a PictureBox control with a picture file at run time, the Visual Studio
    ..NET Integrated Development Environment (IDE) maintains a lock on the file.

    To work around this problem, You need to avoid to use the FromFile method
    to load picture file to a PictureBox control directly. The alternative
    workarounds could be one of the following:

    1. Use the FileStream object to load picture file insteadly:
    // Make sure that you have added the System.IO namespace.
    using System.IO;

    // Specify a valid picture file path on your computer.
    FileStream fs;
    fs = new FileStream("C:\ \WINNT\\Web\\Wa llpaper\\Fly Away.jpg",
    FileMode.Open, FileAccess.Read );
    pictureBox1.Ima ge = System.Drawing. Image.FromStrea m(fs);
    fs.Close();

    2. Use the Bitmap( x, y, pixelformat ) constructor to create a 32bpp
    Bitmap, then Graphics.FromIm age(), then draw your original Image on the new
    one and Dispose() the Graphics. Please refer to the following newsgroup
    thread:

    wse_frm/thread/9f22e459fcace23 4/0341a75ce366272 8?lnk=st&q=Syst em.Drawing.Ima
    ge.FromFile+fil e+lock&rnum=8&h l=en#0341a75ce3 662728

    3. Use the Img.GetTumbnail Image to load image to the PictureBox control,
    please refer to the following newsgroup thread for sample code:

    rm/thread/f3d2227be4476c0 1/f0103a542b89a1c b?lnk=st&q=Syst em.Drawing.Imag e.Fr
    omFile+file+loc k&rnum=4&hl=en# f0103a542b89a1c b

    I hope the above information helps, if you have any questions or concerns,
    please do not hesitate to let me know. I am standing by to help you.

    Thanks!

    Best regards,

    Gary Chang
    Microsoft Online Community Support
    =============== =============== =============== =====
    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.
    =============== =============== =============== =====
    This posting is provided "AS IS" with no warranties, and confers no rights.


    Comment

    • WayDownUnder

      #3
      RE: Image Resource - not released

      Gary,
      Number one works great thanks
      --
      Life in the sun


      ""Gary Chang[MSFT]"" wrote:
      [color=blue]
      > Hi,
      >
      > Thank you posting!
      >
      > I am afraid this is a known issue. When you use the FromFile method to load
      > a PictureBox control with a picture file at run time, the Visual Studio
      > .NET Integrated Development Environment (IDE) maintains a lock on the file.
      >
      > To work around this problem, You need to avoid to use the FromFile method
      > to load picture file to a PictureBox control directly. The alternative
      > workarounds could be one of the following:
      >
      > 1. Use the FileStream object to load picture file insteadly:
      > // Make sure that you have added the System.IO namespace.
      > using System.IO;
      >
      > // Specify a valid picture file path on your computer.
      > FileStream fs;
      > fs = new FileStream("C:\ \WINNT\\Web\\Wa llpaper\\Fly Away.jpg",
      > FileMode.Open, FileAccess.Read );
      > pictureBox1.Ima ge = System.Drawing. Image.FromStrea m(fs);
      > fs.Close();
      >
      > 2. Use the Bitmap( x, y, pixelformat ) constructor to create a 32bpp
      > Bitmap, then Graphics.FromIm age(), then draw your original Image on the new
      > one and Dispose() the Graphics. Please refer to the following newsgroup
      > thread:
      > http://groups.google.com/group/micro...rk.drawing/bro
      > wse_frm/thread/9f22e459fcace23 4/0341a75ce366272 8?lnk=st&q=Syst em.Drawing.Ima
      > ge.FromFile+fil e+lock&rnum=8&h l=en#0341a75ce3 662728
      >
      > 3. Use the Img.GetTumbnail Image to load image to the PictureBox control,
      > please refer to the following newsgroup thread for sample code:
      > http://groups.google.com/group/micro...es.vb/browse_f
      > rm/thread/f3d2227be4476c0 1/f0103a542b89a1c b?lnk=st&q=Syst em.Drawing.Imag e.Fr
      > omFile+file+loc k&rnum=4&hl=en# f0103a542b89a1c b
      >
      > I hope the above information helps, if you have any questions or concerns,
      > please do not hesitate to let me know. I am standing by to help you.
      >
      > Thanks!
      >
      > Best regards,
      >
      > Gary Chang
      > Microsoft Online Community Support
      > =============== =============== =============== =====
      > When responding to posts, please "Reply to Group" via your newsreader so
      > that others may learn and benefit from your issue.
      > =============== =============== =============== =====
      > This posting is provided "AS IS" with no warranties, and confers no rights.
      >
      >
      >[/color]

      Comment

      • Gary Chang[MSFT]

        #4
        RE: Image Resource - not released

        I am glad to know it works. :)

        Have a nice weekend!

        Best regards,

        Gary Chang
        Microsoft Online Community Support
        =============== =============== =============== =====
        When responding to posts, please "Reply to Group" via your newsreader so
        that others may learn and benefit from your issue.
        =============== =============== =============== =====
        This posting is provided "AS IS" with no warranties, and confers no rights.


        Comment

        • Herfried K. Wagner [MVP]

          #5
          Re: Image Resource - not released

          ""Gary Chang[MSFT]"" <v-garych@online.m icrosoft.com> schrieb:[color=blue]
          > 1. Use the FileStream object to load picture file insteadly:
          > // Make sure that you have added the System.IO namespace.
          > using System.IO;
          >
          > // Specify a valid picture file path on your computer.
          > FileStream fs;
          > fs = new FileStream("C:\ \WINNT\\Web\\Wa llpaper\\Fly Away.jpg",
          > FileMode.Open, FileAccess.Read );
          > pictureBox1.Ima ge = System.Drawing. Image.FromStrea m(fs);
          > fs.Close();[/color]

          Note that the stream the image object is based on must not be closed as long
          as the image object is used! Two solutions can be found here:

          <URL:http://dotnet.mvps.org/dotnet/code/graphics/#ImageNoLock>

          --
          M S Herfried K. Wagner
          M V P <URL:http://dotnet.mvps.org/>
          V B <URL:http://classicvb.org/petition/>

          Comment

          • Dennis

            #6
            Re: Image Resource - not released

            Nice Tip...I'll use it.
            --
            Dennis in Houston


            "Herfried K. Wagner [MVP]" wrote:
            [color=blue]
            > ""Gary Chang[MSFT]"" <v-garych@online.m icrosoft.com> schrieb:[color=green]
            > > 1. Use the FileStream object to load picture file insteadly:
            > > // Make sure that you have added the System.IO namespace.
            > > using System.IO;
            > >
            > > // Specify a valid picture file path on your computer.
            > > FileStream fs;
            > > fs = new FileStream("C:\ \WINNT\\Web\\Wa llpaper\\Fly Away.jpg",
            > > FileMode.Open, FileAccess.Read );
            > > pictureBox1.Ima ge = System.Drawing. Image.FromStrea m(fs);
            > > fs.Close();[/color]
            >
            > Note that the stream the image object is based on must not be closed as long
            > as the image object is used! Two solutions can be found here:
            >
            > <URL:http://dotnet.mvps.org/dotnet/code/graphics/#ImageNoLock>
            >
            > --
            > M S Herfried K. Wagner
            > M V P <URL:http://dotnet.mvps.org/>
            > V B <URL:http://classicvb.org/petition/>
            >
            >[/color]

            Comment

            Working...