从数据库中读取WMF文件,转换为位图图像(Read WMF file from db, convert to bitmapped image)

编程入门 行业动态 更新时间:2024-10-27 21:20:06
从数据库中读取WMF文件,转换为位图图像(Read WMF file from db, convert to bitmapped image)

我已经意识到我的第一个问题不会被回答,因为PHP无法处理这种格式: https : //stackoverflow.com/questions/1376103/how-to-convert-metafilepict-to-bitmapped-image-in -php ,所以我有一个C#控制台应用程序,它将从MS SQL 2008 db或Access 2007中加载图像,然后将它们保存到目录中。

最初,这些图像被作为Windows媒体文件(WMF)保存到访问数据库中。

所以,我有以下代码,在最后一行有错误:

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

sql server中的“LocationMap”列是varbinary,Access中是image。

如何从两个数据库中的一个数据库加载它,以便将它保存为位图图像?

这是我的错误:

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

更新:这是我另一次失败的尝试:

//imageBytes = sdr.GetSqlBytes(2); //var stream = imageBytes.Stream; //stream.Seek(0, SeekOrigin.Begin); //locmod.LocationImage = new Metafile(stream);

Update2:我刚试过这个,得到了一个通用的GDI +错误:

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");

错误:

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)

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

最满意答案

您可能会在Stephen Lebans的示例数据库中找到代码:

将OLE字段中的数据写入磁盘

这不是特定于WMF的 - 它适用于任何OLE字段。 我在4个月前使用它从客户端数据库中提取了一堆Word文档,并且它工作正常(尽管只有少数填充了旧版Word的字段无法提取)。

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

更多推荐

本文发布于:2023-07-30 13:20:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1337896.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:位图   转换为   数据库中   图像   文件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!