本快速教程解释了如何在配置设置和任务的分步列表的帮助下在 C# 中旋转图像以实现此功能。在使用C#旋转位图操作时,将演示用户指定的角度以及设置由于图像旋转而暴露的表面的背景颜色的选项。您还将学习同时旋转和翻转任何类型的图像,如 BMP、PNG、JPEG 等。
在 C# 中旋转图像的步骤
- 建立环境以将 Nuget 包管理器中的 Aspose.Imaging 添加到应用程序
- 将源图像加载到要旋转的 Image 类对象中
- 将图像投射到 RasterImage
- 将图像缓存到内存中以在转换期间获得更好的性能
- 将图像旋转 30 度并使用 RasterImage.Rotate 函数将背景颜色设置为绿色
- 使用 RasterImage.RotateFlip 函数在 Y 轴上翻转图像
- 将修改后的图像另存为 BMP
通过使用 C# 按角度旋转图像 操作在环境配置的帮助下进行了详细描述,然后为该功能执行了一系列步骤。目标 BMP 图像作为 RasterImage 加载,具有不同的方法来旋转和翻转图像。图像首先被缓存以获得性能,然后在将其保存回磁盘之前旋转和翻转。
在 C# 中旋转图像的代码
using Aspose.Imaging; | |
namespace RotateImageInCSharp | |
{ | |
class Program | |
{ | |
static void Main(string[] args) // Main function to rotate image in C# | |
{ | |
// Create and load license to rotate the image | |
License licRotateImage = new License(); | |
licRotateImage.SetLicense("Aspose.Imaging.lic"); | |
// Load the source BMP image to Image class object and then cast to RasterImage | |
using (RasterImage rasterImage = (RasterImage)Image.Load("SampleImageToRotate.bmp")) | |
{ | |
// Check if the loaded raster image is not cached | |
if (!rasterImage.IsCached) | |
{ | |
// Cache the image into memory | |
rasterImage.CacheData(); | |
} | |
// Rotate the image at 30 degrees, set flag to resize image proportionally, | |
// and set the background color of the blank space as Green | |
rasterImage.Rotate(30f, true, Color.Green); | |
// Flip the image on Y-Axis | |
rasterImage.RotateFlip(RotateFlipType.RotateNoneFlipY); | |
// Save the rotated and flipped image | |
rasterImage.Save("RotatedImage_out.bmp"); | |
} | |
System.Console.WriteLine("Done"); | |
} | |
} | |
} |
- 旋转位图 C#* 代码演示了使用 RasterImage.Rotate 函数将图像旋转一定角度。您可以为旋转源图像时留空的空间设置背景颜色。此外,您还可以借助 RasterImage.RotateFlip 函数翻转图像,该函数需要 RotateFlipType 枚举器,其中包含诸如 Rotate90FlipX 之类的值以旋转 90 度然后围绕 X 轴翻转,Rotate90FlipY 以旋转 90 度并围绕 Y 轴翻转同样有 16 种不同的旋转和翻转选项。
在本教程中,我们学习了如何在 C# 中旋转图像,但是如果您想学习如何调整图像大小,请参阅 如何在 C# 中调整图像大小 上的文章。