ieyla
ieyla

Reputation: 25

retrieve image in database mysql by using vb

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'."

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

Upvotes: 0

Views: 5600

Answers (3)

user3101758
user3101758

Reputation: 1

retrieve image in database MySQL by using VB. It's working..

But How to retrieve image in database mysql by using webservice?

Try
        Dim adapter As New MySqlDataAdapter
        adapter.SelectCommand = dbcomm

        Dim Data = New DataTable

        adapter = New MySqlDataAdapter("SELECT image,img_filename FROM image_test WHERE img_id='" + txt_image_id.Text + "'", dbconn)

        Dim commandbuild = New MySqlCommandBuilder(adapter)
        adapter.Fill(Data)

        Dim lb() As Byte = Data.Rows(0).Item("image")
        'Dim fl = Data.Rows(0).Item("img_filename")
        Dim lstr As New System.IO.MemoryStream(lb)
        'txt_RetrieveFilename.Text = fl

        PictureBox2.Image = Image.FromStream(lstr)
        PictureBox2.SizeMode = PictureBoxSizeMode.StretchImage
        lstr.Close()
    Catch ex As Exception
        MsgBox(ex.ToString())
    End Try

Upvotes: 0

jasperagrante
jasperagrante

Reputation: 354

You could use memory stream to handle the bytes from the db and convert it to image.

Use this code to convert bytes to image:

Dim bytes() as byte
bytes = (objdr("picture"))

Dim memStream as New MemoryStream(bytes)
PictureBox1.image = Drawing.Image.FromStream(memStream)

Upvotes: 0

Prince Jea
Prince Jea

Reputation: 5680

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

For more info refer to this link

Upvotes: 1

Related Questions