private
static
Bitmap CreateThumbnail(Bitmap source,
int
thumbWi,
int
thumbHi,
bool
maintainAspect)
{
// return the source image if it's smaller than the designated thumbnail
if (source.Width < thumbWi && source.Height < thumbHi) return source;
System.Drawing.Bitmap ret = null ;
try
{
int wi, hi;
wi = thumbWi;
hi = thumbHi;
if (maintainAspect)
{
// maintain the aspect ratio despite the thumbnail size parameters
if (source.Width > source.Height)
{
wi = thumbWi;
hi = ( int )(source.Height * (( decimal )thumbWi / source.Width));
}
else
{
hi = thumbHi;
wi = ( int )(source.Width * (( decimal )thumbHi / source.Height));
}
}
// original code that creates lousy thumbnails
// System.Drawing.Image ret = source.GetThumbnailImage(wi,hi,null,IntPtr.Zero);
ret = new Bitmap(wi, hi);
using (Graphics g = Graphics.FromImage(ret))
{
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.FillRectangle(Brushes.White, 0 , 0 , wi, hi);
g.DrawImage(source, 0 , 0 , wi, hi);
g.Dispose();
}
}
catch
{
ret = null ;
}
return ret;
}
public static void CreateCompressThumbnail(Bitmap source, int thumbWi, int thumbHi, bool maintainAspect, string SavePathThumb, string fileName)
{
using (System.Drawing.Image myThumbnail = CreateThumbnail(source, thumbWi, thumbHi, maintainAspect))
{
System.Drawing.Imaging.EncoderParameters encoderParams = new System.Drawing.Imaging.EncoderParameters();
long [] quality = new long [ 1 ];
quality[ 0 ] = 75 ;
System.Drawing.Imaging.EncoderParameter encoderParam = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
encoderParams.Param[ 0 ] = encoderParam;
System.Drawing.Imaging.ImageCodecInfo[] arrayICI = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();
System.Drawing.Imaging.ImageCodecInfo jpegICI = null ;
for ( int x = 0 ; x < arrayICI.Length; x ++ )
{
if (arrayICI[x].FormatDescription.Equals( " JPEG " ))
{
jpegICI = arrayICI[x];
break ;
}
}
Bitmap bmp = new Bitmap(myThumbnail);
bmp.Save(SavePathThumb + fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
// myThumbnail.Save(Path.Combine(SavePathThumb, fileName + ".jpg"), jpegICI, encoderParams);
bmp.Dispose();
myThumbnail.Dispose();
{
// return the source image if it's smaller than the designated thumbnail
if (source.Width < thumbWi && source.Height < thumbHi) return source;
System.Drawing.Bitmap ret = null ;
try
{
int wi, hi;
wi = thumbWi;
hi = thumbHi;
if (maintainAspect)
{
// maintain the aspect ratio despite the thumbnail size parameters
if (source.Width > source.Height)
{
wi = thumbWi;
hi = ( int )(source.Height * (( decimal )thumbWi / source.Width));
}
else
{
hi = thumbHi;
wi = ( int )(source.Width * (( decimal )thumbHi / source.Height));
}
}
// original code that creates lousy thumbnails
// System.Drawing.Image ret = source.GetThumbnailImage(wi,hi,null,IntPtr.Zero);
ret = new Bitmap(wi, hi);
using (Graphics g = Graphics.FromImage(ret))
{
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.FillRectangle(Brushes.White, 0 , 0 , wi, hi);
g.DrawImage(source, 0 , 0 , wi, hi);
g.Dispose();
}
}
catch
{
ret = null ;
}
return ret;
}
public static void CreateCompressThumbnail(Bitmap source, int thumbWi, int thumbHi, bool maintainAspect, string SavePathThumb, string fileName)
{
using (System.Drawing.Image myThumbnail = CreateThumbnail(source, thumbWi, thumbHi, maintainAspect))
{
System.Drawing.Imaging.EncoderParameters encoderParams = new System.Drawing.Imaging.EncoderParameters();
long [] quality = new long [ 1 ];
quality[ 0 ] = 75 ;
System.Drawing.Imaging.EncoderParameter encoderParam = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
encoderParams.Param[ 0 ] = encoderParam;
System.Drawing.Imaging.ImageCodecInfo[] arrayICI = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();
System.Drawing.Imaging.ImageCodecInfo jpegICI = null ;
for ( int x = 0 ; x < arrayICI.Length; x ++ )
{
if (arrayICI[x].FormatDescription.Equals( " JPEG " ))
{
jpegICI = arrayICI[x];
break ;
}
}
Bitmap bmp = new Bitmap(myThumbnail);
bmp.Save(SavePathThumb + fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
// myThumbnail.Save(Path.Combine(SavePathThumb, fileName + ".jpg"), jpegICI, encoderParams);
bmp.Dispose();
myThumbnail.Dispose();
}
调用方法:
Bitmap bitmap = new Bitmap("f://1.jpg");
CreateCompressThumbnail(bitmap, 600, 600, true, "f://", "2.jpg");