C# 将 tiff 转换为 jpg 格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11668945/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
convert tiff to jpg format
提问by ozman
I have a tiff image with two pages. When I convert the file to jpg format I lost second pages. Is there any way to put two images on tiff file into one jpg file. Because of tiff files are too big I have to decrease their sizes. Is there any way to decrease tiff files size programmatically? It could also be a solution to my problem.
我有一个包含两页的 tiff 图像。当我将文件转换为 jpg 格式时,我丢失了第二页。有什么方法可以将 tiff 文件中的两个图像放入一个 jpg 文件中。由于 tiff 文件太大,我必须减小它们的大小。有没有办法以编程方式减小 tiff 文件的大小?这也可能是我的问题的解决方案。
采纳答案by Stephan Bauer
Since a TIFF can contain multiple frames but JPG can't, you need to convert each single frame into a JPG.
由于 TIFF 可以包含多个帧而 JPG 不能,因此您需要将每个单个帧转换为 JPG。
Taken from Windows Dev Center Samples:
public static string[] ConvertTiffToJpeg(string fileName)
{
using (Image imageFile = Image.FromFile(fileName))
{
FrameDimension frameDimensions = new FrameDimension(
imageFile.FrameDimensionsList[0]);
// Gets the number of pages from the tiff image (if multipage)
int frameNum = imageFile.GetFrameCount(frameDimensions);
string[] jpegPaths = new string[frameNum];
for (int frame = 0; frame < frameNum; frame++)
{
// Selects one frame at a time and save as jpeg.
imageFile.SelectActiveFrame(frameDimensions, frame);
using (Bitmap bmp = new Bitmap(imageFile))
{
jpegPaths[frame] = String.Format("{0}\{1}{2}.jpg",
Path.GetDirectoryName(fileName),
Path.GetFileNameWithoutExtension(fileName),
frame);
bmp.Save(jpegPaths[frame], ImageFormat.Jpeg);
}
}
return jpegPaths;
}
}
回答by Roba.M
We faced some problems when converting TIF files to JPEG, because TIF format supports some types of compressions that are not supported in free toolkits. I searched the internet and tried some commercial toolkits, but most of them are hard to implement with many limitations. The toolkit that drew my attention is leadtools, because it supports loading and saving many file formats (including TIF images with different compressions). We used this toolkit convert our images to JPEG format. You can find more information in the following page: http://support.leadtools.com/CS/forums/8925/ShowPost.aspx
我们在将 TIF 文件转换为 JPEG 时遇到了一些问题,因为 TIF 格式支持一些免费工具包中不支持的压缩类型。我在互联网上搜索并尝试了一些商业工具包,但其中大多数都很难实现,并且有很多限制。引起我注意的工具包是leadtools,因为它支持加载和保存多种文件格式(包括具有不同压缩率的TIF 图像)。我们使用这个工具包将我们的图像转换为 JPEG 格式。您可以在以下页面中找到更多信息:http: //support.leadtools.com/CS/forums/8925/ShowPost.aspx
Note that you can convert any VB.Net code to C# by using this free code converter: http://www.developerfusion.com/tools/convert/vb-to-csharp/
请注意,您可以使用此免费代码转换器将任何 VB.Net 代码转换为 C#:http: //www.developerfusion.com/tools/convert/vb-to-csharp/
回答by ketan italiya
using System.Drawing;
using System.Drawing.Imaging;
Bitmap bm=Bitmap.FromFile("photo.tif");
bm.Save("photo.jpg",ImageFormat.Jpeg);
回答by Javad Razzaghikomrudi
public static class ConvertTiffToJpeg
{
static string base64String = null;
public static string ImageToBase64(string tifpath)
{
string path = tifpath;
using (System.Drawing.Image image = System.Drawing.Image.FromFile(path))
{
using (MemoryStream m = new MemoryStream())
{
image.Save(m, ImageFormat.Jpeg);
byte[] imageBytes = m.ToArray();
base64String = Convert.ToBase64String(imageBytes);
return base64String;
}
}
}
}
< img src="data:image/jpeg;base64, @ConvertTiffToJpeg.ImageToBase64(@"c:\sample.tif")"/>
< img src="data:image/jpeg;base64, @ConvertTiffToJpeg.ImageToBase64(@"c:\sample.tif")"/>

