我想检索存储在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存储图像。在前端获取这些图像