การหมุนภาพด้วยมุมที่กําหนดเองด้วยพื้นหลังที่โปร่งใสเป็นความต้องการที่พบบ่อยสําหรับ galeries, dashboards, และเครื่องมือการออกแบบใน .NET. Aspose.Imaging for .Net ให้ API ฯลฯ เพื่อโหลดภาพโดยตรง, รุนได้ตามมุมที่คุณกําหนดไว้และบันทึกไว้ในรูปแบบที่สนับสนุนความโปรดใส
ตัวอย่างที่สมบูรณ
// File: Program.cs
// Requires NuGet: Aspose.Imaging
using System;
using System.Collections.Generic;
using System.IO;
using Aspose.Imaging;
using Aspose.Imaging.ImageOptions;
namespace RotateTransparentDemo
{
public static class Program
{
public static int Main(string[] args)
{
try
{
if (args.Length == 0)
{
// Single-file demo paths. Replace with your own files or pass CLI args.
var input = "input.png"; // source can be PNG, JPG, etc.
var output = "rotated-output.png"; // must be PNG to preserve transparency
RotateSingle(input, output, angleDegrees: 17f, expandCanvas: true);
Console.WriteLine($"Saved: {output}");
return 0;
}
// CLI:
// 1) Single file:
// RotateTransparentDemo.exe <inputPath> <outputPath> <angleDegrees> [expandCanvas:true|false]
//
// 2) Batch folder:
// RotateTransparentDemo.exe --batch <inputDir> <outputDir> <angleDegrees> [expandCanvas:true|false]
if (args[0].Equals("--batch", StringComparison.OrdinalIgnoreCase))
{
if (args.Length < 4)
{
Console.Error.WriteLine("Usage: --batch <inputDir> <outputDir> <angleDegrees> [expandCanvas:true|false]");
return 2;
}
var inputDir = args[1];
var outputDir = args[2];
var angle = float.Parse(args[3]);
var expandCanvas = args.Length >= 5 ? bool.Parse(args[4]) : true;
BatchRotate(inputDir, outputDir, angle, expandCanvas);
Console.WriteLine($"Batch complete. Output in: {outputDir}");
return 0;
}
else
{
if (args.Length < 3)
{
Console.Error.WriteLine("Usage: <inputPath> <outputPath> <angleDegrees> [expandCanvas:true|false]");
return 2;
}
var inputPath = args[0];
var outputPath = args[1];
var angle = float.Parse(args[2]);
var expandCanvas = args.Length >= 4 ? bool.Parse(args[3]) : true;
RotateSingle(inputPath, outputPath, angle, expandCanvas);
Console.WriteLine($"Saved: {outputPath}");
return 0;
}
}
catch (Exception ex)
{
Console.Error.WriteLine("Error: " + ex.Message);
return 1;
}
}
/// <summary>
/// Rotates a single image by an arbitrary angle and preserves transparency in the output PNG.
/// </summary>
private static void RotateSingle(string inputPath, string outputPath, float angleDegrees, bool expandCanvas)
{
if (!File.Exists(inputPath))
throw new FileNotFoundException("Input image not found.", inputPath);
// If you expect large images or batches, you can enable disk cache to reduce RAM pressure:
// Aspose.Imaging.Cache.CacheType = Aspose.Imaging.Cache.CacheType.CacheOnDisk;
// Aspose.Imaging.Cache.CacheFolder = Path.GetFullPath(".imaging-cache");
// Aspose.Imaging.Cache.CacheSize = 512L * 1024 * 1024; // 512 MB
using (var image = Image.Load(inputPath))
{
// Raster operations are available on RasterImage
if (image is not RasterImage raster)
throw new InvalidOperationException("Loaded image is not a raster image.");
// Rotate by an arbitrary angle. Set expandCanvas to true to avoid clipping.
// Color.Transparent keeps the background transparent for alpha-capable formats.
raster.Rotate(angleDegrees, expandCanvas, Color.Transparent);
// Save as PNG to preserve alpha channel
var pngOptions = new PngOptions
{
// Most cases do not need explicit color type; leaving defaults preserves alpha.
// Uncomment if you want to force RGBA:
// ColorType = Aspose.Imaging.FileFormats.Png.PngColorType.TruecolorWithAlpha
};
// Ensure output directory exists
Directory.CreateDirectory(Path.GetDirectoryName(Path.GetFullPath(outputPath)) ?? ".");
raster.Save(outputPath, pngOptions);
}
}
/// <summary>
/// Rotates all supported images in a folder and writes PNG outputs with transparency preserved.
/// </summary>
private static void BatchRotate(string inputDir, string outputDir, float angleDegrees, bool expandCanvas)
{
if (!Directory.Exists(inputDir))
throw new DirectoryNotFoundException("Input directory not found: " + inputDir);
Directory.CreateDirectory(outputDir);
// Common raster extensions. Adjust as needed.
var extensions = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
{
".png", ".jpg", ".jpeg", ".bmp", ".gif", ".tif", ".tiff", ".webp"
};
var files = Directory.GetFiles(inputDir, "*.*", SearchOption.AllDirectories);
foreach (var file in files)
{
var ext = Path.GetExtension(file);
if (!extensions.Contains(ext))
continue;
var rel = Path.GetRelativePath(inputDir, file);
var relBase = Path.ChangeExtension(rel, ".png"); // output is PNG to keep transparency
var outPath = Path.Combine(outputDir, relBase);
Directory.CreateDirectory(Path.GetDirectoryName(Path.GetFullPath(outPath)) ?? ".");
try
{
RotateSingle(file, outPath, angleDegrees, expandCanvas);
Console.WriteLine($"OK {rel} -> {relBase}");
}
catch (Exception ex)
{
Console.WriteLine($"ERR {rel}: {ex.Message}");
}
}
}
}
}
- วิธีการทํางาน *
Image.Loadเปิดแหล่งที่มา รหัสตรวจสอบและนําไปสู่RasterImageสําหรับการดําเนินงานเรสเตอร์raster.Rotate(angle, expandCanvas, Color.Transparent)การหมุนตามค่าใด ๆ- การประหยัดด้วย
PngOptionsรักษาความโปร่งใส - โหมดแบทช์เดินในต้นกล่องหมุนรูปแบบที่ได้รับการสนับสนุนและเขียน PNGs
คู่มือขั้นตอน
1) ดาวน์โหลดภาพ
ใช Image.Load เปิดไฟล์แหล่งกําเนิด Cast to RasterImage หากคุณต้องการสมาชิกที่เฉพาะอย่างยิ่ง
using (var image = (RasterImage)Image.Load(inputPath))
{
// work with the image here
}
2) การหมุนด้วยมุมใด ๆ
โทรศัพท Rotate(angle, expandCanvas, backgroundColor).
angleมีระดับexpandCanvas = trueการหลีกเลี่ยงการคลิกโดยการรีไซเคิลแถบเพื่อให้เหมาะกับขอบหมุนColor.Transparentรักษาพื้นหลังที่โปร่งใสหากรูปแบบการส่งออกรองรับ alpha
image.Rotate(17, true, Color.Transparent);
3) บันทึกในรูปแบบที่สนับสนุนความโปร่งใส
PNG สนับสนุนความโปร่งใส ใช้ PngOptions เพื่อบันทึกการส่งออกแบบหมุนด้วยช่องอัลฟาที่เก็บไว้
image.Save(outputPath, new PngOptions());
ตัวเลือกทางปฏิบัติ
- หลีกเลี่ยงการคลิก: ชุด
expandCanvas = trueเมื่อหมุนด้วยมุมที่ไม่ซ้าย - พื้นหลังที่ซับซ้อน: ผ่านความแข็งแกร่ง
Colorแทนที่จะColor.Transparentถ้าคุณต้องการพื้นหลังที่เต็มไปด้วย - การผลิตที่ไม่เสียหาย: ปรับปรุง PNG สําหรับ UI, โลโก้และพื้นผิวที่ต้องการขอบคริสต์และความโปร่งใส
- รูปภาพ: หากคุณไม่ต้องการความโปร่งใสการประหยัดเป็น JPEG สามารถลดขนาดไฟล์
คอมเพลิดเพลินและ Fixes
พื้นหลังโปร่งใสที่สูญเสียการบันทึกด้วย
PngOptionsหรือรูปแบบอื่นที่สนับสนุน alpha JPEG ไม่สนับสนุนความโปร่งใสชิ้นส่วนของภาพหายไปหลังจากหมุนใช
expandCanvas = trueการเติบโตของถังเพื่อให้เนื้อหาที่หมุนพอดีขอบที่จอดหลังการหมุนการหมุนใช้ข้อกําหนดของห้องสมุด หากแหล่งข้อมูลของคุณมีความละเอียดต่ําโปรดพิจารณาการรีไซเคิล input ไปยังความละเอียดสูงก่อนที่จะ rotate จากนั้นลดลงหลังจากบันทึกเพื่อลดรายการที่มองเห็น
หน่วยความจําและภาพขนาดใหญ่จัดภาพได้อย่างรวดเร็วและหลีกเลี่ยงการโหลดไฟล์ขนาดใหญ่จํานวนมากในเวลาเดียวกัน การตั้งค่าการ caching ของไดรฟ์สําหรับชิ้นส่วนที่ใหญ่หากจําเป็น
รายการตรวจสอบขนาดเล็ก
- โหลดด้วย
Image.Load - รูตเตอร์กับ
image.Rotate(angle, true, Color.Transparent) - การบันทึกด้วย
new PngOptions()สําหรับความโปร่งใส - ใช้พื้นหลังที่แข็งแกร่งหากความโปร่งใสไม่จําเป็น
- การยืนยันการส่งออกอย่างมองเห็นสําหรับสินทรัพย์หลัก
FAQ
รูปแบบที่รักษาความโปร่งใสหลังจากหมุนPNG และรูปแบบอื่น ๆ ที่สามารถใช้ได้ อัลฟา JPEG ไม่รักษาความโปร่งใส
** ฉันจะหมุนได้อย่างไรโดยไม่ต้องขยายสายเคเบิล**Pass expandCanvas = false. เนื้อหานอกเหนือจากขอบเขตเดิมจะถูกคลิก
ฉันสามารถหมุนได้จากมุมใด ๆใช่ พารามิเตอร์มุมยอมรับค่าระดับดิจิตอลใด ๆ
More in this category
- การเพิ่มประสิทธิภาพของ GIF ของ animated ใน .NET โดยใช้ Aspose.Imaging
- Optimize Multi-Page TIFFs for Archival in .NET ด้วย Aspose
- HEIC ไปยัง JPEG / PNG แปลงด้วย Aspose.Imaging สําหรับ .NET
- การเคลื่อนไหวที่ขับเคลื่อนข้อมูลใน .NET ด้วย Aspose.Imaging
- การบีบอัดภาพที่มีคุณภาพและไม่มีการสูญเสียใน .NET ด้วย Aspose.Imaging