111,094
社区成员




public static void thumb(string rSrcImgPath, string rDstImgPath, int width, string lv = "h", int height = 0)
{
//利用Image对象装载源图像
System.Drawing.Image image = System.Drawing.Image.FromFile(rSrcImgPath);
//创建一个System.Drawing.Bitmap对象,并设置你希望的缩略图的宽度和高度。
int thumbWidth, thumbHeight;
double srcWidth = image.Width;
double srcHeight = image.Height;
if (height == 0)//如果高度height为0,则原比例缩放
{
thumbWidth = width;
double d = srcHeight / srcWidth;
thumbHeight = Convert.ToInt32( d * width );
}
else//否则按要求宽高生成
{
thumbWidth = width;
thumbHeight = height;
}
Bitmap bmp = new Bitmap(thumbWidth, thumbHeight);
//从Bitmap创建一个System.Drawing.Graphics对象,用来绘制高质量的缩小图。
System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp);
if (lv=="h")
{
//设置 System.Drawing.Graphics对象的SmoothingMode属性为HighQuality
gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//下面这个也设成高质量
gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
//下面这个设成High
gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
}
else if (lv=="m")
{
gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None;
gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.Default;
gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Default;
}
else if (lv=="l")
{
gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None;
gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighSpeed;
gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Low;
}
//把原始图像绘制成上面所设置宽高的缩小图
System.Drawing.Rectangle rectDestination = new System.Drawing.Rectangle(0, 0, thumbWidth, thumbHeight);
gr.DrawImage(image, rectDestination, 0, 0,Convert.ToInt32( srcWidth),Convert.ToInt32( srcHeight), GraphicsUnit.Pixel);
//保存图像,大功告成!
bmp.Save(rDstImgPath);
//最后别忘了释放资源
bmp.Dispose();
image.Dispose();
}
Bitmap myBitmap;
ImageCodecInfo myImageCodecInfo;
Encoder myEncoder;
EncoderParameter myEncoderParameter;
EncoderParameters myEncoderParameters;
// Create a Bitmap object based on a BMP file.
myBitmap = new Bitmap("Shapes.bmp");
// Get an ImageCodecInfo object that represents the JPEG codec.
myImageCodecInfo = GetEncoderInfo("image/jpeg");
// Create an Encoder object based on the GUID
// for the Quality parameter category.
myEncoder = Encoder.Quality;
// 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 bitmap as a JPEG file with quality level 25.
myEncoderParameter = new EncoderParameter(myEncoder, 25L);
myEncoderParameters.Param[0] = myEncoderParameter;
myBitmap.Save("Shapes025.jpg", myImageCodecInfo, myEncoderParameters);