碼上快樂
1秒登錄
首頁
榜單
標簽
關於
搜索
相關內容
簡體
繁體
C#等比例縮放圖片
本文轉載自
查看原文
2019-07-11 17:48
3337
[37]C#
等比例縮放圖片(C#)
private Bitmap ZoomImage(Bitmap bitmap,
int destHeight,
int destWidth)
{
try
{
System.Drawing.Image sourImage = bitmap;
int width = 0, height = 0;
//按比例縮放
int sourWidth = sourImage.Width;
int sourHeight = sourImage.Height;
if (sourHeight > destHeight || sourWidth > destWidth)
{
if ((sourWidth * destHeight) > (sourHeight * destWidth))
{
width = destWidth;
height = (destWidth * sourHeight) / sourWidth;
}
else
{
height = destHeight;
width = (sourWidth * destHeight) / sourHeight;
}
}
else
{
width = sourWidth;
height = sourHeight;
}
Bitmap destBitmap =
new Bitmap(destWidth, destHeight);
Graphics g = Graphics.FromImage(destBitmap);
g.Clear(Color.Transparent);
//設置畫布的描繪質量
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.DrawImage(sourImage,
new Rectangle((destWidth - width) / 2, (destHeight - height) / 2, width, height), 0, 0, sourImage.Width, sourImage.Height, GraphicsUnit.Pixel);
g.Dispose();
//設置壓縮質量
System.Drawing.Imaging.EncoderParameters encoderParams =
new System.Drawing.Imaging.EncoderParameters();
long[] quality =
new
long[1];
quality[0] = 100;
System.Drawing.Imaging.EncoderParameter encoderParam =
new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
encoderParams.Param[0] = encoderParam;
sourImage.Dispose();
return destBitmap;
}
catch
{
return bitmap;
}
}
×
免責聲明!
本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。
猜您在找
等比例縮放圖片(C#)
C# 等比例縮放圖片
圖片等比例縮放
圖片等比例縮放
js實現圖片的等比例縮放
html img圖片等比例縮放
css 如何實現圖片等比例縮放
html img圖片等比例縮放
html img圖片等比例縮放
JS實現等比例縮放圖片
粵ICP備18138465號
© 2018-2025 CODEPRJ.COM