您當前位置>首頁 » 新聞資訊 » 技(jì)術(shù)分(fēn)享 >
C# 生(shēng)成縮略圖方法
發表時(shí)間(jiān):2021-3-19
發布人(rén):葵宇科(kē)技(jì)
浏覽次數(shù):58
拿(ná)記事(shì)本寫的(de),沒有(yǒu)調試,大(dà)概就(jiù)是(sh₹φ ì)這(zhè)麽個(gè)原理(lǐ),參考參考得(de)了(le)~
可(kě)以按寬度,高(gāo)度縮放(fàng)及指定高(gāo)寬裁剪
private void MakeThum&∞ bnail(string sourcePath, stri¥♥≈ng newPath, int width, int he≤φ←✔ight)
{
System.Drawing.Image ig = S≈§ystem.Drawing.Image.FromFile(sourceP≤♥φαath);
int towidth = width;
int toheight = height;
int x = 0;
int y = 0;
int ow = ig.Width;
int oh = ig.Height;
if (height == 0)
{
toheight = oh * width / ow™×;
}
else if (width == 0)
{
towidth = ow * height / oh;
}
else
{
if ((double)ig.Width / (double)ig.Heig₽÷₹ht > (double)towidth / (doubl§ >e)toheight)
{
oh = ig.Height;
ow = ig.Height * towidth / toheight;
y = 0;
x = (ig.Width - ow) / 2;
}
else
{
ow = ig.Width;
oh = ig.Width * height / towidth;
x = 0;
y = (ig.Height - oh) / 2;
}
}
System.Drawing.Image bitmap = new Syste±☆♣πm.Drawing.Bitmap(towidth, toheight);
System.Drawing.Graphics g = Sysλ™Ωtem.Drawing.Graphics.FromImage(bitmap);
g.InterpolationMode = System♥σ.Drawing.Drawing2D.InterpolationMode.High;
g.SmoothingMode = System.Drawing.Drawing2Dβ♣≥.SmoothingMode.HighQuality;
g.Clear(System.Drawing.Color.Transparent)±®☆α;
g.DrawImage(ig, new System.Drawing.Recta€÷ε™ngle(0, 0, towidth, toheight), neΩ¥§w System.Drawing.Rectangle(x, y, ow, ¥≤oh), System.Drawing.GraphicsUnit.®≠Pixel);
try
{
bitmap.Save(newPath, System.Draα✘π€wing.Imaging.ImageFormat.Jpeg);
}
catch (Exception ex)
{
throw ex;
}
finally
{
ig.Dispose();
bitmap.Dispose();
g.Dispose();
}
}