views:

309

answers:

2

I have realized that my first question won't be answerable, since php can't deal with this format: http://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 chance 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&lt;/TraceIdentifier&gt;&lt;Description&gt;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)
+1  A: 

Have you tried the Metafileclass to load it?

http://msdn.microsoft.com/en-us/library/wb42xhfh.aspx

Brian ONeil
+1  A: 

You might find the code in Stephen Lebans' sample database illuminating:

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).

David-W-Fenton
+1 - for a useful tool. It shows me that there is something wrong with the data, as, out of 49 ole fields, 1 gets extracted, and then I get an error. So, the problem isn't with my approach, there is something that Access can do that I can't, it appears.
James Black