转换为 TIFF






2.23/5 (15投票s)
本文将帮助您将不同格式的图像转换为TIFF格式。
引言
使用C#和.NET Framework进行不同图形图像格式之间的转换非常常见。本文重点关注OCR的需求,可以用作构建OCR的先决条件组件。
背景
我开始研究OCR并收集信息,发现一个重要的事情:它可以从不同格式的图像文件中读取嵌入的文本信息,但当图像为TIFF图像格式时,才能实现所需的准确度。本文重点关注这一点,将不同格式的图像转换为TIFF格式。
Using the Code
本文简单易懂,代码非常易于使用。
本文有两个主要功能
- 打开图像
- 将图像转换为.Tiff格式。
可以通过按下应用程序上的“打开”按钮来打开需要转换的图像。
“打开”按钮的代码
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;
}
一旦打开需要转换的图像,就可以通过按下应用程序上的“另存为Tiff”按钮继续转换过程。在这里,处理过程通过检查图像格式进行,然后通过使用.NET提供的广泛功能,将图像转换为TIFF格式。
“另存为TIFF”按钮的代码
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());
}
图像转换完成后,您可以继续处理并从图像中提取嵌入的文本。
关注点
从.tiff图像中提取嵌入的文本信息可以提高OCR的准确性。
祝你好运!