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:
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:
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!