下面是关于C#图片按比例缩放实例的详细攻略。
步骤一:导入命名空间和设置窗口
首先,在代码中导入必要的命名空间,以使用Image类和Bitmap类。
using System.Drawing;
using System.Drawing.Imaging;
接着,在窗口中添加PictureBox控件,用于显示缩放后的图片。在属性中将SizeMode设置为Zoom,让图片自适应PictureBox控件的大小。
步骤二:加载原始图片
使用Image.FromFile方法加载本地的原始图片。
Image image = Image.FromFile(@"C:\path\to\image.jpg");
步骤三:对图片进行缩放
按比例缩放图片的方法是,根据目标宽度和原始宽度的比例,计算出目标长度,然后根据目标长度和原始长度的比例,计算出目标高度。使用Bitmap类的SetResolution方法设置图片的分辨率,以免出现锐化过度的情况。
int width = pictureBox1.Width;
int height = pictureBox1.Height;
float ratio = (float)width / (float)image.Width;
int newWidth = (int)(image.Width * ratio);
int newHeight = (int)(image.Height * ratio);
Bitmap bmp = new Bitmap(newWidth, newHeight, PixelFormat.Format24bppRgb);
bmp.SetResolution(image.HorizontalResolution, image.VerticalResolution);
Graphics graphic = Graphics.FromImage(bmp);
graphic.SmoothingMode = SmoothingMode.HighQuality;
graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphic.DrawImage(image, new Rectangle(0, 0, newWidth, newHeight));
graphic.Dispose();
完成缩放后,将Bitmap对象转换为Image对象,并设置到PictureBox控件中。
pictureBox1.Image = bmp;
示例说明一
下面是一个完整的图片按比例缩放实例。在这个例子中,我们使用了WinForms应用程序和一个PictureBox控件。用PictureBox显示原始图片和缩放后的图片。
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Windows.Forms;
namespace ImageResize
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Image image = Image.FromFile(@"C:\path\to\image.jpg");
int width = pictureBox1.Width;
int height = pictureBox1.Height;
float ratio = (float)width / (float)image.Width;
int newWidth = (int)(image.Width * ratio);
int newHeight = (int)(image.Height * ratio);
Bitmap bmp = new Bitmap(newWidth, newHeight, PixelFormat.Format24bppRgb);
bmp.SetResolution(image.HorizontalResolution, image.VerticalResolution);
Graphics graphic = Graphics.FromImage(bmp);
graphic.SmoothingMode = SmoothingMode.HighQuality;
graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphic.DrawImage(image, new Rectangle(0, 0, newWidth, newHeight));
graphic.Dispose();
pictureBox1.Image = bmp;
}
}
}
示例说明二
有时,我们需要将多张图片按比例缩放后保存到本地。下面是一个例子,它可以加载多张图片,按比例缩放后将它们保存到指定文件夹。
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;
namespace ImageBatchResize
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var openFileDialog = new OpenFileDialog();
openFileDialog.Multiselect = true;
openFileDialog.Filter = "Image files (*.jpg, *.jpeg, *.png, *.bmp)|*.jpg;*.jpeg;*.png;*.bmp";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
var saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "Image files (*.jpg, *.jpeg, *.png, *.bmp)|*.jpg;*.jpeg;*.png;*.bmp";
saveFileDialog.InitialDirectory = @"C:\path\to\save\images";
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
foreach (var filePath in openFileDialog.FileNames)
{
Image image = Image.FromFile(filePath);
int width = pictureBox1.Width;
int height = pictureBox1.Height;
float ratio = (float)width / (float)image.Width;
int newWidth = (int)(image.Width * ratio);
int newHeight = (int)(image.Height * ratio);
Bitmap bmp = new Bitmap(newWidth, newHeight, PixelFormat.Format24bppRgb);
bmp.SetResolution(image.HorizontalResolution, image.VerticalResolution);
Graphics graphic = Graphics.FromImage(bmp);
graphic.SmoothingMode = SmoothingMode.HighQuality;
graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphic.DrawImage(image, new Rectangle(0, 0, newWidth, newHeight));
graphic.Dispose();
bmp.Save(Path.Combine(saveFileDialog.InitialDirectory, Path.GetFileName(filePath)), ImageFormat.Jpeg);
}
}
}
}
}
}
这个例子中,我们添加了一个OpenFileDialog控件,用于选择多个需要缩放的图片。然后,使用SaveFileDialog控件设置保存目录和文件名,并且使用Path类的Combine方法构建完整的文件路径。每次迭代中,我们都按比例缩放图片,并将缩放后的图片保存到本地。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:C#图片按比例缩放实例 - Python技术站