图像 vb.net 文件被另一个进程使用错误

发布于 2024-08-19 07:59:23 字数 1752 浏览 4 评论 0原文

我正在编写一个小程序,通过打开的文件对话框选择图片。当我选择一张图片时,我希望它覆盖当前图片并显示新图像。现在,我选择具有不同扩展名的图像没有任何问题。因此,当我当前有 .png 时,我可以选择 .jpg 但当我选择与当前图像具有相同扩展名的图像时,我会收到错误: 该进程无法访问文件“C:\Users....\woontypeimages\chalet_foto.jpg”,因为它正在被另一个进程使用。

    If ofd.ShowDialog() = Windows.Forms.DialogResult.OK Then
        Dim sFilename As String = cboWoningtypesWoningtype.SelectedItem.ToString & "_foto" & System.IO.Path.GetExtension(ofd.FileName)
        System.IO.File.Copy(ofd.FileName, Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\Camping Relaxx\woontypeimages\" & sFilename, True)
        txtWoningtypesFoto.Text = sFilename
        updateImages()
    End If

    Private Sub updateImages()

    Try
        picFoto.Image = Nothing
        txtWoningtypesFoto.BackColor = clrReadonly
        txtWoningtypesFoto.ForeColor = Color.Black
        picFoto.Image = System.Drawing.Image.FromFile(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\Camping Relaxx\woontypeimages\" & txtWoningtypesFoto.Text)
    Catch ex As Exception
        txtWoningtypesFoto.BackColor = clrError
        txtWoningtypesFoto.ForeColor = Color.White
    End Try
    Try
        picGrondplan.Image = Nothing
        txtWoningtypesGrondplan.BackColor = clrReadonly
        txtWoningtypesGrondplan.ForeColor = Color.Black
        picGrondplan.Image = System.Drawing.Image.FromFile(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\Camping Relaxx\woontypeimages\" & txtWoningtypesGrondplan.Text)
    Catch ex As Exception
        txtWoningtypesGrondplan.BackColor = clrError
        txtWoningtypesGrondplan.ForeColor = Color.White
    End Try

End Sub

如果有人可以帮助我,我会很高兴

提前致谢

I'm writing a little program where I select a picture through an open file dialogue. When I selected a picture I want it to overwrite the current picture and display the new image. Now I don't have any problems with picking an image with a different extension. So when I currently have a .png I can select a .jpg but when I choose an image with the same extension as the current image I get an error:
The process cannot access the file 'C:\Users....\woontypeimages\chalet_foto.jpg' because it is being used by another process.

    If ofd.ShowDialog() = Windows.Forms.DialogResult.OK Then
        Dim sFilename As String = cboWoningtypesWoningtype.SelectedItem.ToString & "_foto" & System.IO.Path.GetExtension(ofd.FileName)
        System.IO.File.Copy(ofd.FileName, Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\Camping Relaxx\woontypeimages\" & sFilename, True)
        txtWoningtypesFoto.Text = sFilename
        updateImages()
    End If

    Private Sub updateImages()

    Try
        picFoto.Image = Nothing
        txtWoningtypesFoto.BackColor = clrReadonly
        txtWoningtypesFoto.ForeColor = Color.Black
        picFoto.Image = System.Drawing.Image.FromFile(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\Camping Relaxx\woontypeimages\" & txtWoningtypesFoto.Text)
    Catch ex As Exception
        txtWoningtypesFoto.BackColor = clrError
        txtWoningtypesFoto.ForeColor = Color.White
    End Try
    Try
        picGrondplan.Image = Nothing
        txtWoningtypesGrondplan.BackColor = clrReadonly
        txtWoningtypesGrondplan.ForeColor = Color.Black
        picGrondplan.Image = System.Drawing.Image.FromFile(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\Camping Relaxx\woontypeimages\" & txtWoningtypesGrondplan.Text)
    Catch ex As Exception
        txtWoningtypesGrondplan.BackColor = clrError
        txtWoningtypesGrondplan.ForeColor = Color.White
    End Try

End Sub

If anyone could help me I would be pleased

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

护你周全 2024-08-26 07:59:23

您不必担心 Dispose(),您可以使用 PictureBox 的Load(string) 方法不会锁定文件。

Me.PictureBox1.Load("C:\test.png")

Instead of worrying about Dispose() you can instead use the Load(string) method of the PictureBox which won't lock the file.

Me.PictureBox1.Load("C:\test.png")
相守太难 2024-08-26 07:59:23

使用这些 :

picFoto.Image.Dispose()
picGrondplan.Image.Dispose()

而不是 :

picFoto.Image = Nothing
picGrondplan.Image = Nothing

Image.FromFile 方法会保持对源文件的锁定,直到图像被释放为止。将对象设置为空并不会立即处理它 - 垃圾收集器将在自己的时间内处理该问题(很可能直到您关闭带有打开图片框的表单为止)。需要立即释放文件句柄进行处置。

Use these :

picFoto.Image.Dispose()
picGrondplan.Image.Dispose()

instead of :

picFoto.Image = Nothing
picGrondplan.Image = Nothing

The Image.FromFile method maintains a lock on the source file until the image has been disposed. Setting an object to nothing does not immediately dispose it - the garbage collector will take care of that in its own time (which might well not be until you've closed the form with the picture box on). Dispose is required to immediately free the file handle.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文