前言:
图像压缩的格式目前较常用的有jpeg,png,gif和tiff;
jpeg是有损压缩,但压缩比很大;
png是无损压缩,但是压缩比很小,而且用png24才一般不致于图片损失;
gif也是无损压缩,但是压缩比比png还要小,但是因为是隔行存放数据,解码很快
tiff也是无损压缩,压缩比可以较容易调节,一般用于高清出版物,因为是目前最复杂的图象保存格式,所以代码操作起来较麻烦;
好了,直接上代码
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Bitmap myBitmap;
ImageCodecInfo myImageCodecInfo;
Encoder myEncoder;
EncoderParameter myEncoderParameter;
EncoderParameters myEncoderParameters;
// Create a Bitmap object based on a BMP file.
myBitmap = new Bitmap(@"D:\BOARD\JH3.bmp");
// Get an ImageCodecInfo object that represents the TIFF codec.
myImageCodecInfo = GetEncoderInfo("image/tiff");
// Create an Encoder object based on the GUID
// for the ColorDepth parameter category.
myEncoder = Encoder.ColorDepth;
// Create an EncoderParameters object.
// An EncoderParameters object has an array of EncoderParameter
// objects. In this case, there is only one
// EncoderParameter object in the array.
myEncoderParameters = new EncoderParameters(1);
// Save the image with a color depth of 24 bits per pixel.
myEncoderParameter =
new EncoderParameter(myEncoder, 24L);
myEncoderParameters.Param[0] = myEncoderParameter;
myBitmap.Save(@"d:\Shapes24bpp.tiff", myImageCodecInfo, myEncoderParameters);
}
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;
}
}