Read WMF file from db, convert to bitmapped image
I have realized that my first question won't be answerable, since php can't deal with this format: https://stackoverflow.com/questions/1376103/how-to-convert-metafilepict-to-bitmapped-image-in-php, so I have a C# console application that will just load up the images, from either an MS SQL 2008 db, or Access 2007, and then save them to a directory.
The images were saved as Windows Media File (WMF) to the access database, originally.
So, I have the following code, which has an error on the last line:
byte[] mybytes = (byte[])sdr["LocationMap"];
System.IO.MemoryStream ms =
new System.IO.MemoryStream(mybytes);
System.Drawing.Bitmap b =
(System.Drawing.Bitmap)System.Drawing.Image.FromStream(ms);
The column "LocationMap" in sql server is varbinary, and in Access is image.
How can I load this from one of the two databases, so I can save it as a bitmapped image?
This was my error:
A first cha开发者_开发知识库nce exception of type 'System.ArgumentException' occurred in System.Drawing.dll
System.Transactions Critical: 0 : <TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Critical"><TraceIdentifier>http://msdn.microsoft.com/TraceCodes/System/ActivityTracing/2004/07/Reliability/Exception/Unhandled</TraceIdentifier><Description>Unhandled exception</Description><AppDomain>ConvertWMFToFile.vshost.exe</AppDomain><Exception><ExceptionType>System.ArgumentException, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType><Message>Parameter is not valid.</Message><StackTrace> at System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement, Boolean validateImageData)
at System.Drawing.Image.FromStream(Stream stream)
UPDATE: This was my other failed attempt:
//imageBytes = sdr.GetSqlBytes(2);
//var stream = imageBytes.Stream;
//stream.Seek(0, SeekOrigin.Begin);
//locmod.LocationImage = new Metafile(stream);
Update2: I just tried this and got a generic GDI+ error:
byte[] mybytes = (byte[])sdr["LocationMap"];
var f = File.Create("tempfile.wmf");
f.Write(mybytes, 0, locmod.LocationImageBytes.Length);
f.Close();
var myimage = new Metafile("tempfile.wmf");
Error:
System.Runtime.InteropServices.ExternalException was unhandled
Message="A generic error occurred in GDI+."
Source="System.Drawing"
ErrorCode=-2147467259
StackTrace:
at System.Drawing.Imaging.Metafile..ctor(String filename)
You might find the code in Stephen Lebans' sample database illuminating:
- Write data in OLE fields to disk
That's not specific to WMF -- it works for any OLE field. I used it 4 months ago to extract a bunch of Word docs from a client database and it worked fine (though a few of the fields that were populated with older versions of Word could not be extracted).
Have you tried the Metafile
class to load it?
http://msdn.microsoft.com/en-us/library/wb42xhfh.aspx
精彩评论