C#图片按比例缩放的实现代码
发布时间:2022-12-19 03:58:16
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:Button ID="Button1" runat="server" T
- 在编写程序,我们经常会对一些时间进行比较,比如要搜寻一个时间范围中的数据,需要用户输入开始时间和结束时间,如果结束时间小于或等于开始时间,那
- 最近做了个自定义键盘,但面对不同分辨率的机型其中数字键盘不能根据界面大小自已铺满,但又不能每种机型都做一套吧,所以要做成自适应,那这里主讲思
- 了解了inbound事件的传播过程, 对于学习outbound事件传输的流程, 也不会太困难outbound事件传输流程在我们业务代码中,
- 以前面试的时候经常会碰到这样的问题.,叫你写一下ArrayList.LinkedList.Vector三者之间的区别与联系:原先一直搞不明白
- 因为项目中之前的模块用的分页插件是Datatables,很方便,但是新做的模块表格中的布局有变化,Datatables插件满足不了了。为了风
- StopWatch是位于org.springframework.util包下的一个工具类,通过它可方便的对程序部分代码进行计时(ms级别),
- 本文所述为基于C#实现的折叠菜单,风格仿照Windows打开我的电脑后左侧的伸缩菜单效果,并且同样是蓝色的效果,看着和windows的效果一
- 使用 replace 函数动态填充字符串String str="Hello {0},我是 {1},今年{2}岁"
- 去掉DataTable中(所有列的数据都相同)重复的行: //DataView dv = dt3.DefaultView;&nbs
- import java.util.concurrent.Semaphore;public class ThreeThread {
- 1.第一步在android studio的project视图模式下右击res文件夹2.然后找到New --> Android reso
- Android * 功能/手机关闭能拍照效果如下: 其实 * 与偷录实现方式是一样的,都是使用到的WindowManager来绘制
- 有很多应用场景,用到了接口动态实现,下面举几个典型的应用:1、mybatis / jpa 等orm框架,可以在接口上加注解进行开发,不需要编
- WebMvcConfigurer配置类其实是Spring内部的一种配置方式,采用JavaBean的形式来代替传统的xml配置文件形式进行针对
- 前言本文告诉大家如何使用 Marshal 做出可以快速释放内存的大数组。最近在做 3D ,需要不断申请一段大内存数组,然后就释放他,但是 C
- 在一些电子商务网站上经常能够看到一些滚动的广告条,许多软件在首次使用时也有类似的广告条,如图:其实在github上有实现这种效果的控件,不过
- 1、什么是内存泄漏内存泄漏是指无用对象(不再使用的对象)持续占有内存或无用对象的内存得不到及时释放,从而造成内存空间的浪费称为内存泄漏。随着
- 介绍在本文中,我们将了解如何将现有应用程序迁移到Spring 6以及如何充分利用此升级。本文中的提示基于我在Hypersistence Op
- 本文实例为大家分享了OpenCV+Qt实现图像处理操作的具体代码,供大家参考,具体内容如下一、目标Qt界面实现 雪花屏 高斯模糊 中值滤波