Answered by:
Releasing a JPG from the Image control

Question
-
Hello,
I have to use the Image control in an application I'm writing in VB in VS 2012. I cannot get the control to release the jpg. When I try to move the jpg, I get the error saying the file cannot be move because it is in use by another process.
I've tried to clear the Image box by saying:
picBox.Image = Nothing
picBox.Image.Dispose
picBox = Nothing
How can I get the Image box to release the file?
I use the following code to load the jpg:
Public Sub DisplayPic(strPicName As String, picBox As PictureBox)
Dim intPicBoxHeight As Integer
Dim intPicBoxWidth As Integer
Dim scale_factor_height As Single
Dim scale_factor_width As Single
Dim bm_source As Bitmap
Dim bm_destination As Bitmap
Dim gr_destination As Graphics
bm_source = New Bitmap(strPicName)
intPicBoxHeight = picBox.Size.Height
intPicBoxWidth = picBox.Size.Width
scale_factor_height = intPicBoxHeight / bm_source.Height
scale_factor_width = intPicBoxWidth / bm_source.Width
bm_destination = New Bitmap( _
CInt(bm_source.Width * scale_factor_width), _
CInt(bm_source.Height * scale_factor_height))
gr_destination = Graphics.FromImage(bm_destination)
gr_destination.DrawImage(bm_source, 0, 0, _
bm_destination.Width + 1, _
bm_destination.Height + 1)
picBox.Image = bm_destination
picBox.Visible = True
End Sub
where strPicName is the name of the jpg file.
Thanks
Monday, March 14, 2016 11:04 PM
Answers
-
Hi,
you'll have to Dispose the initial image you get from a file, here its bm_source.
After redrawing it to your bm_destination simply call:
bm_source.Dispose() bm_source = Nothing
Then you can move the file.
Regards,
Thorsten
- Marked as answer by Ralph65 Monday, March 14, 2016 11:50 PM
Monday, March 14, 2016 11:38 PM
All replies
-
When I try to move the jpg, I get the error saying the file cannot be move because it is in use by another process.
I've tried to clear the Image box by saying:
picBox.Image = Nothing
The reference that is preventing the file from being moved is bm_source, not the picture box. You are copying the file from bm_source to bm_destination, so you no longer need bm_source.
Monday, March 14, 2016 11:38 PM -
Hi,
you'll have to Dispose the initial image you get from a file, here its bm_source.
After redrawing it to your bm_destination simply call:
bm_source.Dispose() bm_source = Nothing
Then you can move the file.
Regards,
Thorsten
- Marked as answer by Ralph65 Monday, March 14, 2016 11:50 PM
Monday, March 14, 2016 11:38 PM -
It worked! Thank youMonday, March 14, 2016 11:51 PM