从 MemoryStream 加载位图时出错

发布于 2024-09-27 04:37:35 字数 1840 浏览 1 评论 0原文

我正在尝试从内存流加载图像(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

妥活 2024-10-04 04:37:35
    len = (int)fs.Length - 1;

不知道为什么您认为这是一个好主意,用于在 VB 中编程?删除 -1,您想要读取所有字节,而不是所有字节减去最后一个字节。 File.ReadAllBytes() 将是一个不错的选择。

    len = (int)fs.Length - 1;

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.

浮世清欢 2024-10-04 04:37:35

不能保证 Read 会填满缓冲区;只是它必须返回一些数据。更常见的方法可能是:

byte[] buffer = new byte[2048];
int read;
while((read = source.Read(buffer, 0, buffer.Length)) > 0) {
    memStream.Write(buffer, 0, read);
}
memStream.Position = 0; //rewind to read it

There is no guarantee that Read fills the buffer; simply itmust return some data. A more common approach might be:

byte[] buffer = new byte[2048];
int read;
while((read = source.Read(buffer, 0, buffer.Length)) > 0) {
    memStream.Write(buffer, 0, read);
}
memStream.Position = 0; //rewind to read it
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文