Visual Basic读取将图像保存到Access中

时间:2016-01-05 05:29:13

标签: vb.net

有人可以给我一个解释这个代码,并且为了您的信息,我试图保存一个显示在图片框中的图片并将其保存到Microsoft访问数据库,我不明白什么意思是,特别是0。

 If Not PictureBox1.Image Is Nothing Then

            Dim fsreader As New FileStream(OpenFileDialog1.FileName, FileMode.Open, FileAccess.Read)
            Dim breader As New BinaryReader(fsreader)
            Dim imgbuffer(fsreader.Length) As Byte
            breader.Read(imgbuffer, 0, fsreader.Length)
            fsreader.Close()

1 个答案:

答案 0 :(得分:0)

您的代码段代码是将图片二进制文件保存到VB.Net中的imgbuffer变量的代码。此代码段未将此图像保存到Ms Access中。但...

我将尝试解释它,此代码的工作原理如何:

If Not PictureBox1.Image Is Nothing Then 'This code for checking if there's any images in the picture box, if it's so run next code.
    Dim fsreader As New FileStream(OpenFileDialog1.FileName, FileMode.Open, FileAccess.Read)  'This variable declared for getting file name, access file and the read mode. And open it using `Filestream` variable, so after it's opened, the length of images will be saved into `fsreader` variable
    Dim breader As New BinaryReader(fsreader) 'This code for reading the binary of images from `fsreader` variable and put the image buffer from range of byte to a variable
    Dim imgbuffer(fsreader.Length) As Byte  'This variable is use to collect image buffer with specified length of binary images from `fsreader` variable.
    breader.Read(imgbuffer, 0, fsreader.Length) 'This code is use for reading binary of images with specified length of byte and put the image buffer into a imgbuffer variable. The number `0` in the brackets means the binary images will be put fully in imgbuffer. Why `0` ? can I use larger than `0` ? yes you can but the image will be corrupt or differ from the original, so you must read the binary images from 0 to the image file size.
    fsreader.Close() 'This is for closing `fsreader` from accessing images file.

那么,下一步是什么?

通过该代码段,您可以继续处理imgbuffer变量,使用Ms-Access中提供的某些库集合将其保存到VB.Net,例如OleDB或还要别的吗。最后,我更希望使用MemoryStream放置图片缓冲区并将其保存到Bitmap变量中。

希望这能解释您的代码段。