使用vb.net替换特定文件夹中的图片 [英] Replace Picture in a specific Folder using vb.net

查看:289
本文介绍了使用vb.net替换特定文件夹中的图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Goodmorning。



有没有人知道如何更换文件夹中的图像?

因为当我尝试这个时,我得到了错误GDI +中发生的一般错误



这是我的流程:

*首先我将图像加载到图片框,以便用户可以看到当前图片

 如果 M_FilePath = < span class =code-keyword> Nothing  然后 
其他
PictureBox2.Image = Nothing
PictureBox2.Image = Image.FromFile(M_FilePath)
End 如果





*并浏览新的图片

  Dim  OD  As   OpenFile对话框

使用 OD
如果 .ShowDialog = Windows.Forms .DialogResult.OK 然后
PictureBox2.Image = 没什么
PictureBox2.Image = Image.FromFile(.FileName)
结束 如果
结束









*然后,我想更新我浏览的新图片。

 PictureBox2.Image.Save(Application.StartupPath +\ ITEM \+ txtItemCode.Text.Trim +.jpg)





但我仍然收到错误。

任何建议都会非常感激。非常感谢你提前。

解决方案

这没有任何意义,但一个大问题是你正在使用 Image .FromFile 。这将使文件在 Image 对象的生命周期内保持锁定状态,并阻止您向其写入新文件内容。为了解决这个问题,你必须创建一个FileStream对象并将其传递给 Image.FromStream 。然后你可以关闭FileStream,然后以你想要的任何方式写入文件。



示例...... [ ^ ]

Goodmorning.

Is there anyone know how to replace an image in a folder?
because when im trying this, I get an error "A Generic Error Occured in GDI+"

Here is my process:
*First I load the image in a picture box, so that the user can see the current picture

If M_FilePath = Nothing Then
           Else
               PictureBox2.Image = Nothing
                PictureBox2.Image = Image.FromFile(M_FilePath)
           End If



*and browse a new picture

Dim OD As New OpenFileDialog

       With OD
           If .ShowDialog = Windows.Forms.DialogResult.OK Then
               PictureBox2.Image = Nothing
               PictureBox2.Image = Image.FromFile(.FileName)
           End If
       End With





*Then, I want to update the new picture i browsed.

PictureBox2.Image.Save(Application.StartupPath + "\ITEM\" + txtItemCode.Text.Trim + ".jpg")



but still i got the error.
Any Suggestions will be so much appreciated. Thank you so much in advance.

解决方案

This doesn't really make any sense, but a big problem is that you're using Image.FromFile. This will keep the file locked for the lifetime of the Image object and prevent you from writing new file content to it. In order to get around this, you have to create a FileStream object and pass that to Image.FromStream. Then you can close the FileStream and then write to the file any way you want.

Examples...[^]


这篇关于使用vb.net替换特定文件夹中的图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆