C#图片按比例缩放的实现代码
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;
}//根据图片大小获取新图片从原图片截取的区域
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();//获得包含有关内置图像编码解码器的信息的ImageCodecInfo 对象。
ImageCodecInfo jpegICI = null;
for (int i = 0; i < arrayICI.Length; i++)
{
if (arrayICI[i].FormatDescription.Equals("JPEG"))
{
jpegICI = arrayICI[i];//设置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>();
// 原始宽高在指定宽高范围内,不作任何处理
if (orgWidth <= spcWidth && orgHeight <= spcHeight)
{
size["Width"] = orgWidth;
size["Height"] = orgHeight;
}
else
{
// 取得比例系数
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;
}
}
}
您可能感兴趣的文章:
- asp.net(C#)使用QRCode生成图片中心加Logo或图像的二维码实例
- Asp.net(C#)读取数据库并生成JS文件制作首页图片切换效果(附demo源码下载)
- ASP.NET(C#)实现一次性动态上传多张图片的代码(多个文件)
- asp.net(c#)实现从sqlserver存取二进制图片的代码
- asp.net(C#)压缩图片,可以指定图片模板高宽
- asp.net(c#)获取内容第一张图片地址的函数
- asp.net(c#)判断远程图片是否存在
- C# 将字节流转换为图片的实例方法
- C#识别出图片里的数字和字母
- c#实现图片二值化例子(黑白效果)
- c#图片缩放图片剪切功能实现(等比缩放)
- asp.net(c#)编程实现将彩色图片变灰阶图片的方法示例
- php实现图片等比例缩放代码
- Android ImageView图片代码实现按屏幕宽度等比例缩放
- javascript 指定区域内图片等比例缩放实现代码 脚本之家整合版 原创
- JS实现按比例缩放图片的方法(附C#版代码)
- JS实现按比例缩放图片的方法(附C#版代码)
- php实现图片等比例缩放代码
- JS实现按比例缩放图片的方法(附C#版代码)
- javascript 指定区域内图片等比例缩放实现代码 脚本之家整合版 原创
- JavaScript实现网页图片等比例缩放实现代码及调用方式
- 实现图片等比例缩放的js代码
- 实现图片按照比例显示(java代码)
- 按比例缩放图片的js代码
- 一个可以使得上传的图片大小按照指定的宽度,高度自动按比例进行缩放的函数(C#)
- 用JavaScript实现图片等比例缩放
- C# 添加图片水印类实现代码
- 如何在C#中实现图片缩放
- jQuery实现按比例缩放图片
- 用 JavaScript 实现网页图片等比例缩放
- asp.net +C# +sql2000 实现仿csdn的图片轮换代码