Grafikler Kullanarak Resimler Çizme

Aspose.PSD kütüphanesi ile basit şekiller olan çizgiler, dikdörtgenler ve daireler gibi şekillerin yanı sıra çokgenler, eğriler, yaylar ve Bezier şekilleri gibi karmaşık şekiller de çizebilirsiniz. Aspose.PSD kütüphanesi, Aspose.PSD ad alanındaki Graphics sınıfını kullanarak bu şekilleri oluşturur. Grafik nesneleri, bir resim üzerinde farklı çizim işlemlerini gerçekleştirmekten sorumludur ve bu sayede resmin yüzeyini değiştirir. Graphics sınıfı, şu şekilleri geliştirmek için çeşitli yardımcı nesneler kullanır:

Graphics Sınıfı ile Çizim

Aşağıda, Graphics sınıfının kullanımını gösteren bir kod örneği bulunmaktadır. Örnek kaynak kodu, basit ve takip etmesi kolay tutulması için birden fazla parçaya bölünmüştür. Adım adım, örnekler, nasıl yapılacağını göstermektedir:

  1. Bir resim oluşturun.
  2. Bir Graphics nesnesi oluşturun ve başlatın.
  3. Yüzeyi temizleyin.
  4. Bir elips çizin.
  5. Doldurulmuş bir çokgen çizin ve resmi kaydedin.

Programlama Örnekleri

Bir Resim Oluşturma

Herhangi Bir Dosya Oluşturma adımlarında açıklanan yöntemlerden birini kullanarak bir resim oluşturmaya başlayın.

// 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());
}

Graphics Nesnesi Oluşturma ve Başlatma

Daha sonra, bir Graphics nesnesi oluşturun ve başlatın ve bunun için Image nesnesini yapıcı fonksiyona ileterek bunu başlatın.

// 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);
}

Yüzeyi Temizleme

Graphics sınıfı Clear metodu çağırılarak yüzey temizlenir ve parametre olarak renk geçirilir. Bu metot, argüman olarak geçirilen rengi kullanarak Grafik yüzeyini doldurur.

// 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());
}

Bir Elips Çizin

Graphics sınıfının şekilleri çizmek ve doldurmak için birçok metodu olduğunu fark edebilirsiniz. Aspose.PSD for .NET API Referansında tüm metotların tam listesini bulacaksınız. Graphics sınıfı tarafından sunulan DrawEllipse yöntemine ekranın etrafındaki elipsin sınırlayıcı dikdörtgeni tanımlamak için bir Rectangle nesnesi kabul eden sürümlerinden birini kullanmak için istediğiniz rengi içeren Pen nesnesini kullandınız.

// 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());
}

Doldurulmuş Bir Çokgen Çizin

Ardından, LinearGradientBrush ve nokta dizisini kullanarak bir çokgen çizin. Graphics sınıfı, FillPolygon() yöntemini kabul eden bir dizi aşırı yüklü sürüm açığa çıkarmıştır. Bunların tümü dolgunun özelliklerini tanımlayan bir Brush nesnesini ilk argüman olarak alır ve ikinci argümanı bir nokta dizisi olarak alır. Lütfen, dizi içindeki her iki ardışık noktanın bir çokgenin bir kenarını belirttiğine dikkat edin.

// 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());
}

Grafikler Kullanarak Resimler Çizme : Tam Kaynak

// 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());
}

Tüm işlemeyen ve yönetilmeyen kaynaklara erişen IDisposable arabirimini gerçekleyen tüm sınıflar, düzgün bir şekilde atıldığından emin olmak için Using deyimi içinde başlatılır.