我正在尝试更改我的tiff文件的分辨率,使用下面的代码成功更改了它,但它在绘制后将输出文件大小从74kb增加到828kb
使用的代码:
public void Resize(string imageFile, string outputFile, int width,int height)
{
using (var srcImage = Image.FromFile(imageFile))
{
var newWidth = (int)(width);
var newHeight = (int)(height);
using (var newImage = new Bitmap(newWidth, newHeight))
{
using (var graphics = Graphics.FromImage(newImage))
{
graphics.SmoothingMode = SmoothingMode.AntiAlias;
graphics.InterpolationMode = InterpolationMode.NearestNeighbor;
graphics.PixelOffsetMode = PixelOffsetMode.None;
graphics.DrawImage(srcImage, new Rectangle(0, 0, newWidth, newHeight));
newImage.Save(outputFile,ImageFormat.Tiff);
}
}
}
}
答案 0 :(得分:0)
Tiff文件格式是无损格式,因此要减小使用压缩所需的大小,例如Lzw。
因此,您需要在保存文件时包含这些详细信息。
ImageCodecInfo myImageCodecInfo = GetEncoderInfo("image/tiff");
System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Compression;
EncoderParameters myEncoderParameters = new EncoderParameters(1);
EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, (long)EncoderValue.CompressionLZW);
myEncoderParameters.Param[0] = myEncoderParameter;
Helper功能来获取编码器
private static ImageCodecInfo GetEncoderInfo(String mimeType)
{
int j;
ImageCodecInfo[] encoders;
encoders = ImageCodecInfo.GetImageEncoders();
for (j = 0; j < encoders.Length; ++j)
{
if (encoders[j].MimeType == mimeType)
return encoders[j];
}
return null;
}
可在此处找到完整代码和更多详细信息:https://msdn.microsoft.com/en-us/library/system.drawing.imaging.encoder.compression(v=vs.110).aspx