即使在处理位图后我也无法删除位图源文件。
我想更改图像属性项,然后将图像源的文件名更改为图像日期的形式。当我在重命名副本后尝试删除原始图像时,我收到通常的消息:访问路径'H:\Photos \ Test Folder \ MBR_1.JPG'被拒绝。这个问题的常见答案是处理图像。我做了,但我仍然得到了消息。如果代码看起来很有趣,那是因为它是VB.NET。
我尝试过:
I want to change an image property item and then change the filename of the image source to a form of the date of the image. When I try to delete the original image after renaming a copy, I get the usual message: "Access to the path 'H:\Photos\Test Folder\MBR_1.JPG' is denied." The common reply to this problem is to dispose the image. I did, but I still get the message. If the code looks funny to you, that's because it is VB.NET.
What I have tried:
Imports System.IO
Imports System.Drawing.Imaging
Sub test(ByVal szFolder As String)
Dim aszFiles() As String = Directory.GetFiles(szFolder)
For intFile As Integer = aszFiles.Length - 1 To 0 Step -1
Dim szOriginalFile As String = aszFiles(intFile)
' Determine if this file is a .jpg or .avi file
Dim szExtension As String = LCase(Path.GetExtension(szOriginalFile))
Select Case szExtension
Case ".jpg", ".jpeg"
Try
Dim szFilePath As String = Path.GetDirectoryName(szOriginalFile)
If szFilePath = "" Then szFilePath = Path.GetPathRoot(szOriginalFile) ' If root path is selected.
If Microsoft.VisualBasic.Right(szFilePath, 1) <> "\" Then szFilePath &= "\"
Dim szTempBakFile As String = Path.GetDirectoryName(szOriginalFile) & "\" & _
Path.GetFileNameWithoutExtension(szOriginalFile) & ".bak"
If Microsoft.VisualBasic.Right(szFilePath, 1) <> "\" Then szFilePath &= "\"
Dim szNewFile As String = Path.GetDirectoryName(szOriginalFile) & "\" & _
"File" & intFile.ToString & ".jpeg"
If szNewFile <> szOriginalFile Then
Dim Image1 As Bitmap = New Bitmap(szOriginalFile) ' If file has no bitmap, Try-Catch goes to Next File
Image1.Save(szTempBakFile, ImageFormat.Jpeg) ' save Image1 as "old name.bak"
Image1.Dispose() ' Unlock file
File.Move(szTempBakFile, szNewFile) ' Rename image with new name
File.Delete(szOriginalFile) ' Delete file
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Select
Next
End Sub
如果我从中提取代码,并通过预设文件名调整它以在我的系统上工作:
If I extract the code from that, and tweak it to work on my system by presetting the file names:
Dim szOriginalFile As String = "D:\Temp\MyPic.jpg"
Dim szTempBakFile As String = "D:\Temp\MyPicTemp.jpg"
Dim szNewFile As String = "D:\Temp\MyPicNew.jpg"
Dim Image1 As Bitmap = New Bitmap(szOriginalFile)
Image1.Save(szTempBakFile, ImageFormat.Jpeg)
Image1.Dispose()
File.Move(szTempBakFile, szNewFile)
File.Delete(szOriginalFile)
它工作正常:我最终得到一个输出新名称下的文件。
如果我在运行之间重新命名文件,它可以在不退出应用程序的情况下多次执行此操作。
所以事实并非如此导致问题的代码 - 它是一个真正的权限问题,或代码中的其他东西是锁定文件。
BTW:这是一个非常非常差的通过加载和编写JPG文件来重命名JPG文件的想法:每次保存JPG图像时,由于使用了有损压缩算法,因此会丢失细节。六次迭代,你会得到一个非常模糊的图像,与原始图像几乎没有相似之处。
It works fine: I end up with a single output file under the new name.
Provided I rename the file back between runs, it can do it as many times as a like without exiting the app.
So it isn't that code that is causing the problem - it's either a genuine permissions problem, or something else in your code is locking the file.
BTW: it's a very, very poor idea to rename JPG files by loading and writing them: each time you save a JPG image, you lose detail because it uses a lossy compression algorithm. Half a dozen iterations, and you will get a very fuzzy image that bears little resemblance to the original.