C# 按指定宽高缩放图片
/// <summary>
/// 按指定宽高缩放图片
/// </summary>
/// <param name="image">原图片</param>
/// <param name="dstWidth">目标图片宽</param>
/// <param name="dstHeight">目标图片高</param>
/// <returns></returns>
public Image ScaleImage(Image image, int dstWidth, int dstHeight)
{
Graphics g = null;
try
{
//按比例缩放
float scaleRate = GetWidthAndHeight(image.Width, image.Height, dstWidth, dstHeight);
int width = (int)(image.Width * scaleRate);
int height = (int)(image.Height * scaleRate);
Bitmap destBitmap = new Bitmap(width, height);
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(image, new Rectangle(0, 0, width, height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel);
return destBitmap;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
throw;
}
finally
{
if (g != null)
{
g.Dispose();
}
if (image != null)
{
image.Dispose();
}
}
}
/// <summary>
/// 获取图片缩放比例
/// </summary>
/// <param name="oldWidth">原图片宽</param>
/// <param name="oldHeigt">原图片高</param>
/// <param name="newWidth">目标图片宽</param>
/// <param name="newHeight">目标图片高</param>
/// <returns></returns>
public float GetWidthAndHeight(int oldWidth, int oldHeigt, int newWidth, int newHeight)
{
//按比例缩放
float scaleRate;
if (oldWidth >= newWidth && oldHeigt >= newHeight)
{
int widthDis = oldWidth - newWidth;
int heightDis = oldHeigt - newHeight;
if (widthDis > heightDis)
{
scaleRate = newWidth * 1f / oldWidth;
}
else
{
scaleRate = newHeight * 1f / oldHeigt;
}
}
else if (oldWidth >= newWidth)
{
scaleRate = newWidth * 1f / oldWidth;
}
else if (oldHeigt >= newHeight)
{
scaleRate = newHeight * 1f / oldHeigt;
}
else
{
int widthDis = newWidth - oldWidth;
int heightDis = newHeight - oldHeigt;
if (widthDis > heightDis)
{
scaleRate = newHeight * 1f / oldHeigt;
}
else
{
scaleRate = newWidth * 1f / oldWidth;
}
}
return scaleRate;
}
C# 按指定宽高缩放图片的更多相关文章
- Android大图片之缩略图,以及对原图依照指定宽高裁剪成缩略图
<Android大图片之变换缩略图,以及对原始大图片依照指定宽.高裁剪成缩略图> 在Android的ImageView载入图像资源过程中,出于性能和内存开销的须要.有时候须要把一个原 ...
- php 图片上传的公共方法(按图片宽高缩放或原图)
写的用于图片上传的公共方法类调用方法: $upload_name='pic';$type = 'logo_val';$file_name = 'logo_' . $user_id .create_st ...
- ThinkPHP 5 文件上传及指定宽高生成缩略图公共方法
这个是非常常用的案例,ThinkPHP 5 文件上传及指定宽高生成缩略图公共方法/** * 单文件上传 * name:表单上传文件的名字 * ext: 文件允许的后缀,字符串形式 * path:文件保 ...
- Matrix: 利用Matrix来设置ImageView的宽高,使图片能正常显示
在Android中加载ImageView一般都不会给ImageView的宽高设置一个确切的值,一般都是直接写成: <ImageView android:id="@+id/iv_test ...
- 头像修改功能 包含ios旋转图片 但是旋转后没遮罩, 正常图片可以显示遮罩 宽高不规则图片没做控制 遮罩框可以拖动
https://blog.csdn.net/wk767113154/article/details/77989544 参考资料 <template> <div id="p ...
- js 获取图片宽高 和 图片大小
获取要查看大小的img var img_url = 'http://img5.imgtn.bdimg.com/it/u=4267222417,1017407570&fm=200&gp= ...
- excel表格添加固定宽高的图片
import xlsxwriter,xlrd import glob #打开excel文件 data=xlrd.open_workbook('优秀创意库-20180725.xlsx') #获取第一张工 ...
- JS实现图片宽高的等比缩放
关于图片宽高的等比缩放,其实需求就是让图片自适应父容器的宽高,并且是等比缩放图片,使图片不变形. 例如,需要实现如下的效果: 要实现上面的效果,需要知道图片的宽高,父容器的宽高,然后计算缩放后的宽高. ...
- java高质量缩放图片
可按照比例缩放,也可以指定宽高 import com.sun.image.codec.jpeg.JPEGImageEncoder; import com.sun.image.codec.jpeg.JP ...
- table-cell实现未知宽高图片,文本水平垂直居中在div
<BODY> <h1>未知宽高的图片水平垂直居中在div</h1> <!--box-outer--> <div class="box-o ...
随机推荐
- 我们在SqlSugar开发框架中,用到的一些设计模式
我们在<SqlSugar开发框架>中,有时候都会根据一些需要引入一些设计模式,主要的目的是为了解决问题提供便利和代码重用等目的.而不是为用而用,我们的目的是解决问题,并在一定的场景下以水到 ...
- oracle中约束(constraints)是如何影响查询计划的
原文: http://www.oracle.com/technetwork/issue-archive/2009/09-may/o39asktom-096149.html oracle中约束(cons ...
- CentOS 7 SSH连接超时自动断开解决方案
用SSH登录到Linux的时候,由于默认的连接超时时间很短,经常断开! 1.修改文件 # vi /etc/ssh/sshd_config # vi /etc/ssh/sshd_config 找到 #C ...
- org.apache.http.client.ClientProtocolException: URI does not specify a valid host name:localhost:xxx
今天部署应用的时候遇到的,总结一下我知道的有2个原因: 1.地址前要加http:// 这就是标题报错的原因,他用的是localhost:xxx 2.地址本身拼错了也会报这个,例如地址:http:// ...
- Docker进阶之02-Swarm集群入门实践
Docker集群概述 Docker集群有2种方案: 1.在Docker Engine 1.12之前的集群模式被称为经典集群,这是通过API代理系统实现的集群,目前已经不再维护. 2.自Docker E ...
- 初始pyqt5
开发pyqt5桌面应用 必须使用两个类:QApplication和QWidget.都在PyQt5.QtWidgets中 安装 pip install pyqt5 -i https://pypi.dou ...
- 【LeetCode栈与队列#05】滑动窗口最大值
滑动窗口最大值 力扣题目链接(opens new window) 给定一个数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧.你只可以看到在滑动窗口内的 k 个数字.滑动窗口 ...
- centos docker服务问题
概述 docker的应用版本正式上线,结果一上线就出各种幺蛾子. 本文档主要介绍centos系统安装docker和启动的问题解决方法. 环境 docker registry:2 centos 6 &a ...
- 第一百零九篇:基本数据类型(String类型)
好家伙, 本篇内容为<JS高级程序设计>第三章学习笔记 1.String类型 字符串类型是最常用的几个基本类型之一 字符串可以使用双引号,单引号以及反引号(键盘左Tab上面那个)标示 ...
- 【Azure K8S】演示修复因AKS密钥过期而导致创建服务不成功的问题(The provided client secret keys for app ****** are expired)
问题描述 在Azure Kubernetes 服务中,创建一个Internal Load Balancer服务,使用以下yaml内容: internallb.yaml apiVersion: v1 k ...