当前位置:网站首页>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 .
边栏推荐
- Application of point cloud intelligent drawing in intelligent construction site
- 海思3559 sample解析:vio
- 年后求职找B端产品经理?差点把自己坑惨了......
- 57. 插入区间
- [distillation] pointdistiller: structured knowledge distillationwards efficient and compact 3D detection
- Getting started with OpenMP
- Five causes of PCB board deformation and six solutions 2021-10-08
- smartBugs安装小问题总结
- "Spatial transformation" significantly improves the quality of ground point extraction of cliff point cloud
- 2160. 拆分数位后四位数字的最小和
猜你喜欢

Home environment monitoring system design (PC version) (mobile app version to be determined)

基于STM32MP157调试MIPI-DSI屏幕

How to use ad wiring for PCB design?
![Insert and sort the linked list [dummy unified operation + broken chain core - passive node]](/img/2a/ccb1145d2b4f9fbd8d0812deace93b.png)
Insert and sort the linked list [dummy unified operation + broken chain core - passive node]

Chuantu microelectronics high speed and high performance rs-485/422 transceiver series

一“石”二“鸟”,PCA有效改善机载LiDAR林下地面点部分缺失的困局

微信小程序开通客服消息功能开发
![不同路径II[针对DFS的动态规划改进]](/img/bb/1e1cee22b9de954de242d299a1a0eb.png)
不同路径II[针对DFS的动态规划改进]

Terms and concepts related to authority and authentication system

npm install 报错 : gyp ERR! configure error
随机推荐
C reads XML on the web
How much do you know about electronic components on PCB?
单位转换-毫米转像素-像素转毫米
个人域名和企业域名的区别
传统的IO存在什么问题?为什么引入零拷贝的?
Share the process requirements for single-layer flexible circuit board
判断用户是否是第一次进入某个页面
点云智绘在智慧工地中的应用
Advantages and differences of three kinds of vias in PCB 2021-10-27
年后求职找B端产品经理?差点把自己坑惨了......
The fourth floor is originally the fourth floor. Let's have a look
ts环境搭建
Chuantu microelectronics ca-if1051 can-fd transceiver
C#入门教程
Modular programming of LCD1602 LCD controlled by single chip microcomputer
RTKLIB-b33版本中GALILEO广播星历存储问题
C get the version number of exe - file version and assembly version
力扣78:子集
Getting started with OpenMP
Estimation of dense forest volume based on LIDAR point cloud with few ground points