Click here to Skip to main content
15,881,938 members
Articles / Programming Languages / C#

Converting to TIFF

Rate me:
Please Sign up or sign in to vote.
2.23/5 (15 votes)
29 Sep 2007CPOL1 min read 42.9K   1.6K   14   1
This article will help you convert imgaes of different formats to TIFF format.

Screenshot - Gova_s_ImgConverter.jpg

Introduction

Conversion between different graphical image formats using C# and the .NET Framework is very common. This article focuses on the requirement of OCR and can be used as a pre-requirement component to build OCR.

Background

I started working on OCR and was collecting information, and I came across an important thing: it can read embedded text information from image files which can be of a different format, but it can perform and achieve the required level of accuracy when the images are of the TIFF image format. This article focuses on this point and converts images of different formats to TIFF format.

Using the Code

The article is simple and the code is very easy to use.

This article has two main functions:

  • Open images
  • Convert images to .Tiff format.

The images which need to be converted can be opened by pressing the "Open" button on the application.

Code for Open button:
C#
private void btnOpen_Click(object sender, EventArgs e)
{
    openFileDialog1.Title = "Open Image File";
    openFileDialog1.Filter = "Bitmap Files|*.bmp" +
        "|Enhanced Windows MetaFile|*.emf" +
        "|Exchangeable Image File|*.exif" +
        "|Gif Files|*.gif|Icons|*.ico|JPEG Files|*.jpg" +
        "|PNG Files|*.png|TIFF Files|*.tif|Windows MetaFile|*.wmf";
    openFileDialog1.DefaultExt = "bmp";
    openFileDialog1.FilterIndex = 1;
    openFileDialog1.FileName = "";
    openFileDialog1.ShowDialog();
    if (openFileDialog1.FileName == "")
        return;
    CurrentFile = openFileDialog1.FileName.ToString();
    img = Image.FromFile(openFileDialog1.FileName);
    pictureBox1.Image = img;
    textBox1.Text = CurrentFile;
}

Once the image which needs to be converted is opened, you can continue with the process of conversion by pressing the "SaveAs Tiff" button on the application. Here, the processing proceeds by checking the image format, and then by using the wide range of functionalities provided by .NET, the image is converted to TIFF format.

Code for SaveAs TIFF button:
C#
private void btnSave_Click(object sender, EventArgs e)
{
    string newName = System.IO.Path.GetFileNameWithoutExtension(CurrentFile);
    newName = newName + ".tif";
    if (System.IO.Path.GetExtension(CurrentFile).ToUpper().Contains(".TIF"))
    {
        MessageBox.Show("Image file saved to " + newName.ToString(), 
            "Image Saved", MessageBoxButtons.OK, MessageBoxIcon.Information);
        return;
    }
    try
    {
        img.Save(newName, ImageFormat.Tiff);
    }
    catch (Exception ee)
    {
        string error = ee.Message.ToString();
        MessageBox.Show("Failed to save image to TIFF format.", "Error", 
            MessageBoxButtons.OK, MessageBoxIcon.Error);
        return;
    }
    MessageBox.Show("Image file saved to " + newName.ToString(), 
        "Image Saved", MessageBoxButtons.OK, MessageBoxIcon.Information);
    textBox2.Text = System.IO.Path.GetFullPath(newName.ToString());
}

Once the image is converted, you can continue processing and then extract the embedded text from the image.

Points of Interest

Extracting the embedded text information from .tiff images increases the accuracy for OCRs.

Good luck guys!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer India
India India
This is Govardhana Reddy, i am here to explore this world of INTERNET. I feel this is one way through which i can explore this world of INTERNET.

I want to be one among the best in this profession (a Software Developer, not a Software Engineer its a bit Controversial.) if not the "BEST"

My definition of a Software Engineer : "A person who knows what to cut/copy and where to paste".

Apart from my technical stuff I love Long Drives, Computer Gaming, Sports, Bikes and much more to say.

Anyways long road ahead keep me accompanied...

Comments and Discussions

 
GeneralMy vote of 1 Pin
acosmin2-Sep-09 12:12
acosmin2-Sep-09 12:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.