Terkadang kita perlu membuat gambar dengan menggabungkan beberapa gambar JPG menjadi satu. Artikel ini menjelaskan cara melakukannya di C#.
- C# Imaging API – Unduhan Gratis
- Menggabungkan Gambar JPG Secara Horizontal dalam C#
- Menggabungkan Gambar JPG Secara Vertikal dalam C#
- Menggabungkan Gambar JPG ke dalam PDF dalam C#
- Menggabungkan Gambar JPG ke dalam PNG dalam C#
C# Imaging API – Unduh Gratis
Aspose.Imaging for .NET menyediakan sejumlah rutinitas fleksibel untuk membuat dan memanipulasi gambar dalam aplikasi .NET. Ini memungkinkan Anda menggabungkan file JPG dalam beberapa baris kode. Anda dapat menginstalnya dalam aplikasi .NET menggunakan NuGet atau unduh DLL API.
Install-Package Aspose.Imaging
Menggabungkan Gambar JPG Secara Horizontal di C#
Berikut adalah langkah-langkah untuk menggabungkan gambar JPEG secara horizontal di C#.
- Buat larik gambar JPEG yang ingin Anda gabungkan.
- Hitung lebar gambar yang dihasilkan dengan menambahkan lebar semua gambar dalam larik dan tinggi dengan mencari tinggi maksimum gambar dalam larik.
- Buat gambar baru menggunakan kelas JpegImage dan atur lebar dan tingginya ke yang dihitung pada langkah terakhir.
- Ulangi array gambar (Anda ingin menggabungkan) dan lakukan tugas berikut pada setiap gambar:
- Memuat piksel gambar menggunakan metode LoadArgb32Pixels dan menyimpannya dalam gambar yang dihasilkan menggunakan metode SaveArgb32Pixels. Metode ini juga menggunakan objek Rectangle sebagai argumen yang menentukan posisi gambar pada gambar yang dihasilkan.
- Simpan gambar yang dihasilkan sebagai gambar JPEG.
Contoh kode berikut menunjukkan cara menggabungkan gambar JPEG secara horizontal di C#.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
using System; using System.Collections.Generic; using System.IO; using System.Linq; using Aspose.Imaging; using Aspose.Imaging.FileFormats.Jpeg; using Aspose.Imaging.ImageOptions; using Aspose.Imaging.Sources; // Array of images you want to merge. string[] imagePaths = { "Image1.jpeg", "Image2.jpg" }; // Name of the output/resulting file. string outputPath = "MergeImages_Horizontally.jpg"; // Getting resulting image size. List<Size> imageSizes = new List<Size>(); foreach (string imagePath in imagePaths) { using (RasterImage image = (RasterImage)Image.Load(imagePath)) { imageSizes.Add(image.Size); } } // Width and height of the resulting image int newWidth = imageSizes.Sum(size => size.Width); int newHeight = imageSizes.Max(size => size.Height); // Merging images into one. Source tempFileSource = new FileCreateSource("temp.jpg", isTemporal: true); JpegOptions options = new JpegOptions() { Source = tempFileSource, Quality = 100 }; using (JpegImage newImage = (JpegImage)Image.Create(options, newWidth, newHeight)) { int stitchedWidth = 0; foreach (string imagePath in imagePaths) { using (RasterImage image = (RasterImage)Image.Load(imagePath)) { Rectangle bounds = new Rectangle(stitchedWidth, 0, image.Width, image.Height); newImage.SaveArgb32Pixels(bounds, image.LoadArgb32Pixels(image.Bounds)); stitchedWidth += image.Width; } } newImage.Save(outputPath); }

Gambar Keluaran

Menggabungkan Gambar JPG Secara Vertikal di C#
Langkah-langkah untuk menggabungkan gambar JPEG secara vertikal sama seperti di atas. Perbedaan kecilnya adalah kami menghitung tinggi gambar yang dihasilkan dengan menambahkan tinggi semua gambar dalam larik dan lebar dengan mencari lebar maksimum gambar dalam larik. Contoh kode berikut menunjukkan cara menggabungkan gambar JPEG secara vertikal di C#.
// An array of images you want to merge | |
string[] imagePaths = { "Image1.jpeg", "Image2.jpg" }; | |
string outputPath = "MergeImages_Vertically.jpg"; | |
// Getting resulting image size. | |
List<Size> imageSizes = new List<Size>(); | |
foreach (string imagePath in imagePaths) | |
{ | |
using (RasterImage image = (RasterImage)Image.Load(imagePath)) | |
{ | |
imageSizes.Add(image.Size); | |
} | |
} | |
// Width and Height of the resulting image | |
int newWidth = imageSizes.Max(size => size.Width); | |
int newHeight = imageSizes.Sum(size => size.Height); | |
// Merging images into one. | |
using (MemoryStream memoryStream = new MemoryStream()) | |
{ | |
StreamSource outputStreamSource = new StreamSource(memoryStream); | |
JpegOptions options = new JpegOptions() { Source = outputStreamSource, Quality = 100 }; | |
using (JpegImage newImage = (JpegImage)Image.Create(options, newWidth, newHeight)) | |
{ | |
int stitchedHeight = 0; | |
foreach (string imagePath in imagePaths) | |
{ | |
using (RasterImage image = (RasterImage)Image.Load(imagePath)) | |
{ | |
Rectangle bounds = new Rectangle(0, stitchedHeight, image.Width, image.Height); | |
newImage.SaveArgb32Pixels(bounds, image.LoadArgb32Pixels(image.Bounds)); | |
stitchedHeight += image.Height; | |
} | |
} | |
newImage.Save(outputPath); | |
} | |
} |

Menggabungkan Gambar JPG ke dalam PDF di C#
Anda mungkin perlu menggabungkan gambar JPEG ke dalam PDF. Anda dapat melakukan ini hanya dengan membuat sedikit perubahan pada metode Image.Save (gunakan ekstensi .pdf alih-alih .jpg).
// An array of images you want to merge. | |
string[] imagePaths = { "Image1.jpeg", "Image2.jpg" }; | |
string outputPath = "MergeHorizontalAsPDF"; | |
string tempFilePath = "temp.jpg"; | |
// Getting resulting image size. | |
List<Size> imageSizes = new List<Size>(); | |
foreach (string imagePath in imagePaths) | |
{ | |
using (RasterImage image = (RasterImage)Image.Load(imagePath)) | |
{ | |
imageSizes.Add(image.Size); | |
} | |
} | |
// Width and height of the resulting image. | |
int newWidth = imageSizes.Sum(size => size.Width); | |
int newHeight = imageSizes.Max(size => size.Height); | |
// Merging images into one. | |
Source tempFileSource = new FileCreateSource(tempFilePath, isTemporal: true); | |
JpegOptions options = new JpegOptions() { Source = tempFileSource, Quality = 100 }; | |
using (JpegImage newImage = (JpegImage)Image.Create(options, newWidth, newHeight)) | |
{ | |
int stitchedWidth = 0; | |
foreach (string imagePath in imagePaths) | |
{ | |
using (RasterImage image = (RasterImage)Image.Load(imagePath)) | |
{ | |
Rectangle bounds = new Rectangle(stitchedWidth, 0, image.Width, image.Height); | |
newImage.SaveArgb32Pixels(bounds, image.LoadArgb32Pixels(image.Bounds)); | |
stitchedWidth += image.Width; | |
} | |
} | |
newImage.Save(outputPath + ".pdf", new PdfOptions()); | |
} |
Menggabungkan Gambar JPG menjadi PNG di C#
Demikian pula, Anda mungkin ingin menggabungkan gambar JPEG ke dalam PNG. Seperti yang ditunjukkan di atas, Anda hanya perlu menggunakan ekstensi .png alih-alih .jpg dalam metode Image.Save.
// An array of images you want to merge. | |
string[] imagePaths = { "Image1.jpeg", "Image2.jpg" }; | |
string outputPath = "MergeHorizontalAsPNG"; | |
// Getting resulting image size. | |
List<Size> imageSizes = new List<Size>(); | |
foreach (string imagePath in imagePaths) | |
{ | |
using (RasterImage image = (RasterImage)Image.Load(imagePath)) | |
{ | |
imageSizes.Add(image.Size); | |
} | |
} | |
// Width and height of the resulting image. | |
int newWidth = imageSizes.Sum(size => size.Width); | |
int newHeight = imageSizes.Max(size => size.Height); | |
// Merging images into one and save as PNG | |
using (MemoryStream memoryStream = new MemoryStream()) | |
{ | |
StreamSource outputStreamSource = new StreamSource(memoryStream); | |
JpegOptions options = new JpegOptions() { Source = outputStreamSource, Quality = 100 }; | |
using (JpegImage newImage = (JpegImage)Image.Create(options, newWidth, newHeight)) | |
{ | |
int stitchedWidth = 0; | |
foreach (string imagePath in imagePaths) | |
{ | |
using (RasterImage image = (RasterImage)Image.Load(imagePath)) | |
{ | |
Rectangle bounds = new Rectangle(stitchedWidth, 0, image.Width, image.Height); | |
newImage.SaveArgb32Pixels(bounds, image.LoadArgb32Pixels(image.Bounds)); | |
stitchedWidth += image.Width; | |
} | |
} | |
newImage.Save(outputPath + ".png", new PngOptions()); | |
} | |
} |
Kesimpulan
Di artikel ini, Anda belajar cara menggabungkan gambar JPEG di C#. Anda dapat menggabungkannya secara horizontal atau vertikal. Anda juga belajar cara menyimpan gambar gabungan sebagai PDF atau PNG. Untuk informasi lebih lanjut, silakan periksa dokumentasi dari Aspose.Imaging untuk .NET. Jika Anda memiliki pertanyaan, jangan ragu untuk menghubungi kami di Forum Dukungan kami.