System.Drawing.Image.Save抛出ExternalException:GDI中发生一般错误 [英] System.Drawing.Image.Save throws ExternalException: A generic error occurred in GDI

查看:330
本文介绍了System.Drawing.Image.Save抛出ExternalException:GDI中发生一般错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有函数,它接受一个位图,它的副本组成部分,并将其保存为8bpp TIFF。结果图像的文件名是唯一的,文件不存在,程序具有写权限到目标文件夹。

I have function which takes a bitmap, copies part of it and saves it as 8bpp tiff. Filename of the result image is unique and file doesn't exist, program has permission to write to the target folder.

void CropImage(Bitmap map) {
        Bitmap croped = new Bitmap(200, 50);

        using (Graphics g = Graphics.FromImage(croped)) {
            g.DrawImage(map, new Rectangle(0, 0, 200, 50), ...);
        }

        var encoderParams = new EncoderParameters(2);
        encoderParams.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.ColorDepth, 8L);
        encoderParams.Param[1] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)EncoderValue.CompressionNone);

        croped.Save(filename, tiffEncoder, encoderParams);
        croped.Dispose();
    }

奇怪的是,这funcion效果很好,在某些计算机(Win 7的)并抛出System.Runtime.InteropServices.ExternalException:在其他计算机上(主要是Win XP的)GDI异常发生了一般性错误

Weird thing is that this funcion works well on some computers (Win 7) and throws System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI exception on other computers (mostly Win XP).

所有计算机都安装了.NET 3.5 SP1运行。

All computers have the .NET 3.5 SP1 runtime installed.

如果我用 croped.Save(文件名,ImageFormat.Tiff); ,而不是 croped.Save(文件名,蒂芬codeR,EN coderParams); 比它适用于所有的计算机,但我需要保存的TIFF在8bpp格式

If I use croped.Save(filename, ImageFormat.Tiff); instead croped.Save(filename, tiffEncoder, encoderParams); than it works on all computers, but I need to save Tiff in 8bpp format.

你有任何想法,问题出在哪里可能?

Do you have any ideas, where the problem could be?

谢谢,卢卡斯

推荐答案

GDI是windows操作系统的功能。我在与16位TIFF文件时遇到了类似的问题,并使出了不同的库。请参阅<一href="http://stackoverflow.com/questions/2041783/using-libtiff-from-c-to-access-tiled-tiff-images">using的libtiff从C#

GDI is a windows operating system functionality. I encountered similar issues when dealing with 16-bit TIFF files, and resorted to a different library. See using LibTIFF from c#

MSDN帮助提示功能都可用,但在随后的Windows抛出一般错误的异常,当您试图将一个位图复制到一个新的位图,或将其保存到一个文件或流。事实上,相同的功能效果很好在Windows7(这似乎有很好的TIFF支持)。请参见新的WIC functioanity在Windows 7

The MSDN help suggest that the functionality is available, but at then Windows throws 'generic error' exceptions when you try to copy a bitmap into a new bitmap, or save it to a file or stream. Indeed the same functionality works well on Windows7 (which seems to have good TIFF support). See New WIC functioanity in Windows 7.

我已经使用了其他的解决办法是让一个不安全副本8位。这样我才得以保存PNG文件(面板)。我没有尝试过本作TIFF。

The other solution I have used is to make a unsafe copy in 8-bit. This way I was able to save PNG files (with palette). I have not tried this for TIFF.

       // part of a function taking a proprietary TIFF tile structure as input and saving it into the desired bitmap format
      // tile.buf is a byterarray, containing 16-bit samples from TIFF.

        bmp = new Bitmap(_tile_width, _tile_height, PixelFormat.Format8bppIndexed);
        System.Drawing.Rectangle rect = new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height);
        BitmapData bmpData =bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,bmp.PixelFormat);
        int bytes = bmpData.Stride * bmp.Height;

        dstBitsPalette = (byte *)bmpData.Scan0;

        offset=0;


        for (offset = 0; offset < _tile_size; offset += 2)
        {
             dstBitsPalette[offset >> 1] = tile.buf[offset + 1];
        }

        // setup grayscale palette
        ColorPalette palette = bmp.Palette;
        for (int i = 0; i < 256; i++)
        {
            Color c = Color.FromArgb(i, i, i);
            palette.Entries[i] = c;
        }
        bmp.Palette = palette;
        bmp.UnlockBits(bmpData);

        return bmp;

    }

这篇关于System.Drawing.Image.Save抛出ExternalException:GDI中发生一般错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆