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

Tam*_*mir 2 c# system.drawing tiff winforms

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

我如何编辑多页tiff?

Chr*_*467 6

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

            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

片段加载tif文件并将一页(变量tiffpage中的数字)提取到新的位图中.这未编入索引,可以创建图形对象.