当前位置:网站首页>C#中如何调整图像大小
C#中如何调整图像大小
2022-06-25 06:41:00 【金士顿】
在本篇文章中,我将介绍如何在C#中来调整你想要的图像大小。要实现这一目标,我们可以采取以下几个步骤:
1.首先要获取你想要调整大小的图像:
string path = Server.MapPath("~/Images");
System.Drawing.Image img = System.Drawing.Image.FromFile(string.Concat(path,"/3904.jpg"));
2.将图像转换为Bitmap:
Bitmap b = new Bitmap(img);
3.创建一个调整图像大小的方法:
private static System.Drawing.Image resizeImage(System.Drawing.Image imgToResize, Size size)
{
//获取图片宽度
int sourceWidth = imgToResize.Width;
//获取图片高度
int sourceHeight = imgToResize.Height;
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;
//计算宽度的缩放比例
nPercentW = ((float)size.Width / (float)sourceWidth);
//计算高度的缩放比例
nPercentH = ((float)size.Height / (float)sourceHeight);
if (nPercentH < nPercentW)
nPercent = nPercentH;
else
nPercent = nPercentW;
//期望的宽度
int destWidth = (int)(sourceWidth * nPercent);
//期望的高度
int destHeight = (int)(sourceHeight * nPercent);
Bitmap b = new Bitmap(destWidth, destHeight);
Graphics g = Graphics.FromImage((System.Drawing.Image)b);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
//绘制图像
g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
g.Dispose();
return (System.Drawing.Image)b;
}
在上面的方法中,我们获取了位图图像,然后绘制了不同尺寸的图像(这里绘制出的图像是基于指定的纵横比)
4.调用上述方法,得到调整大小之后的图片:
System.Drawing. Image i = resizeImage(b, new Size(100, 100));
输出结果:
谢谢浏览,希望对你有所帮助。
边栏推荐
- lebel只想前面有星号,但是不想校验
- Collection of common terms and meanings in forestry investigation based on lidar
- NSIS 静默安装vs2013运行时
- 数据可视化没有重点怎么办?
- Sichuan Tuwei ca-if1051 can transceiver has passed aec-q100 grade 1 certification
- 57. 插入区间
- "Spatial transformation" significantly improves the quality of ground point extraction of cliff point cloud
- “空间转换”显著提升陡崖点云的地面点提取质量
- Without "rice", you can cook "rice". Strategy for retrieving missing ground points under airborne lidar forest using "point cloud intelligent mapping"
- 【QT】qtcreator便捷快捷键以及QML介绍
猜你喜欢

CPDA|数据分析师成长之路如何起步?
![对链表进行插入排序[dummy统一操作+断链核心--被动节点]](/img/2a/ccb1145d2b4f9fbd8d0812deace93b.png)
对链表进行插入排序[dummy统一操作+断链核心--被动节点]

Summary of small problems in smartbugs installation
![[single chip microcomputer project training] multipoint temperature wireless acquisition system based on nRF905](/img/a7/fc5d2f4640322a5d7222cce83c8898.jpg)
[single chip microcomputer project training] multipoint temperature wireless acquisition system based on nRF905

Application of point cloud intelligent drawing in intelligent construction site

Five causes of PCB board deformation and six solutions 2021-10-08

Without "rice", you can cook "rice". Strategy for retrieving missing ground points under airborne lidar forest using "point cloud intelligent mapping"

Four software 2021-10-14 suitable for beginners to draw PCB

STL tutorial 4- input / output stream and object serialization

Modular programming of digital light intensity sensor module gy-30 (main chip bh1750fvi) controlled by single chip microcomputer (under continuous updating)
随机推荐
Sichuan Tuwei ca-if1051 can transceiver has passed aec-q100 grade 1 certification
Access to foreign lead domain name mailbox
Runtime——methods成员变量,cache成员变量
【深度学习 轻量型backbone】2022 EdgeViTs CVPR
This year, I graduated
STL教程4-输入输出流和对象序列化
Modular programming of digital light intensity sensor module gy-30 (main chip bh1750fvi) controlled by single chip microcomputer (under continuous updating)
MySQL face Scripture eight part essay
ELK + filebeat日志解析、日志入库优化 、logstash过滤器配置属性
Cglib dynamic proxy
NSIS silent installation vs2013 runtime
微信小程序入门记录
How to use printf of 51 single chip microcomputer
基于激光雷达的林业调查常用术语及含义锦集
Five causes of PCB board deformation and six solutions 2021-10-08
无“米”,也能煮“饭”利用“点云智绘”反演机载LiDAR林下缺失地面点攻略
Chuantu microelectronics breaks through the high-end isolator analog chip market with ca-is3062w
Hisilicon 3559 sample parsing: Vio
一次弄清楚 Handler 可能导致的内存泄漏和解决办法
el-input实现尾部加字