我有一个应用程序,该应用程序允许用户定义图像的区域并将该区域保存为归档。我已经违背了我无法分类的障碍。我创建的位图将所选区域绘制到“ MemoryBMP”的映像上被卡住,看来这是为任何非文件加载的位图设置的ImageFormat。问题是我的位图是在内存中创建的,我想将其保存为CCITT4 BITONAL TIFF,但我遇到了“ GDI+中发生的通用错误”。我非常相信这是由于image.rawformat属性是可怕的内存BMP所致。

image.save()的过载具有一个imageFormat参数,当我使用该传递imageFormat.Tiff时,它可以保存正常,但是我没有机会指定我的编码器参数。

我能想到的唯一可能的解决方法是使用image.save(image,imageFormat)保存到磁盘,然后重新加载它(RawFormat现在将正确设置为ImageFormat.tif),然后再次保存通过编码器设置。那只是愚蠢的,一定有更好的方法。

这是一个剪切的代码(这只是测试内容),应该让您了解我在做什么,以防我之前的描述不够清楚:

SizeF dpiScale = GetScreenToImageDPIRatio(loadedImage);
using (Bitmap image = new Bitmap(loadedImage,
    (int)(_cropBox.Width * dpiScale.Width),
    (int)(_cropBox.Height * dpiScale.Height)))
{
    image.SetResolution(loadedImage.HorizontalResolution, 
        loadedImage.VerticalResolution);

    using (Graphics g = Graphics.FromImage(image))
    {
        g.DrawImage(loadedImage, 0, 0, new Rectangle(
            (int)(_cropBox.Location.X * dpiScale.Width),
            (int)(_cropBox.Location.Y * dpiScale.Height),
            (int)(_cropBox.Width * dpiScale.Width),
            (int)(_cropBox.Height * dpiScale.Height)),
            GraphicsUnit.Pixel);
    }

    //  It's stuck as a MemoryBmp so none of these checks will work
    if (true || image.RawFormat.Equals(ImageFormat.Tiff))
    {
        ImageCodecInfo tiffCodecInfo = ImageUtils.GetEncoderInfo("image/tiff");
        EncoderParameters myEncoderParameters = new EncoderParameters(2);

        myEncoderParameters.Param[0] = new
            EncoderParameter(System.Drawing.Imaging.Encoder.Compression,
                (long)EncoderValue.CompressionCCITT4);
        myEncoderParameters.Param[1] = new
            EncoderParameter(System.Drawing.Imaging.Encoder.ColorDepth, 1L);

        image.Save(Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".tif"),
            tiffCodecInfo, myEncoderParameters);

        //  The file is a "MemoryBmp" and it's screwing things up
        //image.Save(Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".tif"),
        //    ImageFormat.Tiff);
    }
    else
    {
        //  other format saving support, blah blah blah
    }
}

哦,我应该提到“加载”确实是一个TIFF图像,我将对LoadEdiMage的引用传递给了bitmap.ctor(),以查看是否会向其提示,但没有区别。

有帮助吗?

解决方案

发现CCITT4确实需要双色调图像,HA!所以我离开了谷歌搜索,遇到了一个 Codeproject文章 并在源头上偷偷摸摸。作者提供了一种相当不错的方法,可以将32BPPARGB转换为1BPP双层。我尝试了一下,它很快(一页大约34毫秒的测试显示),它可以解决问题!

我正在回答自己的问题。我希望这至少可以帮助遇到这个问题的其他人。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top