How to draw an image
How to draw an image
Several methods are available for drawing graphics primitives such as draw_line, draw_ellipse, draw_rect draw_arc, draw_bezier and draw_string. In the beginning, we create a graphic surface to draw an image and describe graphic properties. In our example, we will create a surface with a size of 100 x 100 pixels and a color depth of 32 bits per pixel and set the background to yellow color with Clear method. Next, we define a drawing tool - a Pen object with a Blue color, to draw a dotted line or use SolidBrush for continuous lines with different colors, followed by X and Y coordinates of the start and end points of the lines:
import aspose.pycore as aspycore | |
from aspose.imaging import Image, Pen, Color, Graphics, Point | |
from aspose.imaging.imageoptions import BmpOptions | |
from aspose.imaging.sources import FileCreateSource | |
from aspose.imaging.brushes import SolidBrush | |
import os | |
if 'TEMPLATE_DIR' in os.environ: | |
templates_folder = os.environ['TEMPLATE_DIR'] | |
else: | |
templates_folder = r"C:\Users\USER\Downloads\templates" | |
delete_output = 'SAVE_OUTPUT' not in os.environ | |
data_dir = templates_folder | |
out_file = os.path.join(data_dir, "result.bmp") | |
# Create an instance of BmpOptions and set its various properties | |
with BmpOptions() as save_options: | |
save_options.bits_per_pixel = 32 | |
# Set the Source for BmpOptions and create an instance of Image | |
save_options.source = FileCreateSource(out_file, False) | |
with Image.create(save_options, 100, 100) as image: | |
# Create and initialize an instance of Graphics class and clear Graphics surface | |
graphic = Graphics(image) | |
graphic.clear(Color.yellow) | |
# Draw two dotted diagonal lines by specifying the Pen object having blue color and co-ordinate Points | |
graphic.draw_line(Pen(Color.blue), 9, 9, 90, 90) | |
graphic.draw_line(Pen(Color.blue), 9, 90, 90, 9) | |
# Draw a four continuous line by specifying the Pen object having Solid Brush with red color and two point structures | |
graphic.draw_line(Pen(SolidBrush(Color.red)), Point(9, 9), Point(9, 90)) | |
graphic.draw_line(Pen(SolidBrush(Color.aqua)), Point(9, 90), Point(90, 90)) | |
graphic.draw_line(Pen(SolidBrush(Color.black)), Point(90, 90), Point(90, 9)) | |
graphic.draw_line(Pen(SolidBrush(Color.white)), Point(90, 9), Point(9, 9)) | |
image.save() | |
if delete_output: | |
os.remove(out_file) |
A more detailed description of how to draw graphic elements such as lines, ellipses, Bezier curves and text strings, you can find in the Aspose Imaging Developer Guide.
Example of the image with lines, ellipses and rectangles drawings on a transparent background:
