2012-10-18 27 views
8

嗨我想將圖像轉換爲字節數組傳遞到SQL作爲字節()。我試着去使用圖像轉換器,但它一直未能如何將System.Drawing.Image轉換爲字節數組?

Dim converter As New ImageConverter 
nRow.Signature = converter.ConvertTo(imgSignature, TypeOf(Byte()) 

我不斷收到錯誤字節是一種不表達

回答

12

的VB.NET typeof運算符不去做你認爲它。有些混淆可能是由於C#typeof運營商。 VB.NET等價物是GetType()函數。這工作正常:

Dim converter As New ImageConverter 
nRow.Signature = converter.ConvertTo(imgSignature, GetType(Byte()) 

類型轉換器使用一個MemoryStream使用PNG圖像格式進行轉換。

14

可以使用MemoryStream。通過將圖像保存爲MemoryStream,可以從圖像獲取數據的字節數組:

Dim ms = new MemoryStream() 
imgSegnature.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg) ' Use appropriate format here 
Dim bytes = ms.ToArray() 
+1

謝謝你,對我很有幫助。 –

相關問題