Results 1 to 7 of 7

Thread: Image not releasing its memory... I think.

  1. #1

    Thread Starter
    Addicted Member Rockhopper's Avatar
    Join Date
    Aug 2003
    Location
    Cape Town, South Africa
    Posts
    199

    Image not releasing its memory... I think.

    I have a program that creates a bitmap, and saves it as a .jpg file.

    All works great, except if in the program i want to delete this newly created file without first restarting the application. An exception is thrown saying that the file is in use, even if i have disposed the image from the picturebox on the screen and within the code that created the image!

    Here is an example of the code:

    Private sub CreateImage()

    Dim i1 As System.Drawing.Image

    i1 = System.Drawing.Image.FromFile("FileName")

    Dim w As Integer = 88
    Dim h As Integer = 72
    Dim b1 As New System.Drawing.Bitmap(w, h, PixelFormat.Format24bppRgb)

    Dim g1 As Graphics = Nothing

    g1 = Graphics.FromImage(b1)

    g1.DrawImage(i1, New Rectangle(0, 0, 88, 72), New Rectangle(0, 0, i1.Width, i1.Height), GraphicsUnit.Pixel)

    g1.InterpolationMode = Drawing.Drawing2D.InterpolationMode.High

    g1.SmoothingMode = Drawing.Drawing2D.SmoothingMode.AntiAlias

    b1.Save("NewImage.jpg", Imaging.ImageFormat.Jpeg)

    b1.Dispose()

    g1.dispose()

    End sub


    Private sub DeleteImage()

    File.Delete("NewImage.jpg")
    End Sub

    It looks as if the image is not being released from memory, even though i dispose it.

    I have also tried b1 = Nothing and g1 = Nothing and i1 = Nothing

    Any help is much appreciated.
    If the facts don't fit the theory, change the facts. --Albert Einstein

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    First off , try to fire the GC .
    VB Code:
    1. GC.Collect()

  3. #3

    Thread Starter
    Addicted Member Rockhopper's Avatar
    Join Date
    Aug 2003
    Location
    Cape Town, South Africa
    Posts
    199
    Firstly, thanx for your time.

    Ok, i just tried the garbage collector, i still get the problem "File already in use".

    any other ideas?
    If the facts don't fit the theory, change the facts. --Albert Einstein

  4. #4
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    This still holds reference (as I can see) ,Why don't you dispose it ?
    VB Code:
    1. i1

  5. #5

    Thread Starter
    Addicted Member Rockhopper's Avatar
    Join Date
    Aug 2003
    Location
    Cape Town, South Africa
    Posts
    199
    This is the current code, it still gives the error

    Dim rnd As New System.Random
    PIRSmall = rnd.Next(100000)
    '1.2
    Dim i1 As System.Drawing.Image
    i1 = System.Drawing.Image.FromFile(ofdSelectPic2.FileName)


    '1.3 ''''''''The w and h here are used for all the b definitions
    Dim w As Integer = 88
    Dim h As Integer = 72
    Dim b1 As New System.Drawing.Bitmap(w, h, PixelFormat.Format24bppRgb)

    '1.4
    Dim g1 As Graphics = Nothing
    g1 = Graphics.FromImage(b1)
    g1.DrawImage(i1, New Rectangle(0, 0, 88, 72), New Rectangle(0, 0, i1.Width, i1.Height), GraphicsUnit.Pixel)
    g1.InterpolationMode = Drawing.Drawing2D.InterpolationMode.High
    g1.SmoothingMode = Drawing.Drawing2D.SmoothingMode.AntiAlias
    '1.5

    '1.6
    'Now save the new file
    b1.Save(Application.StartupPath & "\menu\S" & PIRSmall & ".jpg", Imaging.ImageFormat.Jpeg)

    Try
    b1.Dispose()
    b1 = Nothing
    Catch ex As Exception
    End Try
    Try
    g1.Dispose()
    g1 = Nothing
    Catch ex As Exception
    End Try
    Try
    i1.Dispose()
    i1 = Nothing
    Catch ex As Exception
    End Try
    GC.Collect()
    If the facts don't fit the theory, change the facts. --Albert Einstein

  6. #6

    Thread Starter
    Addicted Member Rockhopper's Avatar
    Join Date
    Aug 2003
    Location
    Cape Town, South Africa
    Posts
    199
    The code i am using is the only code i have been able to find to save a jpeg to hard disk. Does anyone maybe know of other code that does not leave the image open in memory?
    If the facts don't fit the theory, change the facts. --Albert Einstein

  7. #7
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    A quick demo . It seems everything is ok . Revise your code .
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width