|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Deleting a Photo stored in a PictureboxI have a picturebox on a form containing the photo of a person. As you advance through the records, the photo updates. Rather than storing the images in an inefficient blob field in a table, I have the separate images stored in an image directory with primary key as the filename (3476.jpg). If the image exists, I display the image. All this works fine. I have a buttons that allow for the insertion and deletion of photos. The “add†works fine too. But I am getting errors when I attempt to delete an image – “file is being used by another process.†The only time the image is “used†is when I display it on the form. I am assuming the form/picturebox is locking the image (what else?). I coded the procedure to reset the image to a default missing image photo. That did not work so I set the picturebox to nothing first. That did not work either. I also verified the string being passed to the delete.file method. Here is my code: Private Sub cmdRemovePhoto_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRemovePhoto.Click Dim intReturn As Integer intReturn = MsgBox("Delete this Photo? This cannot be reversed.", MsgBoxStyle.YesNo + MsgBoxStyle.Question + MsgBoxStyle.DefaultButton2, "Warning...") If intReturn = 6 Then picPhoto.Image = Nothing picPhoto.Image = Image.FromFile(pubGlobalSetup & "\imageFolder\missingphoto.jpg") Try File.Delete(pubGlobalSetup & "\imageFolder\" & LTrim(Str(intContactID)) & ".jpg") ' <--Error Here cmdRemovePhoto.Enabled = False Catch ee As Exception MsgBox(ee.Message) End Try End If End Sub I would sure appreciate any ideas. This should be easy. I have no clue what could be locking the file. It has to be the picturebox doesn’t it? But at the time I am deleting, it has been replaced. I checked the file attribues at the time I delete and it is set only to "Archive". Someone in Google had a similar problem and the respondant suggested a file.close method. There is no such method. There is a fileclose(buffer#) method for datafiles. Thanks much! -- Tom Try loading. Making a in memory copy of the image (drawimage). Dispose
original Image used for load. Let me know... -tom TomA ha scritto: Show quoteHide quote > Hi All, > > I have a picturebox on a form containing the photo of a person. As you > advance through the records, the photo updates. Rather than storing the > images in an inefficient blob field in a table, I have the separate images > stored in an image directory with primary key as the filename (3476.jpg). If > the image exists, I display the image. All this works fine. > > I have a buttons that allow for the insertion and deletion of photos. The > "add" works fine too. But I am getting errors when I attempt to delete an > image - "file is being used by another process." The only time the image is > "used" is when I display it on the form. I am assuming the form/picturebox > is locking the image (what else?). I coded the procedure to reset the image > to a default missing image photo. That did not work so I set the picturebox > to nothing first. That did not work either. I also verified the string > being passed to the delete.file method. Here is my code: > > > Private Sub cmdRemovePhoto_Click(ByVal sender As System.Object, ByVal e > As System.EventArgs) Handles cmdRemovePhoto.Click > > Dim intReturn As Integer > > intReturn = MsgBox("Delete this Photo? This cannot be reversed.", > MsgBoxStyle.YesNo + MsgBoxStyle.Question + MsgBoxStyle.DefaultButton2, > "Warning...") > > If intReturn = 6 Then > > picPhoto.Image = Nothing > > picPhoto.Image = Image.FromFile(pubGlobalSetup & > "\imageFolder\missingphoto.jpg") > > Try > > File.Delete(pubGlobalSetup & "\imageFolder\" & > LTrim(Str(intContactID)) & ".jpg") ' <--Error Here > > cmdRemovePhoto.Enabled = False > > Catch ee As Exception > > MsgBox(ee.Message) > > End Try > > End If > > End Sub > > > > I would sure appreciate any ideas. This should be easy. I have no clue > what could be locking the file. It has to be the picturebox doesn't it? But > at the time I am deleting, it has been replaced. I checked the file > attribues at the time I delete and it is set only to "Archive". Someone in > Google had a similar problem and the respondant suggested a file.close > method. There is no such method. There is a fileclose(buffer#) method for > datafiles. > > Thanks much! > -- > Tom Thanks Tom (1),
Your suggestion worked. I had to make several changes in the program. In the event someone else has the same problem and is searching, I loaded the image clone into the picturebox using the following code: Private m_Bitmap As Bitmap ' Form level Dim bm As New Bitmap(ImagefileName) m_Bitmap = New Bitmap(bm.Width, bm.Height) Dim gr As Graphics = Graphics.FromImage(m_Bitmap) gr.DrawImage(bm, 0, 0) bm.Dispose() picPhoto.Image = m_Bitmap The file was no longer locked - the picturebox contained a clone and I disposed of the original file from memory. I was free to delete the file without a file locking challenge. Take care! Tom (2) -- Show quoteHide quoteTom "tommaso.gasta***@uniroma1.it" wrote: > Try loading. Making a in memory copy of the image (drawimage). Dispose > original Image used for load. > Let me know... > > -tom > > TomA ha scritto: > > > Hi All, > > > > I have a picturebox on a form containing the photo of a person. As you > > advance through the records, the photo updates. Rather than storing the > > images in an inefficient blob field in a table, I have the separate images > > stored in an image directory with primary key as the filename (3476.jpg). If > > the image exists, I display the image. All this works fine. > > > > I have a buttons that allow for the insertion and deletion of photos. The > > "add" works fine too. But I am getting errors when I attempt to delete an > > image - "file is being used by another process." The only time the image is > > "used" is when I display it on the form. I am assuming the form/picturebox > > is locking the image (what else?). I coded the procedure to reset the image > > to a default missing image photo. That did not work so I set the picturebox > > to nothing first. That did not work either. I also verified the string > > being passed to the delete.file method. Here is my code: > > > > > > Private Sub cmdRemovePhoto_Click(ByVal sender As System.Object, ByVal e > > As System.EventArgs) Handles cmdRemovePhoto.Click > > > > Dim intReturn As Integer > > > > intReturn = MsgBox("Delete this Photo? This cannot be reversed.", > > MsgBoxStyle.YesNo + MsgBoxStyle.Question + MsgBoxStyle.DefaultButton2, > > "Warning...") > > > > If intReturn = 6 Then > > > > picPhoto.Image = Nothing > > > > picPhoto.Image = Image.FromFile(pubGlobalSetup & > > "\imageFolder\missingphoto.jpg") > > > > Try > > > > File.Delete(pubGlobalSetup & "\imageFolder\" & > > LTrim(Str(intContactID)) & ".jpg") ' <--Error Here > > > > cmdRemovePhoto.Enabled = False > > > > Catch ee As Exception > > > > MsgBox(ee.Message) > > > > End Try > > > > End If > > > > End Sub > > > > > > > > I would sure appreciate any ideas. This should be easy. I have no clue > > what could be locking the file. It has to be the picturebox doesn't it? But > > at the time I am deleting, it has been replaced. I checked the file > > attribues at the time I delete and it is set only to "Archive". Someone in > > Google had a similar problem and the respondant suggested a file.close > > method. There is no such method. There is a fileclose(buffer#) method for > > datafiles. > > > > Thanks much! > > -- > > Tom > > It is also nice Herfried's suggestion (the second one) based on
streams. If you do not need to resize the image, it would be interesting to measure which one is faster, to use DrawImage or the stream. I suspect that the second way might be faster. If you try it out let us know! -tom (1) :) TomA ha scritto: Show quoteHide quote > Thanks Tom (1), > > Your suggestion worked. I had to make several changes in the program. In > the event someone else has the same problem and is searching, I loaded the > image clone into the picturebox using the following code: > > Private m_Bitmap As Bitmap ' Form level > > Dim bm As New Bitmap(ImagefileName) > m_Bitmap = New Bitmap(bm.Width, bm.Height) > Dim gr As Graphics = Graphics.FromImage(m_Bitmap) > gr.DrawImage(bm, 0, 0) > bm.Dispose() > picPhoto.Image = m_Bitmap > > The file was no longer locked - the picturebox contained a clone and I > disposed of the original file from memory. I was free to delete the file > without a file locking challenge. > > Take care! > > Tom (2) > -- > Tom > > > "tommaso.gasta***@uniroma1.it" wrote: > > > Try loading. Making a in memory copy of the image (drawimage). Dispose > > original Image used for load. > > Let me know... > > > > -tom > > > > TomA ha scritto: > > > > > Hi All, > > > > > > I have a picturebox on a form containing the photo of a person. As you > > > advance through the records, the photo updates. Rather than storing the > > > images in an inefficient blob field in a table, I have the separate images > > > stored in an image directory with primary key as the filename (3476.jpg). If > > > the image exists, I display the image. All this works fine. > > > > > > I have a buttons that allow for the insertion and deletion of photos. The > > > "add" works fine too. But I am getting errors when I attempt to delete an > > > image - "file is being used by another process." The only time the image is > > > "used" is when I display it on the form. I am assuming the form/picturebox > > > is locking the image (what else?). I coded the procedure to reset the image > > > to a default missing image photo. That did not work so I set the picturebox > > > to nothing first. That did not work either. I also verified the string > > > being passed to the delete.file method. Here is my code: > > > > > > > > > Private Sub cmdRemovePhoto_Click(ByVal sender As System.Object, ByVal e > > > As System.EventArgs) Handles cmdRemovePhoto.Click > > > > > > Dim intReturn As Integer > > > > > > intReturn = MsgBox("Delete this Photo? This cannot be reversed.", > > > MsgBoxStyle.YesNo + MsgBoxStyle.Question + MsgBoxStyle.DefaultButton2, > > > "Warning...") > > > > > > If intReturn = 6 Then > > > > > > picPhoto.Image = Nothing > > > > > > picPhoto.Image = Image.FromFile(pubGlobalSetup & > > > "\imageFolder\missingphoto.jpg") > > > > > > Try > > > > > > File.Delete(pubGlobalSetup & "\imageFolder\" & > > > LTrim(Str(intContactID)) & ".jpg") ' <--Error Here > > > > > > cmdRemovePhoto.Enabled = False > > > > > > Catch ee As Exception > > > > > > MsgBox(ee.Message) > > > > > > End Try > > > > > > End If > > > > > > End Sub > > > > > > > > > > > > I would sure appreciate any ideas. This should be easy. I have no clue > > > what could be locking the file. It has to be the picturebox doesn't it? But > > > at the time I am deleting, it has been replaced. I checked the file > > > attribues at the time I delete and it is set only to "Archive". Someone in > > > Google had a similar problem and the respondant suggested a file.close > > > method. There is no such method. There is a fileclose(buffer#) method for > > > datafiles. > > > > > > Thanks much! > > > -- > > > Tom > > > > "TomA" <T***@discussions.microsoft.com> schrieb: Check out the snippets in the article below -- they demonstrate ways to load > I have a buttons that allow for the insertion and deletion of photos. The > “add†works fine too. But I am getting errors when I attempt to delete an > image – “file is being used by another process.†The only time the image > is > “used†is when I display it on the form. I am assuming the > form/picturebox > is locking the image (what else?). an image without locking the file: <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/>
ISO-8859-1 encoding of an xml string
Lauching a form from another form Lookup table in a Label is Recursive Procedures/Function what i need to solve my problem? consume web service via httpwebrequest Limit to range of colors displayed by Label control? How to remove an empty array ? Batch file question Transitioning to VB.NET forum |
|||||||||||||||||||||||