using System.Drawing;
using System.Drawing.Imaging
public static void SmallPic(string stroldPic,string strnewPic,int intWidth, int intHeight)
{
System.Drawing.Image oldimage = System.Drawing.Image.FromFile(stroldPic);
System.Drawing.Image thumbnailImage = oldimage.GetThumbnailImage(intWidth, intHeight,new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);
Bitmap output=new Bitmap(thumbnailImage);
ImageCodecInfo[] codecs=ImageCodecInfo.GetImageEncoders();
ImageCodecInfo ici=null;
foreach(ImageCodecInfo codec in codecs){if(codec.MimeType=="image/jpeg")ici=codec;}
EncoderParameters ep=new EncoderParameters();
ep.Param[0]=new EncoderParameter(System.Drawing.Imaging.Encoder.Quality,(long)100);
output.Save(strnewPic,ici,ep);
ep.Dispose();
output.Dispose();
oldimage.Dispose();
thumbnailImage.Dispose();
System.IO.File.Delete(stroldPic);
}
private static bool ThumbnailCallback()
{
return false;
}
改进后:
public Bitmap SmallPic(Image img, int intWidth, int intHeight)
{
Image thumbnailImage = img.GetThumbnailImage(intWidth, intHeight, new Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);
Bitmap output = new Bitmap(thumbnailImage);
return output;
}
private bool ThumbnailCallback()
{
return false;
}