我正在使用SqlDataReader,并且正在尝试从从Access文件转换而来的表中读取数据,该表位于SQL Server2008中。
数据类型现在是varbinary(max)。
我如何转换它,这样我就可以拥有它作为System.Drawing.Image?
这是一个Windows,我想把它转换成MetaFile或者GIF并保存到文件系统中。
这似乎是不正确的,因为我得到一个错误,它不能转换为图像。
System.Drawing.Image LocationImage = sdr.GetSqlBinary(2)谢谢。
发布于 2009-10-06 03:59:21
使用SqlBytes。使用Image.FromStream从SqlBytes.Stream加载图像。使用阅读器上的CommandBehavior.SequentialAccess加载大尺寸图像:
using (SqlDataReader sdr=cmd.ExecuteReader(CommandBehavior.SequentialAccess))
{
...
System.Data.SqlTypes.SqlBytes imageBytes = srd.GetSqBytes(2);
System.Drawing.Image locationImage = Image.FromStream(imageBytes.Stream);
}要将其另存为PNG/GIF:
SqlCommand cmd = new SqlCommand("update table set image=@image where ...")
MemoryStream streamOutput = new MemoryStream();
image.Save(streamOutput, ImageFormat.Png);
SqlBytes imageBytes = new SqlBytes(streamOutput);
cmd.Parameters.AddWithValue("@image", imageBytes);
cmd.ExecuteNonQuery()更新
我不确定Image.FromStream是否可以加载WMF格式。它可以加载EMF afaik,但我不确定WMF。也许通过Metafile(Stream)构造函数路由调用会起作用,但我绝对不是图形专家。
https://stackoverflow.com/questions/1523504
复制相似问题