在 C# 中缩放图像

本教程介绍如何在 C# 中缩放 image。它包含系统配置、逐步算法和可运行的示例代码,以C#** 中的高档图像。它还涵盖自定义属性和方法来自定义流程以满足您的要求。

在 C# 中缩放照片的步骤

  1. 配置系统以使用 Aspose.Drawing for .NET 来缩放图像
  2. 启动 Bitmap 类的实例
  3. 创建Graphics类的对象并设置插值模式
  4. 设置图像坐标和缩放因子
  5. 缩放输入并写入生成的图像

这些步骤详细阐述了在 C# 中缩放图像而不损失质量的算法。创建具有指定格式和大小的位图类对象。接下来,设置图像的比例并以 JPG 或 PNG 图像格式渲染生成的图像。

用 C# 编写高档图像代码

using System;
using Aspose.Drawing;
class Program
{
static void Main(string[] args)
{
License lic = new License();
lic.SetLicense("license.lic");
Bitmap image = new Bitmap("aspose-logo.png");
int newWidth = image.Width * 5;
int newHeight = image.Height * 5;
Bitmap bitmap = new Bitmap(newWidth, newHeight, Aspose.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.InterpolationMode = Aspose.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
// Scale the image 5x
Rectangle expansionRectangle = new Rectangle(0, 0, newWidth, newHeight);
graphics.DrawImage(image, expansionRectangle);
bitmap.Save("Scale.png");
System.Console.WriteLine("Image Scaled successfully");
}
}

此示例代码是一个基本版本,用于展示如何在 C# 中放大图像。通过设置不同的像素插值模式(例如双三次、双线性、默认等)来改进此代码。同样,您可以更改缩放因子以将图像大小调整为所需的高度和宽度。

本指南介绍了在 C# 中缩放照片的快速方法。另外,如果您对裁剪图像感兴趣,请参阅C# 中的图像裁剪上的文章。

 简体中文