问题
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