当前位置:网站首页>How to resize an image in C #
How to resize an image in C #
2022-06-25 07:49:00 【Kingston】
In this article , I'll show you how to C# To adjust the size of the image you want . To achieve this goal , We can take the following steps :
1. First get the image you want to size :
string path = Server.MapPath("~/Images");
System.Drawing.Image img = System.Drawing.Image.FromFile(string.Concat(path,"/3904.jpg"));
2. Convert image to Bitmap:
Bitmap b = new Bitmap(img);
3. Create a method to resize an image :
private static System.Drawing.Image resizeImage(System.Drawing.Image imgToResize, Size size)
{
// Get picture width
int sourceWidth = imgToResize.Width;
// Get the height of the picture
int sourceHeight = imgToResize.Height;
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;
// Calculate the scaling of the width
nPercentW = ((float)size.Width / (float)sourceWidth);
// Calculate the scaling of the height
nPercentH = ((float)size.Height / (float)sourceHeight);
if (nPercentH < nPercentW)
nPercent = nPercentH;
else
nPercent = nPercentW;
// The desired width
int destWidth = (int)(sourceWidth * nPercent);
// The height of expectations
int destHeight = (int)(sourceHeight * nPercent);
Bitmap b = new Bitmap(destWidth, destHeight);
Graphics g = Graphics.FromImage((System.Drawing.Image)b);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
// The plot
g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
g.Dispose();
return (System.Drawing.Image)b;
}
In the above method , We got the bitmap image , And then draw images of different sizes ( The image drawn here is based on the specified aspect ratio )
4. Call the above methods , Get the resized image :
System.Drawing. Image i = resizeImage(b, new Size(100, 100));
Output results :
Thank you for browsing. , I hope it helped you .
边栏推荐
- Knowledge sharing 𞓜 conventional laminated structure of six layer PCB
- 2160. 拆分数位后四位数字的最小和
- 年后求职找B端产品经理?差点把自己坑惨了......
- [little knowledge] PCB proofing process
- Tips on how to design soft and hard composite boards ~ 22021/11/22
- C#控件刷新
- 基于STM32MP157调试MIPI-DSI屏幕
- 【日常训练】207. 课程表
- C# 读取web上的xml
- Can I open a stock account with a compass? Is it safe?
猜你喜欢
ts环境搭建
Do you know why the PCB produces tin beads? 2021-09-30
Knowledge sharing 𞓜 conventional laminated structure of six layer PCB
Full range of isolator chips with integrated isolated power supply
无“米”,也能煮“饭”利用“点云智绘”反演机载LiDAR林下缺失地面点攻略
机器学习笔记 - 时间序列的线性回归
[batch dos-cmd command - summary and summary] - file and directory operation commands (MD, RD, xcopy, dir, CD, set, move, copy, del, type, sort)
Modular programming of wireless transmission module nRF905 controlled by single chip microcomputer
OpenCV每日函数 结构分析和形状描述符(8) fitLine函数 拟合直线
VOCALOID笔记
随机推荐
Different paths ii[dynamic planning improvement for DFS]
Advantages and differences of three kinds of vias in PCB 2021-10-27
微信小程序入门记录
Do you know why the PCB produces tin beads? 2021-09-30
传统的IO存在什么问题?为什么引入零拷贝的?
Share the process requirements for single-layer flexible circuit board
Six causes of PCB disconnection 2021-10-20
OpenMP入门
C#中如何调整图像大小
搞清信息化是什么,让企业转型升级走上正确的道路
基于RBAC 的SAAS系统权限设计
How much do you know about electronic components on PCB?
LeetCode_哈希表_中等_454.四数相加 II
Elk + filebeat log parsing, log warehousing optimization, logstash filter configuration attribute
VSCode很好,但我以后不会再用了
Tupu software digital twin 3D wind farm, offshore wind power of smart wind power
Microsoft Office Word 远程命令执行漏洞(CVE-2022-30190)分析与利用
Three years of continuous decline in revenue, Tiandi No. 1 is trapped in vinegar drinks
Construction of occupancy grid map
剑指 Offer II 027. 回文链表