Editing a multi-page TIFF image using System.Drawing
Error: The graphic object could not be created from an indexed pixel format image.
... has nothing to do with the fact that it is a multi-page TIFF. An indexed image format means that it has a palette of colors, for example. it is a 256-color image. A 1-bit image (B&W) will also be considered to have a two-color palette.
You cannot perform operations Graphics
on images using the palette, they must first be converted to 15-bit or more color depth.
source to share
I wrote something to extract individual pages from a multipart 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 one page (the number in the tiffpage variable) into a new bitmap. It is not indexed and a graphic can be created.
source to share
Here is a link to a sample CodeProject that includes code to convert a TIFF file to a regular bitmap that you can work with like any other bitmap:
http://www.codeproject.com/KB/GDI-plus/BitonalImageConverter.aspx
source to share
I wrote a small utility to create encrypted PDF files from tiff images. Here is a code snippet to get pages from a 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
}
source to share