vb.net 来自 MSACCESS 表的 PictureBox 中的图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15104168/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 12:33:05  来源:igfitidea点击:

Image in PictureBox from MSACCESS table

vb.netpicturebox

提问by user2113856

Can you tell me how I can to retrieve an image from a field (type oleobject) named IMGFAM in a MSACCESS table to a group (many as records in a table) of pictureboxes at runtime.

您能告诉我如何在运行时从 MSACCESS 表中名为 IMGFAM 的字段(oleobject 类型)检索图像到一组图片框(许多作为表中的记录)。

I can display buttons but I can't do that with the picture (jpg) obtained from the field. Thanks for your help.

我可以显示按钮,但不能用从现场获得的图片 (jpg) 来显示。谢谢你的帮助。

P.S: I used MS Access 2007 and VB.net 2008 express.

PS:我使用了 MS Access 2007 和 VB.net 2008 express。

回答by saysansay

Before you want to manipulate image to db Access, you must to know basic class of IO.MemoryStream. Below Sample code from vb forum http://www.vbforums.com/showthread.php?489787-02-03-Loading-JPG-images-from-Access-to-VB.Net

在将图像操作到db Access 之前,您必须了解IO.MemoryStream 的基本类。下面来自 vb 论坛的示例代码http://www.vbforums.com/showthread.php?489787-02-03-Loading-JPG-images-from-Access-to-VB.Net

Imports System.Data.OleDb
Imports System.IO

Public Class Form1

Private connection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\db1.mdb;User Id=admin;Password=;")

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Me.PictureBox1.Image = Image.FromFile(Path.Combine(Application.StartupPath, "Properties.jpg"))
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Try
        Dim stream As New IO.MemoryStream

        Me.PictureBox1.Image.Save(stream, Imaging.ImageFormat.Jpeg)

        Dim command As New OleDbCommand("INSERT INTO Table1 (Picture) VALUES (@Picture)", connection)

        command.Parameters.AddWithValue("@Picture", stream.GetBuffer())

        connection.Open()
        command.ExecuteNonQuery()
        connection.Close()
        command.Dispose()
        stream.Dispose()
    Catch ex As Exception
        MessageBox.Show(ex.ToString())
    End Try
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Try
        Dim command As New OleDbCommand("SELECT Picture FROM Table1 WHERE ID = @ID", connection)

        Me.connection.Open()
        command.Parameters.AddWithValue("@ID", Me.GetGreatestID())

        Dim pictureData As Byte() = DirectCast(command.ExecuteScalar(), Byte())

        connection.Close()
        command.Dispose()

        Dim stream As New IO.MemoryStream(pictureData)

        Me.PictureBox2.Image = Image.FromStream(stream)
        stream.Dispose()
    Catch ex As Exception
        MessageBox.Show(ex.ToString())
    End Try
End Sub

回答by rasingh

Dim OpenFileDialog1 As New OpenFileDialog
        With OpenFileDialog1

            .CheckFileExists = True

            .ShowReadOnly = False

            .Filter = "All Files|*.*|Bitmap Files (*)|*.bmp;*.gif;*.jpg"

            .FilterIndex = 2

            If .ShowDialog = DialogResult.OK Then

                ' Load the specified file into a PictureBox control.

                pbNewImage.Image = Image.FromFile(.FileName)

            End If

for detail one can visit my blog: [enter link description here][1]