Kreslení obrázků pomocí grafiky
Kreslení obrázků pomocí grafiky
S knihovnou Aspose.PSD můžete kreslit jednoduché tvary jako jsou čáry, obdélníky a kružnice, stejně jako složité tvary jako jsou polygon, křivky, oblouky a tvarové křivky Bézier. Knihovna Aspose.PSD vytváří tyto tvary pomocí třídy Graphics, která se nachází v jmenném prostoru Aspose.PSD. Objekty grafiky jsou zodpovědné za provádění různých kreslicích operací na obrázku, které mění povrch obrázku. Třída Graphics využívá různé pomocné objekty k vylepšení tvarů:
· Tužky, pro kreslení čar, obrysů tvarů nebo vykreslování jiných geometrických reprezentací.
· Kartáče, pro definování, jak jsou oblasti vyplněny.
· Písma, pro definování tvaru písmen textu.
Kreslení s třídou Graphics
Níže je uveden příklad kódu demonstující použití třídy Graphics. Zdrojový kód příkladu byl rozdělen do několika částí, aby byl jednoduchý a snadno sledovatelný. Krok za krokem ukazují příklady, jak:
- Vytvořit obrázek.
- Vytvořit a inicializovat objekt grafiky.
- Vymazat povrch.
- Kreslit elipsu.
- Nakreslete vyplněný polygon a uložte obrázek.
Vzorové programování
Vytvoření obrázku
Začněte vytvářením obrázku pomocí jedné z metod popsaných v Sekci vytváření souborů.
// 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()); | |
} |
Vytvoření a inicializace objektu Graphics
Poté vytvořte a inicializujte objekt grafiky předáním objektu Image do jeho konstruktoru.
// 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); | |
} |
Vyčištění povrchu
Vymažte povrch grafiky zavoláním metody Clear třídy Graphics a předejte barvu jako parametr. Tato metoda vyplní povrch grafiky barvou předanou jako argument.
// 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()); | |
} |
Kreslení elipsy
Můžete si všimnout, že třída Graphics má vystaveno spoustu metod pro kreslení a vyplňování tvarů. Kompletní seznam metod naleznete v referenční příručce API Aspose.PSD pro .NET. Třída Graphics má několik přetížených verzí metody DrawEllipse. Všechny tyto metody přijímají objekt Pen jako svůj první argument. Pozdější parametry jsou předány k definici ohraničujícího obdélníku kolem elipsy. Pro potřeby tohoto příkladu použijte verzi metody přijímající objekt Rectangle jako druhý parametr pro nakreslení elipsy použitím objektu Pen ve vaší požadované barvě.
// 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()); | |
} |
Kreslení vyplněného polygonu
Poté nakreslete polygon pomocí objektu LinearGradientBrush a pole bodů. Třída Graphics má vystaveno několik přetížených verzí metody FillPolygon(). Všechny tyto přijímají objekt Brush jako svůj první argument, který definuje vlastnosti vyplnění. Druhým parametrem je pole bodů. Upozorňujeme, že každé dva po sobě jdoucí body v poli určují stranu polygonu.
// 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()); | |
} |
Kreslení obrázků pomocí grafiky: Úplný zdroj
// 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()); | |
} |
Všechny třídy, které implementují IDisposable a přistupují k neupraveným prostředkům, jsou vytvořeny ve výroku Using, aby bylo zajištěno, že jsou správně zlikvidovány.