C#如何从存储在数据库中的OLE对象保存图像

时间:2011-07-14 15:24:37

标签: c# image ms-access

当头文件作为OLE对象嵌入到访问数据库中时,它似乎与图像一起存储,并且它们阻止我写入存储到光盘的字节,因为它引发了异常'GDI +中发生了一般错误。“

如何从存储在访问数据库中的OLE对象中仅提取图像字节,然后保存到磁盘?

photo = ((rs.Fields["photo"].Value == System.DBNull.Value) ? 
    null : (byte[])rs.Fields["photo"].Value)

...

if (photo != null)
{
    MemoryStream stream = new MemoryStream(photo);
    Image image = new Bitmap(stream);
    stream.Close();

    image.Save(@"C:\Temp\images\test", ImageFormat.Jpeg);
 }

1 个答案:

答案 0 :(得分:1)

using (MemoryStream stream = new MemoryStream(photo))
using (Image image = Image.FromStream(stream))
{
    image.Save(@"C:\Temp\images\test.jpg", ImageFormat.Jpeg);
}