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)
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
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