使用 System.Drawing 编辑多页 TIFF 图像

发布于 2024-08-03 01:08:13 字数 109 浏览 8 评论 0原文

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

如何编辑多页 tiff?

I'm tring to edit multipage tiff by creating Graphics from the image, but i encountered the error message: “A Graphics object cannot be created from an image that has an indexed pixel format.”

How can i edit multipage tiff?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

迷你仙 2024-08-10 01:08:13

我写了一些东西来从多页 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
                ...

            }
        }
    }
}

该代码片段加载 tif 文件并将一页(变量 tiffpage 中的编号)提取到新的位图中。它没有索引,并且可以创建图形对象。

I wrote something to extract single pages from a multipage tiff file.

// 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
                ...

            }
        }
    }
}

The snippet loads the tif file and extracts the one page (number in variable tiffpage) into a new bitmap. This is not indexed and an graphics object can be created.

埖埖迣鎅 2024-08-10 01:08:13

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

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

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

The error : A Graphics object cannot be created from an image that has an indexed pixel format.

...has nothing to do with it being a multipage TIFF. An indexed image format means it has a palette of colours, e.g. it's a 256-colour image. A 1-bit image (B&W) would also count as having a palette of 2 colours.

You can't perform Graphics operations on images that use a palette, they'd need to be converted to 15-bit or more colour depth first.

胡大本事 2024-08-10 01:08:13

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

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

Here is a link to a CodeProject sample that includes code for converting a TIFF file to a normal Bitmap, which you can then work with like any other Bitmap:

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

另类 2024-08-10 01:08:13

我曾经编写过一个小实用程序来从 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
}

I once wrote little utility to create encrypted pdfs from tiff images. Here is a piece of code to get pages from tiff image:

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文