C# How to save image from an OLE Object stored in a Database

半城伤御伤魂 提交于 2019-12-24 10:52:59

问题


It seems that header bytes are stored along with an image when it is embedded into an access database as a OLE Object and they are preventing me from writing the bytes stored to disc as it raises an exception 'A generic error occurred in GDI+.'

How do you extract only the image bytes from a OLE Object stored in an access database and then save to disk?

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:


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


来源:https://stackoverflow.com/questions/6695729/c-sharp-how-to-save-image-from-an-ole-object-stored-in-a-database

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!