我正在尝试检索存储在 oracle blob 中的图像并将其放置在新的 System.Drawing.Image 实例中。我知道我可以将流写入磁盘上的 temp.bmp 文件并从那里读取它,但这对我来说还不够 l33t。如何将 blob 对象直接转换为图像?
George Mauer
问问题
2830 次
2 回答
1
假设 :
- 您正在使用 Microsoft 客户端 (
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;
}
于 2009-09-18T10:04:20.727 回答
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,因此代码与后面的图像有点不同。
于 2008-10-08T19:57:29.250 回答