阅读量:2
在C#中,要调整图像的大小,您可以使用System.Drawing命名空间中的Bitmap类。以下是一个简单的示例,说明如何调整图像的大小:
using System;
using System.Drawing;
namespace ResizeImageExample
{
class Program
{
static void Main(string[] args)
{
// 加载图像
string imagePath = "path/to/your/image.jpg";
using (Image originalImage = Image.FromFile(imagePath))
{
// 设置新的图像大小
int newWidth = 300;
int newHeight = 200;
// 创建一个新的Bitmap对象,用于存储调整大小后的图像
using (Bitmap resizedImage = new Bitmap(newWidth, newHeight))
{
// 使用Graphics对象绘制调整大小后的图像
using (Graphics graphics = Graphics.FromImage(resizedImage))
{
// 设置绘图质量
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.CompositingMode = CompositingMode.SourceCopy;
graphics.SmoothingMode = SmoothingMode.HighQuality;
// 绘制原始图像到新的Bitmap对象上,并设置新大小
graphics.DrawImage(originalImage, 0, 0, newWidth, newHeight);
}
// 保存调整大小后的图像
string outputPath = "path/to/your/resized_image.jpg";
resizedImage.Save(outputPath);
}
}
}
}
}
在这个示例中,我们首先加载原始图像,然后设置新的宽度和高度。接下来,我们创建一个新的Bitmap对象,用于存储调整大小后的图像。我们使用Graphics对象绘制原始图像到新的Bitmap对象上,并设置新大小。最后,我们保存调整大小后的图像。
以上就是关于“c# drawimage图像大小如何调整”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm