博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#中如何调整图像大小
阅读量:7081 次
发布时间:2019-06-28

本文共 1365 字,大约阅读时间需要 4 分钟。

在本篇文章中,我将介绍如何在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));

 输出结果:

谢谢浏览,希望对你有所帮助。

转载地址:http://xxvml.baihongyu.com/

你可能感兴趣的文章
学习该有的思维方式
查看>>
RColorBrewer的使用
查看>>
http协议基础(一)
查看>>
好看的电影-电视剧
查看>>
Linux:查看磁盘空间占用情况
查看>>
redis发布订阅
查看>>
dubbo+zookeeper
查看>>
ZOJ 3642 Just Another Information Sharing Problem【二分图多重匹配】
查看>>
Ansible基础
查看>>
3D打印材料的发展现状(1)
查看>>
API相关基础知识
查看>>
黑马程序员-面向对象-07天-1 (抽象类描述)
查看>>
elasticsearch-hadoop使用示例
查看>>
Vue渐变淡入淡出的轮播图
查看>>
svn+http+ad域
查看>>
php----------php安装xhprof扩展和简单使用
查看>>
NIO服务器
查看>>
Spring配置文件的读取
查看>>
Oracle中日期时间的操作比较和加减-入门基础(转)
查看>>
使用工具安装,运行,停止,卸载Window服务
查看>>