搜索历史

清除历史

热门搜索

网络工作室网站建设seo优化小程序

零基础干货

零基础干货

SERVICE CENTER

C# 按模版比例最大范围的裁剪图片并缩放至模版尺寸

网站建设 2021/12/8 12:44:14    管理员    阅读 597

///

/// 指定长宽裁剪

/// 按模版比例最大范围的裁剪图片并缩放至模版尺寸

///

/// 吴剑 2012-08-08

/// 原图Stream对象

/// 保存路径

/// 最大宽(单位:px)

/// 最大高(单位:px)

/// 默认不起用原始比例

public static void CutForCustom(string fromFile, string fileSaveUrl, int diy_width = QIYEIS_Config.thm_pic_width, int diy_height = QIYEIS_Config.thm_pic_height, bool _filerate = false)

{


int maxWidth = diy_width;

int maxHeight = diy_height;

//int quality = QIYEIS_Config.thm_pic_quality;


//从文件获取原始图片,并使用流中嵌入的颜色管理信息

System.Drawing.Image initImage = System.Drawing.Image.FromFile(fromFile);

////原图宽高均小于模版,不作处理,直接保存

//if (initImage.Width <= maxWidth && initImage.Height <= maxHeight)

//{

// initImage.Save(fileSaveUrl, System.Drawing.Imaging.ImageFormat.Jpeg);

//}

//else

//{

//原图片的宽高比例

double initRate = (double)initImage.Width / initImage.Height;

//模版的宽高比例

double templateRate = (double)maxWidth / maxHeight;

//保持图片原始比例,不足部分白色补齐

if (_filerate == true)

{

MakeThumbnail(fromFile, fileSaveUrl, diy_width, diy_height);

}

else

{


//原图与模版比例相等,直接缩放

if (templateRate == initRate)

{

//按模版大小生成最终图片

System.Drawing.Image templateImage = new System.Drawing.Bitmap(maxWidth, maxHeight);

System.Drawing.Graphics templateG = System.Drawing.Graphics.FromImage(templateImage);

templateG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

templateG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

templateG.Clear(Color.White);

templateG.DrawImage(initImage, new System.Drawing.Rectangle(0, 0, maxWidth, maxHeight), new System.Drawing.Rectangle(0, 0, initImage.Width, initImage.Height), System.Drawing.GraphicsUnit.Pixel);

templateImage.Save(fileSaveUrl, System.Drawing.Imaging.ImageFormat.Jpeg);

}

//原图与模版比例不等,裁剪后缩放

else

{

//裁剪对象

System.Drawing.Image pickedImage = null;

System.Drawing.Graphics pickedG = null;


//定位

Rectangle fromR = new Rectangle(0, 0, 0, 0);//原图裁剪定位

Rectangle toR = new Rectangle(0, 0, 0, 0);//目标定位


//宽为标准进行裁剪

if (templateRate > initRate)

{

//裁剪对象实例化

pickedImage = new System.Drawing.Bitmap(initImage.Width, (int)System.Math.Floor(initImage.Width / templateRate));

pickedG = System.Drawing.Graphics.FromImage(pickedImage);


//裁剪源定位

fromR.X = 0;

fromR.Y = (int)System.Math.Floor((initImage.Height - initImage.Width / templateRate) / 2);

fromR.Width = initImage.Width;

fromR.Height = (int)System.Math.Floor(initImage.Width / templateRate);


//裁剪目标定位

toR.X = 0;

toR.Y = 0;

toR.Width = initImage.Width;

toR.Height = (int)System.Math.Floor(initImage.Width / templateRate);

}

//高为标准进行裁剪

else

{

pickedImage = new System.Drawing.Bitmap((int)System.Math.Floor(initImage.Height * templateRate), initImage.Height);

pickedG = System.Drawing.Graphics.FromImage(pickedImage);


fromR.X = (int)System.Math.Floor((initImage.Width - initImage.Height * templateRate) / 2);

fromR.Y = 0;

fromR.Width = (int)System.Math.Floor(initImage.Height * templateRate);

fromR.Height = initImage.Height;


toR.X = 0;

toR.Y = 0;

toR.Width = (int)System.Math.Floor(initImage.Height * templateRate);

toR.Height = initImage.Height;

}


//设置质量

pickedG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

pickedG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

//裁剪

pickedG.DrawImage(initImage, toR, fromR, System.Drawing.GraphicsUnit.Pixel);

//按模版大小生成最终图片

System.Drawing.Image templateImage = new System.Drawing.Bitmap(maxWidth, maxHeight);

System.Drawing.Graphics templateG = System.Drawing.Graphics.FromImage(templateImage);

templateG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

templateG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

templateG.Clear(Color.White);

templateG.DrawImage(pickedImage, new System.Drawing.Rectangle(0, 0, maxWidth, maxHeight), new System.Drawing.Rectangle(0, 0, pickedImage.Width, pickedImage.Height), System.Drawing.GraphicsUnit.Pixel);


try

{

//以原有后缀保存图片

ImageSave_Extension(fromFile, fileSaveUrl, templateImage);

}

catch (System.Exception e)

{

throw e;

}

finally

{

templateG.Dispose();

templateImage.Dispose();


pickedG.Dispose();

pickedImage.Dispose();



}

////关键质量控制

////获取系统编码类型数组,包含了jpeg,bmp,png,gif,tiff

//ImageCodecInfo[] icis = ImageCodecInfo.GetImageEncoders();

//ImageCodecInfo ici = null;

//foreach (ImageCodecInfo i in icis)

//{

// if (i.MimeType == "image/jpeg" || i.MimeType == "image/bmp" || i.MimeType == "image/png" || i.MimeType == "image/gif")

// {

// ici = i;

// }

//}

//EncoderParameters ep = new EncoderParameters(1);

//ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)quality);


////保存缩略图

//templateImage.Save(fileSaveUrl, ici, ep);

////templateImage.Save(fileSaveUrl, System.Drawing.Imaging.ImageFormat.Jpeg);


////释放资源

//templateG.Dispose();

//templateImage.Dispose();


//pickedG.Dispose();

//pickedImage.Dispose();

}

//}

}

//释放资源

initImage.Dispose();

}


TAG标签:C#图片缩放
链接地址:/html/tech/wzjs/502.html
郑重声明:以上内容来源自玖捌网络工作室,转载请注明出处!

兼顾高品质与个性化的界面设计

网站无须三方授权 · 安全稳定、维护方便

玖捌网络工作室
18972931619
扫码获取最新报价
0.050787