VB.NET中使用的图像 [英] Image used in VB.NET

查看:101
本文介绍了VB.NET中使用的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!我有这个问题后打开图像使用开放文件对话方法和更新数据库内的图像。我更新它后,我尝试移动我上传到数据库的图片,但它似乎导致错误该过程无法完成,因为该文件正由另一个程序使用。因此我必须关闭vb应用程序,以便我可以将文件移动到文件夹,打开和关闭应用程序有点烦人。有没有办法在使用后移动文件而不关闭vb应用程序?



这里是我正在使用的代码...非常感谢:)



尝试

Dim dlgimage As FileDialog = New OpenFileDialog()
dlgimage.Filter = < span class =code-string> 图像文件(* .jpg)| * .jpg

如果dlgimage .ShowDialog()= DialogResult.OK然后
imgName = dlgimage.FileName

Dim newimg As New Bitmap(imgName)

P1picbox.SizeMode = PictureBoxSizeMode.StretchImage
P1picbox.Image = DirectCast(newimg,Image)
Else
退出Sub
结束如果
dlgimage = Nothing
Catch ex As Exception
MsgBox (ex.ToString())
结束尝试





我尝试过:



我试过谷歌搜索,但它似乎只是显示如何使用打开文件对话框打开文件,但我要做的是在我将数据库上传到数据库之后释放应用程序使用的资源..我想要将它移动到另一个文件夹而不关闭应用程序。

解决方案

使用Bitmap构造函数打开图像文件时,文件在图像的生命周期内保持打开状态 - 即直到实际的Image类实例为Disposed。如果你看一下文档,就可以很清楚了:位图构造函数(String) )(System.Drawing) [ ^ ]

文件保持锁定状态,直到分配位图为止。



这意味着您不能写入文件,移动文件,重命名文件或删除文件,直到图像完成。

有这样的方法:读取文件作为使用File.ReadAllBytes的字节数组,然后从字节创建一个MemoryStream,并从中构造一个图像:

  Dim  bytes  As   Byte ()= File.ReadAllBytes(  D:\ Temp \ MyMmage.bmp
Dim ms 作为 MemoryStream(字节)
Dim useThisImage As Image = Image.FromStream(ms)


问题是您在 newimg 中持有的资源,这是从文件创建的位图。请参阅位图构造函数(String)(System.Drawing)中的备注部分 [ ^ ]。

Hi all! I have this problem of after opening an image using open file dialogue method and updating the image inside the database. After I update it i try to move the picture that i uploaded to the database, but it seems that it causes an error of "The process cannot be completed because the file is being used by another program." And thus I have to close the vb app, so that I can moved the file to the folder, and it is a bit annoying to open and close the app. Is there a way to moved the file after being used and not closing the vb app?

here is the code I'm using... many thanks :)

Try

            Dim dlgimage As FileDialog = New OpenFileDialog()
            dlgimage.Filter = "Image File (*.jpg)|*.jpg"

            If dlgimage.ShowDialog() = DialogResult.OK Then
                imgName = dlgimage.FileName

                Dim newimg As New Bitmap(imgName)

                P1picbox.SizeMode = PictureBoxSizeMode.StretchImage
                P1picbox.Image = DirectCast(newimg, Image)
            Else
                Exit Sub
            End If
            dlgimage = Nothing
        Catch ex As Exception
            MsgBox(ex.ToString())
        End Try



What I have tried:

I have tried googling but it seems it just shows how to open file using open file dialogue,,, but what Im tring to do is to release the resources from being used by the app after I have uploaded it to the database.. I want to move it to another folder without closing the app.

解决方案

When you open an image file using the Bitmap constructor, the file is held open for the lifetime of the Image - i.e. until the actual Image class instance is Disposed. If you look at teh documentation, it's pretty clear on this: Bitmap Constructor (String) (System.Drawing)[^]

The file remains locked until the Bitmap is disposed.


Which means that you cannot write to the file, move the file, rename the file or delete the file until the Image has been finished with.
There are ways round this: Read the file as an array of bytes using File.ReadAllBytes, then create a MemoryStream from the bytes, and construct an image from that:

Dim bytes As Byte() = File.ReadAllBytes("D:\Temp\MyImage.bmp")
Dim ms As New MemoryStream(bytes)
Dim useThisImage As Image = Image.FromStream(ms)


The problem is the resource you are holding in newimg which is the Bitmap created from the file. See the Remarks section at Bitmap Constructor (String) (System.Drawing)[^].


这篇关于VB.NET中使用的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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