Drawing Vector Images

Draw Vector Image

Using Aspose.Imaging for Java you can draw vector image on another vector image. With a few simple steps, you will be able to draw a vector image on another vector image.

  • Create MemoryStream object
  • Load a SVG image
  • Rasterize SVG to PNG and write the result to a stream
  • Load a PNG image from stream for further drawing
  • Draw PNG image on existing SVG image
  • Save the results
String dir = "images/";
try (ByteArrayOutputStream drawnImageStream = new ByteArrayOutputStream())
{
// First, rasterize Svg to Png and write the result to a stream.
try (SvgImage svgImage = (SvgImage)Image.load(dir + "asposenet_220_src02.svg"))
{
SvgRasterizationOptions rasterizationOptions = new SvgRasterizationOptions();
rasterizationOptions.setPageSize(Size.to_SizeF(svgImage.getSize()));
PngOptions saveOptions = new PngOptions();
saveOptions.setVectorRasterizationOptions(rasterizationOptions);
svgImage.save(drawnImageStream, saveOptions);
// Now load a Png image from stream for further drawing.
try (RasterImage imageToDraw = (RasterImage)Image.load(new ByteArrayInputStream(drawnImageStream.toByteArray())))
{
// Drawing on the existing Svg image.
SvgGraphics2D graphics = new SvgGraphics2D(svgImage);
// Scale down the entire drawn image by 2 times and draw it to the center of the drawing surface.
int width = imageToDraw.getWidth() / 2;
int height = imageToDraw.getHeight() / 2;
Point origin = new Point((svgImage.getWidth() - width) / 2, (svgImage.getHeight() - height) / 2);
Size size = new Size(width, height);
graphics.drawImage(imageToDraw, origin, size);
// Save the result image
SvgImage resultImage = graphics.endRecording();
try
{
resultImage.save(dir + "asposenet_220_src02.DrawVectorImage.svg");
}
finally
{
resultImage.close();
}
}
}
}