Grafik Kullanarak Resimler Çizme

Grafik Kullanarak Resimler Çizme

Aspose.PSD kütüphanesi ile çizgi, dikdörtgen, daire gibi basit şekillerin yanı sıra çokgenler, eğriler, yaylar ve Bezier şekilleri gibi karmaşık şekiller de çizebilirsiniz. Aspose.PSD kütüphanesi, bu şekilleri Aspose.PSD ad alanında bulunan Graphics sınıfını kullanarak oluşturur. Grafik nesneleri, bir resimde farklı çizim işlemlerini gerçekleştirmekten sorumludur ve bu şekilde resmin yüzeyini değiştirir. Graphics sınıfı, şekilleri geliştirmek için çeşitli yardımcı nesneleri kullanır:

  • Kalemler, çizgileri çizmek, şekilleri çerçevelemek veya diğer geometrik temsilleri oluşturmak için.
  • Fırçalar, alanların nasıl doldurulacağını tanımlamak için.
  • Yazı tipleri, metnin karakter şeklini tanımlamak için.

Grafik Sınıfıyla Çizim Yapma

Aşağıda, Grafik sınıfının kullanımını gösteren bir kod örneği bulunmaktadır. Örnek kaynak kodu, basit ve takip edilmesi kolay olacak şekilde parçalara ayrılmıştır. Adım adım, örnekler aşağıdakileri nasıl yapılacağını göstermektedir:

  1. Bir resim oluşturma.
  2. Bir Grafik nesnesi oluşturup başlatma.
  3. Yüzeyi temizleme.
  4. Bir elips çizme.
  5. Dolu bir çokgen çizme ve resmi kaydetme.

Programlama Örnekleri

Bir Resim Oluşturma

Herhangi bir Oluşturma Dosyaları bölümünde açıklanan yöntemlerden herhangi birini kullanarak bir resim oluşturmaya başlayın.

String dataDir = Utils.getDataDir(DrawingBezier.class) + "DrawingImages/";
// Create an instance of BmpOptions and set its various properties
String outpath = dataDir + "Bezier.bmp";
// Create an instance of BmpOptions and set its various properties
BmpOptions saveOptions = new BmpOptions();
saveOptions.setBitsPerPixel(32);
// Create an instance of Image
try (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.getYellow());
// Initializes the instance of PEN class with black color and width
Pen BlackPen = new Pen(Color.getBlack(), 3);
float startX = 10;
float startY = 25;
float controlX1 = 20;
float controlY1 = 5;
float controlX2 = 55;
float controlY2 = 10;
float endX = 90;
float endY = 25;
// Draw a Bezier shape by specifying the Pen object having black color and co-ordinate Points and save all changes.
graphic.drawBezier(BlackPen, startX, startY, controlX1, controlY1, controlX2, controlY2, endX, endY);
// export image to bmp file format.
image.save(outpath, saveOptions);
}

Bir Grafik Nesnesi Oluşturma ve Başlatma

Daha sonra, bir Grafik nesnesi oluşturun ve başlatın, Image nesnesini oluşturucuya ileterek.

String dataDir = Utils.getDataDir(DrawingArc.class) + "DrawingImages/";
// Create an instance of BmpOptions and set its various properties
String outpath = dataDir + "Arc.bmp";
// Create an instance of BmpOptions and set its various properties
BmpOptions saveOptions = new BmpOptions();
saveOptions.setBitsPerPixel(32);
// Create an instance of Image
try (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.getYellow());
// 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.getBlack()), 0, 0, width, height, startAngle, sweepAngle);
// export image to bmp file format.
image.save(outpath, saveOptions);
}

Yüzeyi Temizleme

Grafik yüzeyini, Clear yöntemini çağırarak ve bir renk olarak parametre olarak göndererek temizleyin. Bu yöntem, argüman olarak geçirilen renk ile Grafik yüzeyini doldurur.

String dataDir = Utils.getDataDir(DrawingUsingGraphics.class) + "DrawingImages/";
// Create an instance of Image
try (PsdImage image = new PsdImage(500, 500)) {
Graphics graphics = new Graphics(image);
// Clear the image surface with white color and Create and initialize a Pen object with blue color
graphics.clear(Color.getWhite());
Pen pen = new Pen(Color.getBlue());
// 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));
LinearGradientBrush linearGradientBrush = new LinearGradientBrush(image.getBounds(), Color.getRed(), Color.getWhite(), 45f);
// graphics.fillPolygon(linearGradientBrush, new Point(200, 200), new Point(400, 200), new Point(250, 350));
Point[] points = {new Point(200, 200), new Point(400, 200), new Point(250, 350)};
graphics.fillPolygon(linearGradientBrush, points);
// export modified image.
image.save(dataDir + "DrawingUsingGraphics_output.bmp", new BmpOptions());
}

Bir Elips Çizme

Grafik sınıfının çizim ve doldurma yöntemlerini birçoklarına açtığını fark edebilirsiniz. Aspose.PSD için Java API Referansında yöntemlerin tam listesini bulabilirsiniz. Grafik sınıfı tarafından açıklanan birden fazla aşırı yüklemeli çizElips yöntemine dikkatinizi çekerken. Tüm bu yöntemler, elipsin etrafındaki sınırlayıcı dikdörtgeni tanımlamak için ikinci bir parametre olarak bir Pen nesnesi kabul eder. Bu örneğin amacı için, istediğiniz rengi kullanarak bir elips çizmek için ikinci bir parametre olarak bir Rectangle nesnesini kabul eden sürümü kullanın.

String dataDir = Utils.getDataDir(DrawingEllipse.class) + "DrawingImages/";
// Create an instance of BmpOptions and set its various properties
String outpath = dataDir + "Ellipse.bmp";
// Create an instance of BmpOptions and set its various properties
BmpOptions saveOptions = new BmpOptions();
saveOptions.setBitsPerPixel(32);
// Create an instance of Image
try (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.getYellow());
// Draw a dotted ellipse shape by specifying the Pen object having red color and a surrounding Rectangle
graphic.drawEllipse(new Pen(Color.getRed()), new Rectangle(30, 10, 40, 80));
// Draw a continuous ellipse shape by specifying the Pen object having solid brush with blue color and a surrounding Rectangle
graphic.drawEllipse(new Pen(new SolidBrush(Color.getBlue())), new Rectangle(10, 30, 80, 40));
// export image to bmp file format.
image.save(outpath, saveOptions);
}

Dolu Bir Çokgen Çizme

Sonra, bir dikdörtgen fırça ve bir nokta dizisi kullanarak bir çokgen çizin. Grafik sınıfı, FillPolygon yöntemine aşırı yüklemeli birkaç sürüm açıklamıştır. Tüm bu, bir doldurma özelliklerini tanımlayan bir Fırça nesnesi olarak ilk argümanı kabul eder. İkinci parametre bir nokta dizisidir. Lütfen dizideki her iki ardışık noktanın çokgenin bir kenarını belirlediğini unutmayın.

String dataDir = Utils.getDataDir(DrawingUsingGraphics.class) + "DrawingImages/";
// Create an instance of Image
try (PsdImage image = new PsdImage(500, 500)) {
Graphics graphics = new Graphics(image);
// Clear the image surface with white color and Create and initialize a Pen object with blue color
graphics.clear(Color.getWhite());
Pen pen = new Pen(Color.getBlue());
// 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));
LinearGradientBrush linearGradientBrush = new LinearGradientBrush(image.getBounds(), Color.getRed(), Color.getWhite(), 45f);
// graphics.fillPolygon(linearGradientBrush, new Point(200, 200), new Point(400, 200), new Point(250, 350));
Point[] points = {new Point(200, 200), new Point(400, 200), new Point(250, 350)};
graphics.fillPolygon(linearGradientBrush, points);
// export modified image.
image.save(dataDir + "DrawingUsingGraphics_output.bmp", new BmpOptions());
}

Grafik Kullanarak Resimler Çizme: Tam Kaynak

String dataDir = Utils.getDataDir(DrawingUsingGraphics.class) + "DrawingImages/";
// Create an instance of Image
try (PsdImage image = new PsdImage(500, 500)) {
Graphics graphics = new Graphics(image);
// Clear the image surface with white color and Create and initialize a Pen object with blue color
graphics.clear(Color.getWhite());
Pen pen = new Pen(Color.getBlue());
// 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));
LinearGradientBrush linearGradientBrush = new LinearGradientBrush(image.getBounds(), Color.getRed(), Color.getWhite(), 45f);
// graphics.fillPolygon(linearGradientBrush, new Point(200, 200), new Point(400, 200), new Point(250, 350));
Point[] points = {new Point(200, 200), new Point(400, 200), new Point(250, 350)};
graphics.fillPolygon(linearGradientBrush, points);
// export modified image.
image.save(dataDir + "DrawingUsingGraphics_output.bmp", new BmpOptions());
}

Tüm IDisposable uygulayan sınıflar ve yönetilmeyen kaynaklara erişen sınıflar, düzgün bir şekilde atılmalarını sağlamak için bir Using deyimi içinde örneklenmektedir.