Tallan's Technology Blog

Tallan's Top Technologists Share Their Thoughts on Today's Technology Challenges

Convert Multi-page image to Collection of Bitmaps

Craig Vallee

Recently on a project, we had the need to convert a saved multi-page tiff to a collection of bitmaps for viewing in a UI using standard GDI+ methods. These bitmaps also needed to be printed in a high quality way for submission to a government agency.  This method ensures that no quality will be lost from the creation of the new bitmap objects.

/////////////////////////////////////////
// by Craig Vallee
// Consultant
// Tallan, Inc.
/////////////////////////////////////////

private static List<Bitmap> ImageToBitmap()
{
    //Create and Image object from file path and name
    Image originalImage = Image.FromFile(@"C:\Temp\Your_File.tif");

    //Create a collection of Bitmap objects 
    List<Bitmap> bitmapList = new List<Bitmap>();

    //Place holders for setting resolution of new Bitmap objects
    var xResolution = originalImage.HorizontalResolution;
    var yResolution = originalImage.VerticalResolution;

    //Create FrameDimesion for iteration through file frames
    FrameDimension frameDimension = new FrameDimension(originalImage.FrameDimensionsList[0]);

    //Framecount of image for iteration through file frames
    int frameCount = originalImage.GetFrameCount(FrameDimension.Page);

    //Simple iteration through file frames
    for (int i = 0; i < frameCount; ++i)
    {
        //Create bitmap to hold individual frame
        Bitmap bmp;

        //Moves active frame pointer to next frame in iteration
        originalImage.SelectActiveFrame(frameDimension, i);

        //Cast image frame to Bitamp holder
        bmp = (Bitmap)originalImage;

        //Create new Bitmap from placeholder
        Bitmap temp = new Bitmap(bmp);

        //Set bitmap resolution based on original resolution
        temp.SetResolution(xResolution, yResolution);

        //Add Bitmap to Bitmap collection
        bitmapList.Add(temp);
    }
    return bitmapList;

}

-Craig

No comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>