我想檢索存儲在Oracle blob中的圖像,並將其放置在新的System.Drawing.Image實例中。我知道我可以將流寫入磁盤上的temp.bmp文件,並從那裏讀取它,但這對我來說已經足夠了。如何將blob對象直接轉換爲圖像?如何獲取作爲oracle blob存儲到圖像對象的圖像
0
A
回答
1
假設:
- 您使用的是微軟的客戶端(
System.Data.OracleClient
)。 - 你有一個合適的
OracleConnection
實例(connection
)。 - 您準備好了
OracleCommand
(command
,根據SELECT my_blob FROM my_table WHERE id=xx
)。
這應該是這樣的:
using (OracleDataReader odr=command.ExecuteReader())
{
reader.Read();
if (!dr.IsDBNull(0))
using (Stream s=(Stream)dr.GetOracleValue(0))
using (Image image=Image.FromStream(s))
return Copy(image);
}
,其中複製是
public static Image Copy(Image original)
{
Image ret=new Bitmap(original.Width, original.Height);
using (Graphics g=Graphics.FromImage(ret))
{
g.DrawImageUnscaled(original, 0, 0);
g.Save();
}
return ret;
}
爲什麼副本是必要見my blog post和/或KB 814675。
0
我知道這使用SQL但它應該爲您的需求
Dim cn As SqlConnection = Nothing
Dim cmd As SqlCommand = Nothing
Dim da As SqlDataAdapter = Nothing
Dim ms As MemoryStream = Nothing
Dim dsImage As Data.DataSet = Nothing
Dim myBytes() As Byte = Nothing
Dim imgJPG As System.Drawing.Image = Nothing
Dim msOut As MemoryStream = Nothing
Try
cn = New SqlConnection(ConnectionStrings("conImageDB").ToString)
cmd = New SqlCommand(AppSettings("sprocGetImage").ToString, cn)
cmd.CommandType = Data.CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@dmhiRowno", irowno)
da = New SqlDataAdapter(cmd)
dsImage = New Data.DataSet
da.Fill(dsImage, "image")
If dsImage.Tables(0).Rows.Count = 0 Then
Throw New Exception("No results returned for rowno")
End If
myBytes = dsImage.Tables(0).Rows(0)("Frontimage")
ms = New MemoryStream
ms.Write(myBytes, 0, myBytes.Length)
imgJPG = System.Drawing.Image.FromStream(ms)
'Export to JPG Stream
msOut = New MemoryStream
imgJPG.Save(msOut, System.Drawing.Imaging.ImageFormat.Jpeg)
imgJPG.Dispose()
imgJPG = Nothing
ms.Close()
sFrontImage = Convert.ToBase64String(msOut.ToArray())
dsImage = New Data.DataSet
da.Fill(dsImage, "image")
myBytes = dsImage.Tables(0).Rows(0)("Backimage")
ms = New MemoryStream
ms.Write(myBytes, 0, myBytes.Length)
imgJPG = System.Drawing.Image.FromStream(ms)
sBackImage = Convert.ToBase64String(ms.ToArray)
Catch ex As System.IO.IOException ' : An I/O error occurs.
Throw ex
Catch ex As System.ArgumentNullException ': buffer is null.
Throw ex
Catch ex As System.NotSupportedException ': The stream does not support writing. For additional information see System.IO.Stream.CanWrite.-or- The current position is closer than count bytes to the end of the stream, and the capacity cannot be modified.
Throw ex
Catch ex As System.ArgumentOutOfRangeException ': offset or count are negative.
Throw ex
Catch ex As System.ObjectDisposedException ' : The current stream instance is closed.
Throw ex
Catch ex As System.ArgumentException
Throw ex
Catch ex As System.Runtime.InteropServices.ExternalException ': The image was saved with the wrong image format
Throw ex
Catch ex As Exception
Throw ex
Finally
If cn IsNot Nothing Then
cn.Close()
cn.Dispose()
cn = Nothing
End If
If cmd IsNot Nothing Then
cmd.Dispose()
cmd = Nothing
End If
If da IsNot Nothing Then
da.Dispose()
da = Nothing
End If
If ms IsNot Nothing Then
ms.Dispose()
ms = Nothing
End If
If msOut IsNot Nothing Then
msOut.Close()
msOut.Dispose()
msOut = Nothing
End If
If dsImage IsNot Nothing Then
dsImage.Dispose()
dsImage = Nothing
End If
If myBytes IsNot Nothing Then
myBytes = Nothing
End If
If imgJPG IsNot Nothing Then
imgJPG.Dispose()
imgJPG = Nothing
End If
End Try
此代碼拉回了前面的圖像TIFF包裹JPEG所以代碼比背面用圖像有點不同的是相似的。
相關問題
- 1. 將圖像作爲blob存儲到Oracle數據庫的問題
- 2. 存儲靜態圖像,Blob存儲區
- 3. 存儲圖像並獲取圖像
- 4. 如何將圖像存儲在BLOB中?
- 5. 存儲圖像爲BLOB數據庫
- 6. 在2017年將圖像存儲爲blob?
- 7. 如何存儲和獲取圖像變量(SESSION)的圖像?
- 8. 在sqlite中存儲blob圖像
- 9. Db4o,Java:使用blob存儲圖像
- 10. 正常的圖像存儲或mySQL blob?
- 11. 存儲少量圖像:blob或fs?
- 12. 爪哇 - 存儲圖像作爲JavaDB之外的Blob
- 13. 保存MATLAB面對象作爲圖像
- 14. 如何將圖像存儲爲riak並從riak中獲取圖像?
- 15. 如何獲取以BLOB格式存儲在數據庫中的圖像的URL
- 16. 如何通過圖像滑塊ID獲取圖像並將其存儲到cookie?
- 17. 將base64圖像保存到移動服務SQL或Blob存儲?
- 18. 如何從ip攝像頭獲取圖像並將圖像存儲在openCV中
- 19. 如何獲取存儲在JavaScript對象內的圖像的寬度和高度?
- 20. 保存任何圖像到MySQL作爲Blob
- 21. 如何從HDFS獲取和查看Oracle BLOB(圖像)數據到本地系統?
- 22. 使用get_serving_url()在數據存儲中作爲blob屬性存儲的圖像
- 23. 設計問題 - 將圖像作爲對象存儲
- 24. 生成URL以將圖像存儲在數據存儲區Blob
- 25. 將圖像存儲到iphone
- 26. 將圖像保存爲blob或轉換爲實際圖像
- 27. 如何存儲頭像Buddypress的圖像
- 28. 如何從存儲在SD卡上的圖像獲取圖像路徑
- 29. 我如何從JSP到JavaScript獲取圖像blob?
- 30. Node.js存儲圖像。在前端獲取這些圖像