您當前位置>首頁 » 新聞資訊 » 技(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, strin™§≈&g newPath, int width, int heigh"β→ t)
{
System.Drawing.Image ig = System.Drawing.Image.Fr≥≈omFile(sourcePath);
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.Heigh€∞§t > (double)towidth / (double)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 Syst€≥em.Drawing.Bitmap(towidth, to∞¥ height);
System.Drawing.Graphics g = System.Drawing.Graph®Ω∏ics.FromImage(bitmap);
g.InterpolationMode = System.Drawing.δ¥Drawing2D.InterpolationMode.High®≠∑ ;
g.SmoothingMode = System.Drawing¶Ω♣&.Drawing2D.SmoothingMode.HighQuality;
g.Clear(System.Drawing.Color.Transp↓♥↓arent);
g.DrawImage(ig, new System.Drawing.Recta↓€×ngle(0, 0, towidth, toheight)₽"γ↓, new System.Drawing.Rectangle(x ✘, y, ow, oh), System.Drawing.GraphicsU↔π£nit.Pixel);
try
{
bitmap.Save(newPath, System.Drawing.Imaging.¶φ↑&ImageFormat.Jpeg);
}
catch (Exception ex)
{
throw ex;
}
finally
{
ig.Dispose();
bitmap.Dispose();
g.Dispose();
}
}