Menggambar Gambar dengan Grafik

Menggambar Gambar dengan Grafik

Dengan perpustakaan Aspose.PSD Anda dapat menggambar bentuk-bentuk sederhana seperti garis, persegi panjang, dan lingkaran, serta bentuk-bentuk kompleks seperti poligon, kurva, busur, dan bentuk Bezier. Perpustakaan Aspose.PSD menciptakan bentuk-bentuk tersebut menggunakan kelas Grafik yang berada di dalam ruang nama Aspose.PSD. Objek Grafik bertanggung jawab untuk melakukan berbagai operasi menggambar pada gambar, sehingga mengubah permukaan gambar. Kelas Grafik menggunakan berbagai objek pembantu untuk meningkatkan bentuk-bentuk tersebut:

  • Pens, untuk menggambar garis, menggambar garis luar bentuk, atau merender representasi geometris lainnya.
  • Kuas, untuk mendefinisikan bagaimana area diisi.
  • Font, untuk mendefinisikan bentuk karakter teks.

Menggambar dengan Kelas Grafik

Berikut adalah contoh kode yang menunjukkan penggunaan kelas Grafik. Kode sumber contoh telah dibagi menjadi beberapa bagian agar tetap sederhana dan mudah diikuti. Langkah demi langkah, contoh-contoh menunjukkan cara untuk:

  1. Membuat sebuah gambar.
  2. Membuat dan menginisialisasi objek Grafik.
  3. Menghapus permukaan.
  4. Menggambar sebuah elips.
  5. Menggambar sebuah poligon berisi dan menyimpan gambar.

Contoh Program

Membuat Gambar

Mulai dengan membuat gambar menggunakan salah satu metode yang dijelaskan dalam Menciptakan Berkas.

// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET
string loadpath = dataDir + "sample.psd";
string outpath = dataDir + "CoreDrawingFeatures.bmp";
// Create an instance of Image
using (PsdImage image = new PsdImage(loadpath))
{
// load pixels
var pixels = image.LoadArgb32Pixels(new Rectangle(0, 0, 100, 10));
for (int i = 0; i < pixels.Length; i++)
{
// specify pixel color value (gradient in this case).
pixels[i] = i;
}
// save modified pixels.
image.SaveArgb32Pixels(new Rectangle(0, 0, 100, 10), pixels);
// export image to bmp file format.
image.Save(outpath, new BmpOptions());
}

Membuat dan Menginisialisasi Objek Grafik

Kemudian membuat dan menginisialisasi objek Grafik dengan meneruskan objek Gambar ke konstruktornya.

// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET
string outpath = dataDir + "Arc.bmp";
// Create an instance of BmpOptions and set its various properties
BmpOptions saveOptions = new BmpOptions();
saveOptions.BitsPerPixel = 32;
// Create an instance of Image
using (Image image = new PsdImage(100, 100))
{
// Create and initialize an instance of Graphics class and clear Graphics surface
Graphics graphic = new Graphics(image);
graphic.Clear(Color.Yellow);
// Draw an arc shape by specifying the Pen object having red black color and coordinates, height, width, start & end angles
int width = 100;
int height = 200;
int startAngle = 45;
int sweepAngle = 270;
// Draw arc to screen and save all changes.
graphic.DrawArc(new Pen(Color.Black), 0, 0, width, height, startAngle, sweepAngle);
// export image to bmp file format.
image.Save(outpath, saveOptions);
}

Menghapus Permukaan

Menghapus permukaan Grafik dengan memanggil metode Bersih Grafik dan meneruskan warna sebagai parameter. Metode ini mengisi permukaan Grafik dengan warna yang dilewatkan sebagai argumen.

// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET
// Create an instance of Image
using (PsdImage image = new PsdImage(500, 500))
{
var graphics = new Graphics(image);
// Clear the image surface with white color and Create and initialize a Pen object with blue color
graphics.Clear(Color.White);
var pen = new Pen(Color.Blue);
// Draw Ellipse by defining the bounding rectangle of width 150 and height 100 also Draw a polygon using the LinearGradientBrush
graphics.DrawEllipse(pen, new Rectangle(10, 10, 150, 100));
using (var linearGradientBrush = new LinearGradientBrush(image.Bounds, Color.Red, Color.White, 45f))
{
graphics.FillPolygon(linearGradientBrush, new[] { new Point(200, 200), new Point(400, 200), new Point(250, 350) });
}
// export modified image.
image.Save(dataDir + "DrawingUsingGraphics_output.bmp", new BmpOptions());
}

Menggambar Elips

Anda mungkin melihat bahwa kelas Grafik telah mengekspos banyak metode untuk menggambar dan mengisi bentuk. Anda akan menemukan daftar lengkap metode dalam Referensi API Aspose.PSD untuk .NET. Ada beberapa versi metode DrawEllipse yang diekspos oleh kelas Grafik. Semua metode ini menerima objek Pens sebagai argumen pertamanya. Parameter-parameter berikutnya diteruskan untuk mendefinisikan persegi panjang pembatas sekitar elips. Untuk contoh ini, gunakan versi yang menerima objek Rectangle sebagai parameter kedua untuk menggambar sebuah elips menggunakan objek Pens dalam warna yang diinginkan.

// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET
string loadpath = dataDir + "sample.psd";
string outpath = dataDir + "CoreDrawingFeatures.bmp";
// Create an instance of Image
using (PsdImage image = new PsdImage(loadpath))
{
// load pixels
var pixels = image.LoadArgb32Pixels(new Rectangle(0, 0, 100, 10));
for (int i = 0; i < pixels.Length; i++)
{
// specify pixel color value (gradient in this case).
pixels[i] = i;
}
// save modified pixels.
image.SaveArgb32Pixels(new Rectangle(0, 0, 100, 10), pixels);
// export image to bmp file format.
image.Save(outpath, new BmpOptions());
}

Menggambar Poligon Berisi

Selanjutnya, gambar poligon menggunakan LinearGradientBrush dan sebuah array titik. Kelas Grafik telah mengekspos beberapa versi metode FillPolygon(). Semua ini menerima objek Brush sebagai argumen pertamanya, yang mendefinisikan karakteristik pengisian. Parameter kedua adalah sebuah array titik. Harap dicatat bahwa setiap dua titik berurutan dalam array menentukan sisi poligon.

// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET
// Create an instance of Image
using (PsdImage image = new PsdImage(500, 500))
{
var graphics = new Graphics(image);
// Clear the image surface with white color and Create and initialize a Pen object with blue color
graphics.Clear(Color.White);
var pen = new Pen(Color.Blue);
// Draw Ellipse by defining the bounding rectangle of width 150 and height 100 also Draw a polygon using the LinearGradientBrush
graphics.DrawEllipse(pen, new Rectangle(10, 10, 150, 100));
using (var linearGradientBrush = new LinearGradientBrush(image.Bounds, Color.Red, Color.White, 45f))
{
graphics.FillPolygon(linearGradientBrush, new[] { new Point(200, 200), new Point(400, 200), new Point(250, 350) });
}
// export modified image.
image.Save(dataDir + "DrawingUsingGraphics_output.bmp", new BmpOptions());
}

Menggambar Gambar dengan Grafik: Sumber Lengkap

// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET
// Create an instance of Image
using (PsdImage image = new PsdImage(500, 500))
{
var graphics = new Graphics(image);
// Clear the image surface with white color and Create and initialize a Pen object with blue color
graphics.Clear(Color.White);
var pen = new Pen(Color.Blue);
// Draw Ellipse by defining the bounding rectangle of width 150 and height 100 also Draw a polygon using the LinearGradientBrush
graphics.DrawEllipse(pen, new Rectangle(10, 10, 150, 100));
using (var linearGradientBrush = new LinearGradientBrush(image.Bounds, Color.Red, Color.White, 45f))
{
graphics.FillPolygon(linearGradientBrush, new[] { new Point(200, 200), new Point(400, 200), new Point(250, 350) });
}
// export modified image.
image.Save(dataDir + "DrawingUsingGraphics_output.bmp", new BmpOptions());
}

Semua kelas yang mengimplementasikan IDisposable dan mengakses sumber daya yang tidak dikelola diinisiasikan dalam pernyataan Using untuk memastikan bahwa mereka dibuang dengan benar.