【问题标题】:Edit multipage TIFF image using System.Drawing使用 System.Drawing 编辑多页 TIFF 图像
【发布时间】:2012-07-12 18:42:44
【问题描述】:

我试图通过从图像创建图形来编辑多页 tiff,但我遇到了错误消息:“无法从具有索引像素格式的图像创建图形对象。”

如何编辑多页 tiff?

【问题讨论】:

    标签: c# winforms tiff system.drawing


    【解决方案1】:

    我写了一些东西来从多页 tiff 文件中提取单页。

    // Load as Bitmap
    using (Bitmap bmp = new Bitmap(file))
    {
        // Get pages in bitmap
        int frames = bmp.GetFrameCount(System.Drawing.Imaging.FrameDimension.Page);
        bmp.SelectActiveFrame(System.Drawing.Imaging.FrameDimension.Page, tiffpage);
        if (bmp.PixelFormat != PixelFormat.Format1bppIndexed)
        {
            using (Bitmap bmp2 = new Bitmap(bmp.Width, bmp.Height))
            {
                bmp2.Palette = bmp.Palette;
                bmp2.SetResolution(bmp.HorizontalResolution, bmp.VerticalResolution);
                // create graphics object for new bitmap
                using (Graphics g = Graphics.FromImage(bmp2))
                {
                    // copy current page into new bitmap
                    g.DrawImageUnscaled(bmp, 0, 0);
    
                                    // do whatever you migth to do
                    ...
    
                }
            }
        }
    }
    

    sn-p 加载 tif 文件并将一页(变量 tiffpage 中的数字)提取到新的位图中。这没有索引,可以创建图形对象。

    【讨论】:

    • 需要注意的是,在SelectActiveFrame中,tiffpage = 0将是第1页。
    【解决方案2】:

    错误:无法从具有索引像素格式的图像创建图形对象。

    ...与它是多页 TIFF 无关。索引图像格式意味着它具有调色板,例如这是一张 256 色的图像。 1 位图像 (B&W) 也算作具有 2 种颜色的调色板。

    您不能对使用调色板的图像执行Graphics 操作,它们需要先转换为 15 位或更高的颜色深度。

    【讨论】:

      【解决方案3】:

      这里是 CodeProject 示例的链接,其中包含用于将 TIFF 文件转换为普通位图的代码,然后您可以像使用任何其他位图一样使用它:

      http://www.codeproject.com/KB/GDI-plus/BitonalImageConverter.aspx

      【讨论】:

        【解决方案4】:

        我曾经写过一个小工具来从 tiff 图像创建加密的 pdf。下面是一段从 tiff 图像中获取页面的代码:

        var bm= new System.Drawing.Bitmap('tif path');
        var total = bm.GetFrameCount(System.Drawing.Imaging.FrameDimension.Page);
        for(var x=0;x<total;x++)
        {
            bm.SelectActiveFrame(System.Drawing.Imaging.FrameDimension.Page,x);
            var img=Image.GetInstance(bm,null,false);
        
            //do what ever you want with img object
        }
        

        【讨论】:

        • 我在 Image 中找不到 GetInstance 方法?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-06-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-16
        • 1970-01-01
        相关资源
        最近更新 更多