2012-03-20 88 views
0

我想通過使用沒有id從vb數據庫mysql中檢索圖像。但爲什麼我有錯誤「無法投射'System.Byte []'類型的對象鍵入'System.Drawing.Image'。」通過使用vb檢索數據庫mysql中的圖像

這是我的編碼。我使用visual basic 2008和數據庫mysql。圖片格式BLOB。例如[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 [email protected]" 
     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 
+0

使用 「無身份證」?你的意思是「使用數字ID」嗎? – Bojangles 2012-03-20 00:47:28

+0

是的。沒有id是數字值 – ieyla 2012-03-20 01:25:18

回答

1

錯誤在於檢索圖像。 您需要先將圖像轉換爲字節數組。 你把它顯示在圖片框前..

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

+0

非常感謝你! :) – ieyla 2012-03-20 03:33:23

0

你可以使用內存流處理來自DB字節並將其轉換爲圖像。用VB

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

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

好的非常感謝你,這是偉大的作品。 :) – ieyla 2012-03-20 03:43:54

+0

但我有錯誤**'paramater無效**在線>昏暗的memStream作爲新的MemoryStream(字節)。 當圖片不可用時,請列出我的代碼以在圖片box1中放置錯誤圖片。 – ieyla 2012-04-04 07:35:02

+0

你可以使用'Try ... Catch Statement'來捕捉錯誤 – jasperagrante 2012-04-08 04:33:17

0

數據庫MySQL的檢索圖像:

使用此代碼字節轉換圖像。它的工作..

但如何通過使用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 
+0

嗯......看起來更像一個問題而不是答案,它是什麼? – kleopatra 2013-12-18 09:33:37

相關問題