private static bool ThumbnailCallback()
{
return false;
}
//****************************************************************************
//设置高质量插值法
//g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
//g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空画布并以透明背景色填充
//g.Clear(Color.Transparent);
//****************************************************************************
/// <summary>
/// 图片智能截取
/// </summary>
/// <returns></returns>
public static bool IntellectiveCut(string srcPath, string destPath, double width, double height)
{
System.Drawing.Image img = new Bitmap(srcPath);
//生成图片大小必需小于原图
if (width > img.Width || height > img.Height)
return false;
//删除的高度,与宽度
double cutWidth, cutHeight;
cutWidth =(img.Width * height/ img.Height -width); //宽度切割,高度缩放
cutHeight = (img.Height * width /img.Width-height);//高度切割,宽度缩放
byte flag=0;//0 截高,1 截宽
flag =(byte)( cutHeight < cutWidth ? 0 : 1);
if (cutWidth >= 0 && cutHeight < 0)
flag = 1;
if (cutHeight >= 0 && cutWidth < 0)
flag = 0;
//System.Drawing.Image.GetThumbnailImageAbort myCallback=new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
System.Drawing.Image thumImg;
if (flag == 0)
thumImg = new Bitmap(img,(int)width, (int)height + (int)cutHeight); //img.GetThumbnailImage((int)width, (int)height + (int)cutHeight, myCallback, IntPtr.Zero);
else
thumImg = new Bitmap(img,(int)width + (int)cutWidth, (int)height);// img.GetThumbnailImage((int)width + (int)cutWidth, (int)height, myCallback, IntPtr.Zero);
System.Drawing.Bitmap destImg = new Bitmap((int)width, (int)height);
Graphics g = Graphics.FromImage(destImg);
Rectangle rect = new Rectangle(0, 0, (int)width,(int) height);
g.DrawImage(thumImg, rect, rect, GraphicsUnit.Pixel);
g.Save();
destImg.Save(destPath,ImageFormat.Jpeg);
thumImg.Dispose();
img.Dispose();
destImg.Dispose();
return true;
}