从 MemoryStream 加载位图时出错
我正在尝试从内存流加载图像(pdf 和 Word 文档),以便我可以在将它们添加到 pdf 之前对其进行操作。每当我尝试加载位图或 GIF 时,我都会遇到可怕的 GDI+ 错误之一。
此调用...
System.Drawing.Bitmap myImage = System.Drawing.Image.FromStream(docStream);
生成此错误...
System.Runtime.InteropServices.ExternalException occurred
Message="A generic error occurred in GDI+."
Source="System.Drawing"
ErrorCode=-2147467259
StackTrace:
at System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement, Boolean validateImageData)
at System.Drawing.Image.FromStream(Stream stream)
此调用...
System.Drawing.Bitmap myImage = new System.Drawing.Bitmap(docStream);
生成此错误...
System.ArgumentException occurred
Message="Parameter is not valid."
Source="System.Drawing"
StackTrace:
at System.Drawing.Bitmap..ctor(Stream stream)
该代码适用于 png、jpg 和 tif,但不适用于 bmp 或 GIF。
基本上是:
MemoryStream docStream = GetMemoryStream(filePath);
// The next line errors...
System.Drawing.Bitmap myImage = new System.Drawing.Bitmap(docStream);
private MemoryStream GetMemoryStream(string file)
{
FileStream fs = null;
Byte[] bytes;
int len;
fs = File.OpenRead(file);
len = (int)fs.Length - 1;
bytes = new Byte[len];
fs.Read(bytes, 0, len);
return new MemoryStream(bytes);
}
我不会在任何地方关闭 MemoryStream,除非 png 和 jpg 不受 关闭内存流 我不认为这是问题所在。当我使用十六进制编辑器查看图像文件时,似乎没有 编码不正确。我还尝试了不同的文件大小,以防万一。
在这一点上我几乎不知所措。任何帮助将不胜感激。
谢谢。
I'm trying to load images (pdfs and Word documents) from a memory stream so I can manipulate them before they get added to a pdf. Whenever I try and load a bitmap or a GIF I get one of the dreaded GDI+ errors.
This call...
System.Drawing.Bitmap myImage = System.Drawing.Image.FromStream(docStream);
Generates this error...
System.Runtime.InteropServices.ExternalException occurred
Message="A generic error occurred in GDI+."
Source="System.Drawing"
ErrorCode=-2147467259
StackTrace:
at System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement, Boolean validateImageData)
at System.Drawing.Image.FromStream(Stream stream)
and this call...
System.Drawing.Bitmap myImage = new System.Drawing.Bitmap(docStream);
Generates this error...
System.ArgumentException occurred
Message="Parameter is not valid."
Source="System.Drawing"
StackTrace:
at System.Drawing.Bitmap..ctor(Stream stream)
The code works for png, jpg and tif, but not for bmp or GIF.
It's basically:
MemoryStream docStream = GetMemoryStream(filePath);
// The next line errors...
System.Drawing.Bitmap myImage = new System.Drawing.Bitmap(docStream);
private MemoryStream GetMemoryStream(string file)
{
FileStream fs = null;
Byte[] bytes;
int len;
fs = File.OpenRead(file);
len = (int)fs.Length - 1;
bytes = new Byte[len];
fs.Read(bytes, 0, len);
return new MemoryStream(bytes);
}
I'm not closing the MemoryStream anywhere and unless the png and jpg aren't affected by a closed MemoryStream I don't think that's the issue. When I look at the image files using a hex editor it doesn't appear that the encoding is incorrect. I've also tried different file sizes, just in case.
At this point I'm pretty much at a loss. Any help would be greatly appreciated.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不知道为什么您认为这是一个好主意,用于在 VB 中编程?删除 -1,您想要读取所有字节,而不是所有字节减去最后一个字节。 File.ReadAllBytes() 将是一个不错的选择。
Not sure why you thought that was a good idea, used to program in VB? Drop the -1, you want to read all the bytes, not all the bytes minus the last one. File.ReadAllBytes() would be a good choice.
不能保证 Read 会填满缓冲区;只是它必须返回一些数据。更常见的方法可能是:
There is no guarantee that Read fills the buffer; simply itmust return some data. A more common approach might be: