从数据库中检索图像并显示在图片框中 [英] retrive image from database and show in picture box

查看:107
本文介绍了从数据库中检索图像并显示在图片框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从数据库中检索图像并显示在图片框中.

解决方案

这不是问题.这甚至不是一句话.一个问题是我想使用数据库中的图像并将其显示在图片框中.我尝试了这个,但是在这行代码中,它没有达到我的期望".

您可以使用从数据库获得的字节创建内存流,可以使用该内存流构造图像,并将该图像传递到图片框.非常简单,但是有几个步骤,因此请尝试一下,并在遇到问题时发布特定代码.
浏览图像

 私有  btnBrowse_Click( ByVal 发​​件人 As 系统.对象 ByVal  e  As  System.EventArgs)句柄 btnBrowse.Click
        Dim 一个 As  新建 OpenFileDialog
       a.Filter = " 
       a.Title = " 
       如果 a.ShowDialog = DialogResult.OK 然后
            Dim  img  As 位图= Image.FromFile(a.FileName)
           img.SetResolution( 72  72 )
           PictureBox1.BackgroundImage = img
           PictureBox1.BackgroundImageLayout = ImageLayout.Stretch
           IsChanged = 
       结束 如果
   结束 



保存在数据库中

 ''' 从Picturebox中读取图像,并以二进制格式在sql中保存图像
 ' 请注意IsChanged是布尔变量,在页面级别声明,在更改图像时将其设置为true.当图像没有变化时,请勿更新图像.
        如果 IsChanged = 真实 然后
            IsChanged = 错误
             Dim  stream1  As   IO.MemoryStream
             img  As 位图
            img = PictureBox1.BackgroundImage
            使用 img
                使用 As   IO.MemoryStream
                    尝试
                        img.Save(stream,Imaging.ImageFormat.Bmp)
                    捕获,例如 As 异常
                        尝试
                            img.Save(stream,Imaging.ImageFormat.Png)
                        捕获 ex1  As 异常
                            尝试
                                img.Save(stream,Imaging.ImageFormat.Gif)
                            捕获 ex2  As 异常
                                尝试
                                    img.Save(stream,Imaging.ImageFormat.MemoryBmp)
                                捕获 ex3  As 异常

                                结束 尝试
                            结束 尝试

                        结束 尝试
                    结束 尝试
                    stream1 =流
                    stream.Dispose()
                结束 使用
            结束 使用
            尝试
                Cmd.CommandText = " 
                Cmd.Parameters.Clear()
                Cmd.Parameters.AddWithValue(" ,stream1.GetBuffer)
                Cmd.ExecuteNonQuery()
                Cmd.Parameters.Clear()
                stream1.Dispose()
            捕获,例如 As 异常
            结束 尝试 



从数据库中检索

 ''' 从数据库&显示在图片框中
 尝试
                 Dim  dt  As   New  DataTable
                dt = DBGetData(" )
                对于 i =  0   dt.Rows .Count- 1 
                    txtNm.Text = dt.Rows(i)(" )
                    txtAdd.Text = dt.Rows(i)(" )
                    txtWebsite.Text = dt.Rows(i)(" )
                    txtMailId.Text = dt.Rows(i)(" )
                    txtCont1.Text = dt.Rows(i)(" )
                    txtCont2.Text = dt.Rows(i)(" )

                     Dim  ImgData  As   Byte ()=  DirectCast (dt.Rows(i)(" ),字节())

                     Dim  As   IO.MemoryStream( ImgData)
                    使用流
                        图= Image.FromStream(stream)
                    结束 使用
                    stream.Dispose()
                    PictureBox1.BackgroundImage =图
                    PictureBox1.BackgroundImageLayout = ImageLayout.Stretch
                下一步
            捕获,例如 As 异常
            结束 尝试 



祝您编码愉快!
:)


retrive image from database and show in picture box.

解决方案

This is not a question. It''s not even a sentence. A question would be ''I want to use an image from the database and show it in a picture box. I tried this and this, but in this line of code, it''s not doing what I expect''.

You can create a memory stream from the bytes you get from the DB, you can use that to construct an image, and pass that image to your picture box. Very simple, but several steps, so try them, and post specific code when you get stuck with it.


Code for browse-save-retrieve image

browse image

Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click
       Dim a As New OpenFileDialog
       a.Filter = "Image(*.jpg,*.png,*.bmp,*.JPeg,*.Gif)|*.jpg;*.png;*.bmp;*.JPeg;*.Gif;"
       a.Title = "Browse icon for ..."
       If a.ShowDialog = DialogResult.OK Then
           Dim img As Bitmap = Image.FromFile(a.FileName)
           img.SetResolution(72, 72)
           PictureBox1.BackgroundImage = img
           PictureBox1.BackgroundImageLayout = ImageLayout.Stretch
           IsChanged = True
       End If
   End Sub



save in database

'''read image from picturebox an Save image in sql in binary format
'note  IsChanged is boolean variable, declare on page level, set it true when image is changed. do not update image when there is no change in it.
        If IsChanged = True Then
            IsChanged = False
            Dim stream1 As New IO.MemoryStream
            Dim img As Bitmap
            img = PictureBox1.BackgroundImage
            Using img
                Using stream As New IO.MemoryStream
                    Try
                        img.Save(stream, Imaging.ImageFormat.Bmp)
                    Catch ex As Exception
                        Try
                            img.Save(stream, Imaging.ImageFormat.Png)
                        Catch ex1 As Exception
                            Try
                                img.Save(stream, Imaging.ImageFormat.Gif)
                            Catch ex2 As Exception
                                Try
                                    img.Save(stream, Imaging.ImageFormat.MemoryBmp)
                                Catch ex3 As Exception

                                End Try
                            End Try

                        End Try
                    End Try
                    stream1 = stream
                    stream.Dispose()
                End Using
            End Using
            Try
                Cmd.CommandText = "update Dtl set Img=@Img"
                Cmd.Parameters.Clear()
                Cmd.Parameters.AddWithValue("@Img", stream1.GetBuffer)
                Cmd.ExecuteNonQuery()
                Cmd.Parameters.Clear()
                stream1.Dispose()
            Catch ex As Exception
            End Try



retrieve from database

'''fatch image from database & display in picture box
            Try
                Dim dt As New DataTable
                dt = DBGetData("select * from Dtl")
                For i = 0 To dt.Rows.Count - 1
                    txtNm.Text = dt.Rows(i)("Nm")
                    txtAdd.Text = dt.Rows(i)("Add")
                    txtWebsite.Text = dt.Rows(i)("Website")
                    txtMailId.Text = dt.Rows(i)("MailId")
                    txtCont1.Text = dt.Rows(i)("ContNo1")
                    txtCont2.Text = dt.Rows(i)("ContNo2")

                    Dim ImgData As Byte() = DirectCast(dt.Rows(i)("Img"), Byte())

                    Dim stream As New IO.MemoryStream(ImgData)
                    Using stream
                        Img = Image.FromStream(stream)
                    End Using
                    stream.Dispose()
                    PictureBox1.BackgroundImage = Img
                    PictureBox1.BackgroundImageLayout = ImageLayout.Stretch
                Next
            Catch ex As Exception
            End Try



Happy Coding!
:)


这篇关于从数据库中检索图像并显示在图片框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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