C#圖片按比例縮放的實(shí)現(xiàn)代碼
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
namespace Publics
{
public class ImgHelper
{
public static void AdjustPhoto(int toWidth, int toHeight, string filePath, string fromFileName, string toFileName, int maxWidth, int maxHeight)
{
Image originalImage = Image.FromFile(filePath + "/" + fromFileName);
//如果尺寸不夠返回保存原圖
if (originalImage.Width < toWidth && originalImage.Height < toHeight)
{
originalImage.Save(filePath + "/" + toFileName);
originalImage.Dispose();
return;
}
//根據(jù)圖片大小獲取新圖片從原圖片截取的區(qū)域
int x, y, w, h;
if (toHeight > 0)
{
if (toWidth > 0)
{
if (originalImage.Width > toWidth && originalImage.Height > toHeight)
{
w = toWidth;
h = toWidth * originalImage.Height / originalImage.Width;
if (h > toHeight)
{
h = toHeight;
w = toHeight * originalImage.Width / originalImage.Height;
x = (toWidth - w) / 2;
y = 0;
}
else
{
x = 0;
y = (toHeight - h) / 2;
}
}
else if (originalImage.Width > toWidth)
{
w = toWidth;
h = toWidth * originalImage.Height / originalImage.Width;
x = 0;
y = (toHeight - h) / 2;
}
else if (originalImage.Height > toHeight)
{
h = toHeight;
w = toHeight * originalImage.Width / originalImage.Height;
x = (toWidth - w) / 2;
y = 0;
}
else
{
w = originalImage.Width;
h = originalImage.Height;
x = (toWidth - w) / 2;
y = (toHeight - h) / 2;
}
}
else
{
if (originalImage.Height > maxHeight)
{
toWidth = toHeight * originalImage.Width / originalImage.Height;
x = 0;
y = 0;
w = toWidth;
h = toHeight;
}
else
{
x = 0;
y = 0;
w = originalImage.Width;
h = originalImage.Height;
toWidth = originalImage.Width;
toHeight = originalImage.Height;
}
}
}
else
{
if (originalImage.Width > maxWidth)
{
toHeight = toWidth * originalImage.Height / originalImage.Width;
x = 0;
y = 0;
w = toWidth;
h = toHeight;
}
else
{
x = 0;
y = 0;
w = originalImage.Width;
h = originalImage.Height;
toWidth = originalImage.Width;
toHeight = originalImage.Height;
}
}
Bitmap bm = new Bitmap(toWidth, toHeight);
Graphics g = Graphics.FromImage(bm);
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.Clear(Color.White);
g.DrawImage(originalImage, new Rectangle(x, y, w, h), 0, 0, originalImage.Width, originalImage.Height, GraphicsUnit.Pixel);
long[] quality = new long[1];
quality[0] = 80;
EncoderParameters encoderParams = new EncoderParameters();
EncoderParameter encoderParam = new EncoderParameter(Encoder.Quality, quality);
encoderParams.Param[0] = encoderParam;
ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();//獲得包含有關(guān)內(nèi)置圖像編碼解碼器的信息的ImageCodecInfo 對(duì)象。
ImageCodecInfo jpegICI = null;
for (int i = 0; i < arrayICI.Length; i++)
{
if (arrayICI[i].FormatDescription.Equals("JPEG"))
{
jpegICI = arrayICI[i];//設(shè)置JPEG編碼
break;
}
}
if (jpegICI != null)
{
//bm.Save(Server.MapPath(path + "/thumb_" + filename), jpegICI, encoderParams);
bm.Save(filePath + "/" + toFileName, jpegICI, encoderParams);
}
bm.Dispose();
originalImage.Dispose();
g.Dispose();
}
/// <summary>
/// 保持比例圖像縮放簡易算法
/// </summary>
/// <param name="spcWidth"></param>
/// <param name="spcHeight"></param>
/// <param name="orgWidth"></param>
/// <param name="orgHeight"></param>
/// <returns></returns>
public static Dictionary<string, int> AdjustSize(int spcWidth, int spcHeight, int orgWidth, int orgHeight)
{
Dictionary<string, int> size = new Dictionary<string, int>();
// 原始寬高在指定寬高范圍內(nèi),不作任何處理
if (orgWidth <= spcWidth && orgHeight <= spcHeight)
{
size["Width"] = orgWidth;
size["Height"] = orgHeight;
}
else
{
// 取得比例系數(shù)
float w = orgWidth / (float)spcWidth;
float h = orgHeight / (float)spcHeight;
// 寬度比大于高度比
if (w > h)
{
size["Width"] = spcWidth;
size["Height"] = (int)(w >= 1 ? Math.Round(orgHeight / w) : Math.Round(orgHeight * w));
}
// 寬度比小于高度比
else if (w < h)
{
size["Height"] = spcHeight;
size["Width"] = (int)(h >= 1 ? Math.Round(orgWidth / h) : Math.Round(orgWidth * h));
}
// 寬度比等于高度比
else
{
size["Width"] = spcWidth;
size["Height"] = spcHeight;
}
}
return size;
}
}
}
相關(guān)文章
C#多線程之Thread中Thread.IsAlive屬性用法分析
這篇文章主要介紹了C#多線程之Thread中Thread.IsAlive屬性用法,實(shí)例分析了C#判斷線程可用狀態(tài)的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04基于C#動(dòng)手實(shí)現(xiàn)網(wǎng)絡(luò)服務(wù)器Web Server
這篇文章主要為大家詳細(xì)介紹了基于C#動(dòng)手實(shí)現(xiàn)網(wǎng)絡(luò)服務(wù)器Web Server,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10WPF實(shí)現(xiàn)倒計(jì)時(shí)轉(zhuǎn)場動(dòng)畫效果
這篇文章主要介紹了如何利用WPF實(shí)現(xiàn)倒計(jì)時(shí)轉(zhuǎn)場動(dòng)畫效果,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)或工作有一定幫助,需要的可以參考一下2022-08-08使用C#實(shí)現(xiàn)基于TCP和UDP協(xié)議的網(wǎng)絡(luò)通信程序的基本示例
這篇文章主要介紹了使用C#實(shí)現(xiàn)基于TCP和UDP協(xié)議的網(wǎng)絡(luò)通信程序的示例,文中分別編寫了基本的服務(wù)器端和客戶端,代碼十分簡單,需要的朋友可以參考下2016-04-04C#中Thread(線程)和Task(任務(wù))實(shí)例詳解
.NET Framework在System.Threading命名空間中具有與線程相關(guān)的類,線程是一小組可執(zhí)行指令,這篇文章主要給大家介紹了關(guān)于C#中Thread(線程)和Task(任務(wù))的相關(guān)資料,需要的朋友可以參考下2022-03-03