C#中如何调整图像大小
2016-10-12 14:42
417 查看
在本篇文章中,我将介绍如何在C#中来调整你想要的图像大小。要实现这一目标,我们可以采取以下几个步骤:
1.首先要获取你想要调整大小的图像:
2.将图像转换为Bitmap:
3.创建一个调整图像大小的方法:
在上面的方法中,我们获取了位图图像,然后绘制了不同尺寸的图像(这里绘制出的图像是基于指定的纵横比)
4.调用上述方法,得到调整大小之后的图片:
输出结果:
谢谢浏览,希望对你有所帮助。
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));
输出结果:
谢谢浏览,希望对你有所帮助。
相关文章推荐
- C#中如何调整图像大小
- c#禁止Form窗口调整大小,如何禁止combobox输入
- 如何调整图像大小
- c#如何禁止Form窗口调整大小?如何禁止combobox输入?
- Visio如何调整锁定图像大小
- C# ppc中,如何正确保存调整大小后的图片?
- Visio如何调整锁定图像大小
- C#图像处理(各种旋转、改变大小、柔化、锐化、雾化、底片、浮雕、黑白、滤镜效果,滤波,图像截取) 对图片的处理 : 亮度调整 抓屏 翻转 随鼠标画矩形
- C# 如何在空间运行时调整控件位置和大小
- .net,C#如何在运行时通过鼠标拖动改变控件的大小
- c# 如何调整 Button 控件在 Enable = false 时的字体颜色
- 如何在C#中调整控件的布局(原创)
- 如何开发类视于CTrackerRect的拖拉框进行图像的调整
- 再学 GDI+[87]: TGPImage(7) - 调整图像大小
- 关于如何调整虚拟磁盘大小的问题总结
- C#如何限制程序内存使用的大小
- 如何使用C#去灰度化一幅图像
- [VB.NET]请问在VB.NET中如何把两幅大小相同的图像左右或上下拼接成一张整图呢!
- C#调用API调整应用程序占用物理内存大小
- C#的WinForm中如何让窗体大小不能改变