当前位置:网站首页>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));
输出结果:
谢谢浏览,希望对你有所帮助。
边栏推荐
- Terms and concepts related to authority and authentication system
- (tool class) use SecureCRT as the communication medium
- Pit encountered by pytorch: why can't l1loss decrease during model training?
- Storage of Galileo broadcast ephemeris in rtklib-b33
- Static bit rate (CBR) and dynamic bit rate (VBR)
- El input to add words to the tail
- 27. 移除元素
- 力扣78:子集
- Microsoft Office Word 远程命令执行漏洞(CVE-2022-30190)分析与利用
- Four software 2021-10-14 suitable for beginners to draw PCB
猜你喜欢
Sichuan Tuwei ca-if1051 can transceiver has passed aec-q100 grade 1 certification
"Spatial transformation" significantly improves the quality of ground point extraction of cliff point cloud
Summary of small problems in smartbugs installation
realsense d455 semantic_ Slam implements semantic octree mapping
el-input实现尾部加字
Different paths ii[dynamic planning improvement for DFS]
Debian introduction
差点被这波Handler 面试连环炮带走~
Sichuan earth microelectronics ca-is1300 isolated operational amplifier for current detection is on the market
shell小技巧(一百三十四)简单的键盘输入记录器
随机推荐
“空间转换”显著提升陡崖点云的地面点提取质量
Four software 2021-10-14 suitable for beginners to draw PCB
Bicubic difference
VectorDraw Web Library 10.10
IAR compiler flashback
Path planner based on time potential function in dynamic environment
Construction of occupancy grid map
Runtime——methods成员变量,cache成员变量
Sichuan earth microelectronics 8-channel isolated digital input receiver
Notes: [open class] neural network and deep learning -- tensorflow2.0 actual combat [Chinese course]
栅格地图(occupancy grid map)构建
smartBugs安装小问题总结
双三次差值bicubic
Vscode official configuration synchronization scheme
CPDA|数据分析师成长之路如何起步?
差点被这波Handler 面试连环炮带走~
权限、认证系统相关名词概念
27. 移除元素
C get the version number of exe - file version and assembly version
音频(五)音频特征提取