使用vb在数据库mysql中检索图像 [英] retrieve image in database mysql by using vb

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

问题描述

我想通过不使用id从vb中的数据库mysql检索图像.但是为什么会有错误无法将类型为'System.Byte []'的对象转换为类型为'System.Drawing.Image'."

i want to retrieve image from database mysql in vb by using no id. but why i have error "Unable to cast object of type 'System.Byte[]' to type 'System.Drawing.Image'."

这是我的要求.我正在使用Visual Basic 2008和数据库mysql.图片格式BLOB.例如[BLOB-22.1 KiB]

this is my codding. i'm using visual basic 2008 and database mysql. picture format BLOB. eg.[BLOB - 22.1 KiB]

Imports MySql.Data.MySqlClient
Imports System.Drawing.Imaging
Public Class Form_Popup

    Private Sub Form_Popup_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        objconn = New MySqlConnection("server=localhost;database=new;userid=root;password= 'root'")
        objconn.Open()
        strsql = "select * from peribadi where Noid =@field1"
        command = (New MySql.Data.MySqlClient.MySqlCommand(strsql, objconn))
        With command
            .Parameters.AddWithValue("@field1", FormRegister.TextBox1.Text)
        End With

        objdr = command.ExecuteReader
        If (objdr.Read()) Then
            Label4.Text = (objdr("Name"))
            Label5.Text = (objdr("Address"))
            PictureBox1.Image = (objdr("picture"))

        End If

    End Sub

推荐答案

错误在于检索图像. 您需要先将图像转换为字节数组. 在将其显示到图片框之前.

The error is in retrieving your image. you need to convert the image first to byte array. before you display it to the picture box..

 Public Function ResizeImageWithAspect(ByVal picImage As Image, ByVal newWidth As Integer) As Bitmap
    Dim original As Image = picImage
    If Not original Is Nothing Then
        //Find the aspect ratio between the height and width.
        Dim aspect As Single = CSng(original.Height) / CSng(original.Width)

        //Calculate the new height using the aspect ratio
        // and the desired new width.
        Dim newHeight As Integer = CInt((newWidth * aspect))

        //Create a bitmap of the correct size.
        Dim temp As New Bitmap(newWidth, newHeight, original.PixelFormat)

        //Get a Graphics object from the bitmap.
        Dim newImage As Graphics = Graphics.FromImage(temp)

        //Draw the image with the new width/height
        newImage.DrawImage(original, 0, 0, newWidth, newHeight)

        //Dispose of our objects.
        Return temp
        original.Dispose()
        temp.Dispose()
        newImage.Dispose()
    Else
        Return Nothing
    End If

End Function

Public Function ByteToImage(ByVal blob() As Byte) As Bitmap
    Dim mStream As New System.IO.MemoryStream
    Dim pData() As Byte = DirectCast(blob, Byte())
    mStream.Write(pData, 0, Convert.ToInt32(pData.Length))
    Dim bm As Bitmap = New Bitmap(mStream, False)
    mStream.Dispose()
    Return bm
End Function

Public Function FileImageToByte(ByVal filePath As String) As Byte()
    Dim fs As FileStream = New FileStream(filePath, FileMode.Open, FileAccess.Read)
    Dim br As BinaryReader = New BinaryReader(fs)
    Dim bm() As Byte = br.ReadBytes(fs.Length)
    br.Close()
    fs.Close()
    Dim photo() As Byte = bm
    Return photo
End Function

有关更多信息,请参考此链接

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

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