当前位置:网站首页>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 .
边栏推荐
- How to use ad wiring for PCB design?
- 年后求职找B端产品经理?差点把自己坑惨了......
- Tupu software digital twin 3D wind farm, offshore wind power of smart wind power
- Keil and Proteus joint commissioning
- 基于地面点稀少的LiDAR点云的茂密森林蓄积量估算
- Pytorch遇到的坑:为什么模型训练时,L1loss损失无法下降?
- Construction of occupancy grid map
- What if there is no point in data visualization?
- 差点被这波Handler 面试连环炮带走~
- 國外LEAD域名郵箱獲取途徑
猜你喜欢

Ns32f103c8t6 can perfectly replace stm32f103c8t6

Modular programming of oled12864 display controlled by single chip microcomputer

Summary of small problems in smartbugs installation

【QT】Qt 5 的程序:打印文档

NPM install reports an error: gyp err! configure error

Pcb|about FPC reinforcement type

Elk + filebeat log parsing, log warehousing optimization, logstash filter configuration attribute

The method of judging whether triode can amplify AC signal

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

"Spatial transformation" significantly improves the quality of ground point extraction of cliff point cloud
随机推荐
Audio (V) audio feature extraction
China Mobile MCU product information
One "stone" and two "birds", PCA can effectively improve the dilemma of missing some ground points under the airborne lidar forest
音频(五)音频特征提取
【日常训练】207. 课程表
Modular programming of oled12864 display controlled by single chip microcomputer
VectorDraw Web Library 10.10
Leetcode daily question - 515 Find the maximum value in each tree row
Find out what informatization is, and let enterprises embark on the right path of transformation and upgrading
1742. 盒子中小球的最大数量
Different paths ii[dynamic planning improvement for DFS]
【QT】qtcreator便捷快捷键以及QML介绍
判断用户是否是第一次进入某个页面
WinForm实现窗口始终在顶层
[batch dos-cmd command - summary and summary] - file and directory operation commands (MD, RD, xcopy, dir, CD, set, move, copy, del, type, sort)
Chuantu microelectronics ca-if1051 can-fd transceiver
Application scheme | application of Sichuan earth microelectronics ca-is398x in PLC field
基于地面点稀少的LiDAR点云的茂密森林蓄积量估算
年后求职找B端产品经理?差点把自己坑惨了......
力扣78:子集