搜索曆史

清除曆史

熱門搜索

網絡工作室網站建設seo優化小程序

零基礎幹貨

零基礎幹貨

SERVICE CENTER

C# 按模版比例最大範圍的裁剪圖片并縮放至模版尺寸

網站建設 2021/12/8 12:44:14   &就麗nbsp;管理員  秒道  閱讀 183

///

/// 指定長(cháng)寬裁剪

/// 按模版比例最大範圍的裁剪圖片并縮放至模理購版尺寸

///

/// 吳劍 2012-08-08

/// 原圖Stream對(duì)象

/// 保存路徑

/// 最大寬(單位:px)

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

/// 默認不起(qǐ)用原始比例

public static void CutFor紅長Custom(string fromFile, string fi了信leSaveUrl, int diy_width = 要還QIYEIS_Config.thm_pic_width, int di自黑y_height = QIYE鐵體IS_Config.thm_pic火熱_height, bool _filerate = fal時現se)

{


int maxWidth = diy_width;

int maxHeight = diy_height;

//int quality = QIYEIS_C呢化onfig.thm_pic_quality;


//從文件獲取原始圖片,并使用流中嵌入的顔色管理信息

System.Drawing.Image initImage 樹費= System.Drawing.Image.FromFile(fro對去mFile);

////原圖寬高均小于模版,不作處理,直接保存

//if (initImage.Width <= maxWidth 商玩&& initImage.Height <= 鐵算maxHeight)

//{

// initImage.Sav農民e(fileSaveUrl, 章紙System.Drawing.Im妹舞aging.ImageFormat.Jpeg);

//}

//else

//{

//原圖片的寬高比例

double initRate = (double)initIm歌山age.Width / initImage.Height;

//模版的寬高比例

double templateRate = (double)ma從鄉xWidth / maxHeight;

//保持圖片原始比例,不足部分白色補齊

if (_filerate == 服金true)

{

MakeThumbnail(fromFile, file水歌SaveUrl, diy_width, diy_height);門鄉

}

else

{


//原圖與模版比例相等,直接縮放

if (templateRate == initRate)

{

//按模版大小生成(chéng)最終圖片

System.Drawing.Image template中哥Image = new System.Drawing.Bitmap懂路(maxWidth, maxHeight);

System.Drawing.Graphics tem外森plateG = System.Dr人玩awing.Graphics.FromImage(templateImage坐就);

templateG.Interpolati森但onMode = System.Drawing.D火內rawing2D.InterpolationMode.High;

templateG.SmoothingMode = Sys秒坐tem.Drawing.Drawing2D.Smoothing冷家Mode.HighQuality;輛頻

templateG.Clear(Color.White);

templateG.DrawImage(initImage,熱月 new System.Drawing.Rect車雨angle(0, 0, maxWidth, maxHe可土ight), new System.Drawing.Rectangle(0,靜鐵 0, initImage.Width, 文自initImage.Height), System.Dra歌美wing.GraphicsUnit.Pi書懂xel);

templateImage.Save(fileSaveUr場訊l, System.Drawing.Imagi媽自ng.ImageFormat.Jpeg);

}

//原圖與模版比例不等,裁剪後(hòu)縮放了北

else

{

//裁剪對(duì)象

System.Drawing.Image pickedI理睡mage = null;

System.Drawing.Graphics p放去ickedG = null;


//定位

Rectangle fromR = new 為海Rectangle(0, 0, 0, 0);//原圖裁剪定位

Rectangle toR =得亮 new Rectangle(0, 0, 0,員工 0);//目标定位


//寬為标準進(jìn)行裁剪

if (templateRate >相來 initRate)

{

//裁剪對(duì)象實例化

pickedImage = new System.Drawin開著g.Bitmap(initImage.Wid科船th, (int)System.Math.Floo子現r(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.W時紅idth;

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


//裁剪目标定位

toR.X = 0;

toR.Y = 0;

toR.Width = initImage.Width;

toR.Height = (int)Sy吧間stem.Math.Floor(initIma器煙ge.Width / templateRate);

}

//高為标準進(jìn)行裁剪

else

{

pickedImage = new System.Dr光議awing.Bitmap((int)System.Math.Floor筆地(initImage.Height * templateRate), 器光initImage.Height);

pickedG = System.Drawing.Graphic可懂s.FromImage(pickedImage);


fromR.X = (int)Syst作吃em.Math.Floor((initImage.Width - 雨快initImage.Height * templateRate) / 2)頻筆;

fromR.Y = 0;

fromR.Width = (上風int)System.Math.Floor(initImage.H用討eight * templateRate);

fromR.Height = 中相initImage.Height;


toR.X = 0;

toR.Y = 0;

toR.Width = (int)System.Math.Floor制數(initImage.Height * template的能Rate);

toR.Height = initImage.H飛低eight;

}


//設置質量

pickedG.InterpolationMode = 喝工System.Drawing.Drawing2D.雜高InterpolationMo窗船de.HighQualityBicubic;

pickedG.Smoothi化金ngMode = System.服了Drawing.Drawing2D.SmoothingMode.Hig土些hQuality;

//裁剪

pickedG.DrawImage(initImage, toR, from照土R, System.Drawing.Graphi亮木csUnit.Pixel);

//按模版大小生成(chéng)最終圖片

System.Drawing.Image民嗎 templateImage = new System.D他喝rawing.Bitmap(maxW我可idth, maxHeight);

System.Drawing.Grap公視hics templateG = System.Drawing.Grap媽說hics.FromImage(templateImage);

templateG.Interpolat熱好ionMode = System.Drawing.Drawing2D暗藍.InterpolationMo船厭de.High;

templateG.SmoothingMode = System明著.Drawing.Drawing2D.Smoothin體拍gMode.HighQuality;

templateG.Clear(Color.White);

templateG.DrawImage(pickedImage, new Sy內醫stem.Drawing.Rectangle(算金0, 0, maxWidth, maxHeig鄉厭ht), new System頻他.Drawing.Rectangl們廠e(0, 0, pickedImage.Width, p數月ickedImage.Height), System.Drawing.河西GraphicsUnit.Pixel);


try

{

//以原有後(hòu)綴保存圖片

ImageSave_Extensio你河n(fromFile, fileSaveUrl, tem快得plateImage);

}

catch (System.Exce林都ption e)

{

throw e;

}

finally

{

templateG.Dispose();

templateImage.Di男都spose();


pickedG.Dispose();

pickedImage.Dispose();



}

////關鍵質量控制

////獲取系統編碼類型數組,包含了jp就件eg,bmp,png,gif,tiff

//ImageCodecInfo作南[] icis = ImageCode行新cInfo.GetImageEncoders();

//ImageCodecInfo ici = null;

//foreach (ImageCodecInfo i 音影in icis)

//{

// if (i.MimeType == "image/jpeg" ||友動 i.MimeType == "image/bmp"白湖 || i.MimeType == "ima頻年ge/png" || i.MimeType ==訊他 "image/gif")

// {

// ici = i;

// }

//}

//EncoderParameters ep = new En火聽coderParameters(1);

//ep.Param[0] = new Encode對相rParameter(System.Drawing.Imagin船錯g.Encoder.Quality, (long)quality);


////保存縮略圖

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

////templateImage.Save(fileS校票aveUrl, System.Drawing.Imaging.好西ImageFormat.Jpeg);


////釋放資源

//templateG.Disp可看ose();

//templateImage.Dispose();


//pickedG.Dispose();

//pickedImage.Dispose();

}

//}

}

//釋放資源

initImage.Dispose();

}


TAG标簽:C#圖片縮放
鍊接地址:
鄭重聲明:以上内容來源自玖捌網絡工作室,轉載請注明出處!

兼顧高品質與個性化的界面(miàn)設計

網站無須三方授權 · 安全穩定、維護方便

玖捌網絡工作室
18972931619
掃碼獲取最新報價
0.052735