当前位置:网站首页>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 .
边栏推荐
- Usememo simulation usecallback
- 剑指 Offer II 027. 回文链表
- Keil and Proteus joint commissioning
- STL教程4-输入输出流和对象序列化
- lebel只想前面有星号,但是不想校验
- VectorDraw Developer Framework 10.10
- Sichuan earth microelectronics high performance, high integration and low cost isolated 485 transceiver
- C get the version number of exe - file version and assembly version
- 用函数的递归来解决几道有趣的题
- Bicubic difference
猜你喜欢

realsense d455 semantic_ Slam implements semantic octree mapping

微信小程序开通客服消息功能开发

Estimation of dense forest volume based on LIDAR point cloud with few ground points

Sichuan earth microelectronics 8-channel isolated digital input receiver

Six causes of PCB disconnection 2021-10-20

The method of judging whether triode can amplify AC signal

SCM Project Training

“空间转换”显著提升陡崖点云的地面点提取质量

消息中间件之ActiveMQ的基本使用

Four software 2021-10-14 suitable for beginners to draw PCB
随机推荐
AttributeError: ‘Upsample‘ object has no attribute ‘recompute_scale_factor‘
【日常训练】207. 课程表
Cglib dynamic proxy
微信小程序入门记录
Sichuan earth microelectronics 8-channel isolated digital input receiver
27. 移除元素
不同路径II[针对DFS的动态规划改进]
Microsoft Office Word 远程命令执行漏洞(CVE-2022-30190)分析与利用
1464. 数组中两元素的最大乘积
Different paths ii[dynamic planning improvement for DFS]
C#获取exe的版本号-文件版本and程序集版本
What if there is no point in data visualization?
单位转换-毫米转像素-像素转毫米
Keil and Proteus joint commissioning
Collection of common terms and meanings in forestry investigation based on lidar
Getting started with OpenMP
c# winform panel自定义图片和文字
Sichuan Tuwei ca-is3105w fully integrated DC-DC converter
Modular programming of oled12864 display controlled by single chip microcomputer
Chuantu microelectronics ca-if1051 can-fd transceiver