[C#]具有多圖的TIFF圖檔拆解

  • 14794
  • 0
  • 2010-08-02

把具有多圖的TIFF圖檔拆解成單一圖檔後,顯示於程式中

這是在藍色小舖遇到的問題,當TIFF圖檔裡面,包含了三個圖檔時,要怎麼去讀取三個圖檔出來,經過在網路上搜尋後,在 Code Project找到相關Class,此Class可以做TIFF檔的拆解與將多圖檔合併成單一TIF檔

A simple TIFF management class

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

TiffManager

public void ConvertTiffFormat(string strNewImageFileName,  
                              EncoderValue compressEncoder);
public Image GetSpecificPage(int pageNumber);
public void JoinTiffImages(string[] imageFiles, string outFile,  
                           EncoderValue compressEncoder);
public void RemoveAPage(int pageNumber, EncoderValue compressEncoder,  
                        string strFileName);
public ArrayList SplitTiffImage(string outPutDirectory, EncoderValue format);

在這邊用到了SplitTiffImage,傳入(拆解後的圖檔資料夾位置, 影像編碼) ,回傳 (拆解後的圖檔名稱陣列)

以下為程式碼,圖檔放置的位置於\Fax中,而拆解完的圖檔,放置的位置我設定在C:\temp,在程式關閉時會將temp資料夾做刪除

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;


namespace WindowsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            string targetPath = @"C:\temp";
            if (!System.IO.Directory.Exists(targetPath))
            {
                System.IO.Directory.CreateDirectory(targetPath);
            }

            else
            {
                System.IO.Directory.Delete(targetPath, true);
                System.IO.Directory.CreateDirectory(targetPath);
            }


            this.InitializePictureBox();
            this.PopulateListView();    
        }


        private void InitializePictureBox()
        {
            this.pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
            this.pictureBox1.BorderStyle = BorderStyle.Fixed3D;
        }


        private void PopulateListView()
        {
            listView1.View = View.Details;
            listView1.FullRowSelect = true;
            ColumnHeader header1, header2;
            header1 = new ColumnHeader();
            header2 = new ColumnHeader();
            header1.Text = "FileName";
            header1.TextAlign = HorizontalAlignment.Left;
            header1.Width = 50;
            header2.Text = "SIZE";
            header2.TextAlign = HorizontalAlignment.Left;
            header2.Width = 85;
            listView1.Columns.Add(header1);
            listView1.Columns.Add(header2);

            System.IO.DirectoryInfo dirInfo = new System.IO.DirectoryInfo(Environment.CurrentDirectory + @"\fax");
            System.IO.FileInfo[] files = dirInfo.GetFiles("*.*");

            if (files != null)
            {
                foreach (System.IO.FileInfo file in files)
                {
                    ListViewItem item = new ListViewItem(file.Name);
                    item.SubItems.Add(file.FullName);
                    listView1.Items.Add(item);
                }

            }

        }


        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.pictureBox1.Image != null)
            {
                this.pictureBox1.Image.Dispose();
                this.pictureBox1.Image = null;
            }

            if (listView1.SelectedItems.Count>0)
            {
                ListViewItem selection = listView1.SelectedItems[0];
                ProcessTiff(selection.SubItems[1].Text);
            }

        }


        public void ProcessTiff(string filename)
        {
            if (this.pictureBox1.Image != null)
            {
                this.pictureBox1.Image.Dispose();
                this.pictureBox1.Image = null;
            }

            comboBox1.Items.Clear();
            string targetPath = @"C:\temp";
            System.IO.Directory.Delete(targetPath, true);
            System.IO.Directory.CreateDirectory(targetPath);

            AMA.Util.TiffManager tim = new AMA.Util.TiffManager(filename);
            ArrayList a = new ArrayList();
            a = tim.SplitTiffImage(targetPath, System.Drawing.Imaging.EncoderValue.CompressionLZW);
            int al = a.Count;
            
            for (int i = 0; i < al; i++)
            {
                this.comboBox1.Items.Add(a[i].ToString());
            }

            comboBox1.SelectedIndex = 0;
        }


        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.pictureBox1.Image != null)
            {
                this.pictureBox1.Image.Dispose();
                this.pictureBox1.Image = null;
            }


            if (this.comboBox1.SelectedItem != null)
            {
                pictureBox1.Image = System.Drawing.Image.FromFile(this.comboBox1.SelectedItem.ToString());
            }

        }



        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.pictureBox1.Image != null)
            {
                this.pictureBox1.Image.Dispose();
                this.pictureBox1.Image = null;
            }

            string targetPath = @"C:\temp";
            if (System.IO.Directory.Exists(targetPath))
            {
                System.IO.Directory.Delete(targetPath, true);
            }

        }

        
    }

}

執行結果

參考

CodeProject - Multipage TIF Viewer

CodeProject - A simple TIFF management class

藍色小舖 - 一個關於 tif 和 listview 的問題