I need to loop thru selected files, extract the embedded Visio object,
and save it out to a JPG.
This would be easy enough through automation if I could get that Visio
object opened in a Visio.ApplicationClass. But, I can't due to the
fact that it's embedded in the database. Does anyone have any ideas? I
am able to read it into my DataTable as a byte array, but when I tried
to use a FileStream and open the file, Visio did not like the
resultant file.
I'm using Visio 2010 (14.0 assembly).
My code thus far:
OleDbConnection conn = new
OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Y:
\myfile.mdb;Persist Security Info=False;");
OleDbCommand cmd = new OleDbCommand("SELECT OLEFile FROM MyTable",
conn);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataTable dt = new System.Data.DataTable();
visioByteArray = (byte[])dt.Rows[0]["OLEFile"];
FileStream fs = new FileStream(Environment.CurrentDirectory +
@"\abc.vsd", FileMode.OpenOrCreate, FileAccess.ReadWrite);
fs.Write(visioByteArray, 0, visioByteArray.);
fs.Close();
// This does produce a file but Visio does not recognize it.
I figured it out. For anyone that's curious...
When an object is embedded in Access as an OLE Object, Access adds an
OLE header to the file.
I put this stream out to a text file and compared it to a legit .VSD
file. I found the stuff that seemed like the header, figured out the
offset, and then replaced one of the lines above with:
int offset = 84; // For me, that was the offset
fs.Write(visioByteArray, offset, visioByteArray.Length - offset);