locked
Accessing Frames in MultiFrame TIFF RRS feed

  • Question

  • User1122355199 posted
    Hello everyone and thanks for your help in advance. I am developing an application that needs to access multi-frame (page) tiff (i.e. a fax). I have figured out how to open the first frame, but can't seem to figure out how to iterate through the various frames in the image. Could some please give me an example or point to soewhere I can get started? Any help is greatly appreciated. Thanks.
    Tuesday, March 1, 2005 12:25 PM

All replies

  • User-505414299 posted
    Hi, I think this is what your looking for??? I recently developed an application which basically 1. takes a multipage tiff image, 2. splits it up into a frame array 3. saves each frame to disk as a single tiff image 4. allows you to navigate through the frames 5. select a frame or multiple frames 6. if more than one frame was selected it creates a new multiframe tif image 7. indexes the selected tif image or newly created multiframe tiff image against customer applications in a database. This was all developed with help from BOB POWELLS website. have a look here http://www.bobpowell.net/gdiplus_faq.htm I wrote this code this morning (on my companies time :o) ) simply create a windows forms project and put this into it.
    
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Diagnostics ;
    using System.Text.RegularExpressions ;
    using System.Data ;
    using System.Data.SqlClient ;
    using System.Drawing.Imaging ;
    using System.Drawing.Printing ;
    using System.IO ;
    using System.Configuration;
    
    namespace MultiframeTiffOpener
    {
    	/// <summary>
    	/// Summary description for Form1.
    	/// </summary>
    	public class Form1 : System.Windows.Forms.Form
    	{
    		private System.Windows.Forms.PictureBox pictureBox1;
    		/// <summary>
    		/// Required designer variable.
    		/// </summary>
    		private System.ComponentModel.Container components = null;
    
    		public Form1()
    		{
    			//
    			// Required for Windows Form Designer support
    			//
    			InitializeComponent();
    
    			//
    			// TODO: Add any constructor code after InitializeComponent call
    			//
    		}
    
    		/// <summary>
    		/// Clean up any resources being used.
    		/// </summary>
    		protected override void Dispose( bool disposing )
    		{
    			if( disposing )
    			{
    				if (components != null) 
    				{
    					components.Dispose();
    				}
    			}
    			base.Dispose( disposing );
    		}
    
    		#region Windows Form Designer generated code
    		/// <summary>
    		/// Required method for Designer support - do not modify
    		/// the contents of this method with the code editor.
    		/// </summary>
    		private void InitializeComponent()
    		{
    			this.pictureBox1 = new System.Windows.Forms.PictureBox();
    			this.SuspendLayout();
    			// 
    			// pictureBox1
    			// 
    			this.pictureBox1.Location = new System.Drawing.Point(16, 8);
    			this.pictureBox1.Name = "pictureBox1";
    			this.pictureBox1.Size = new System.Drawing.Size(536, 408);
    			this.pictureBox1.TabIndex = 0;
    			this.pictureBox1.TabStop = false;
    			// 
    			// Form1
    			// 
    			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    			this.ClientSize = new System.Drawing.Size(840, 669);
    			this.Controls.Add(this.pictureBox1);
    			this.Name = "Form1";
    			this.Text = "Form1";
    			this.Load += new System.EventHandler(this.Form1_Load);
    			this.ResumeLayout(false);
    
    		}
    		#endregion
    
    		/// <summary>
    		/// The main entry point for the application.
    		/// </summary>
    		[STAThread]
    		static void Main() 
    		{
    			Application.Run(new Form1());
    		}
    
    		private void Form1_Load(object sender, System.EventArgs e)
    		{
    			string multiTiffFilename = "C:\\myImage.tif" ;
    			OpenMultiTiffImage(multiTiffFilename) ;
    		}
    		private void OpenMultiTiffImage(string tiffLocation)
    		{
    			Image		multiTifImage ;		// image object to store original multiframe tiff image
    			Image[]		MyImages ;			// image array to store the individual tiff frames
    			int			frameCount = 0 ;	// number of frames in image
    			int			loopCounter = 0 ;	// loop counter for looping thru frames
    			int			imageSelectedOK ;
    			string		DymamicfileName ;	// holds each frames filename to allow it to be saved to disk
    
    
    			// SAVE THE TIFF AS AN IMAGE OBJECT
    			multiTifImage = Image.FromFile(tiffLocation, true) ;
    					
    			// CREATE A GUID for a handle to the image
    			System.Guid[] guid; 
    			guid=multiTifImage.FrameDimensionsList; 
    					
    			// Get and store the frame dimensions of the image in 
    			// a "FrameDimension" object
    			FrameDimension multiTifDimension ;
    			multiTifDimension = new FrameDimension(multiTifImage.FrameDimensionsList[0]) ;
    					
    			// Get the number of frames (pages) in the image
    			frameCount = multiTifImage.GetFrameCount(multiTifDimension ); 
    
    			// initialise the image array to the sie of the number of frames in the
    			// original image
    			MyImages = new Image[frameCount] ;
    
    			for(loopCounter = 0; loopCounter  < frameCount; loopCounter++)
    			{
    				// use arrayindex because the image numbers start at one but array elements are numbered from 0 up
    				int Arrayindex ;
    				Arrayindex = loopCounter ;
    					
    				// CREATE AN IMAGE ENCODER
    				Encoder enc=Encoder.SaveFlag;
    
    				// Obtain the TIFF codec info. 
    				ImageCodecInfo info = GetCodec("image/tiff"); 
    					
    				// Create an encoder parameter list. This needs 1 parameter in it. 
    				EncoderParameters ep=new EncoderParameters(1);
    
    				// Place the MultiFrame encoder value in the parameter list 
    				ep.Param[0]=new EncoderParameter(enc,(long)EncoderValue.MultiFrame);
    
    				// Select a frame from the multiTifImage and load it into the image array
    				imageSelectedOK = multiTifImage.SelectActiveFrame(multiTifDimension,Arrayindex); 
    				
    				MyImages[Arrayindex] = (Image)multiTifImage ;
    
    				//Create checklistbox item names and filenames
    				string extractedImagePath = "Image" + loopCounter.ToString() +".tif" ;
    				DymamicfileName = "C:\\temp\\" + extractedImagePath ;
    					
    				// Save the each picture using the encoder and parameters 
    				MyImages[Arrayindex].Save(DymamicfileName,info,ep) ;
    
    				// Change the encoder value in the list to Flush 
    				ep.Param[0]=new EncoderParameter(enc,(long)EncoderValue.Flush);
    
    				// Use the second of the master frames overloaded SaveAdd methods to flush, save and close the image. 
    				MyImages[Arrayindex].Save(DymamicfileName,info,ep);
    					
    			}
    
    
    		}
    	
    		//========================================================
    		// Gets the ImageCodecInfo for the mime type named (in this case Tiff) 
    		//========================================================
    		public static ImageCodecInfo GetCodec( string mimeType ) 
    		{ 
    			foreach( ImageCodecInfo ice in ImageCodecInfo.GetImageEncoders() ) 
    				if( ice.MimeType.Equals( mimeType ) ) 
    					return ice; 
    
    			throw new Exception( mimeType + " mime type not found in ImageCodecInfo" ); 
    		} 
    	}
    
    
    }
    
    
    
    hope i was of help? any questions, fire away!
    Friday, March 4, 2005 5:37 AM