LEADTOOLS Support
Imaging
Imaging SDK Examples
HOW TO: Draw on Huge Images using C# in Windows
#1
Posted
:
Thursday, January 5, 2023 1:47:36 PM(UTC)
Groups: Manager, Tech Support
Posts: 367
Was thanked: 1 time(s) in 1 post(s)
To draw text and shapes on an image, a System.Drawing.Graphics object is often created from the image, which encapsulates a GDI+ drawing surface.
However, if the image dimensions are above a certain range, GDI+ cannot create a drawing surface from that image, which prevents direct drawing on the image.
One workaround is to clone a small portion of the image where drawing is required, draw on the smaller image then copy it back to its original position in the larger image.
Below are 2 C# functions that use LEADTOOLS to perform identical drawing. The first,
DrawOnImage(), uses the direct approach and works with smaller images. The second,
DrawOnLargeImage(), uses the workaround approach.
The second function has been tested and shown to work with huge images where the first function failed.
Code:
private void DrawOnImage(RasterImage img, string text, int x, int y, Font font)
{
Leadtools.Drawing.RasterImageGdiPlusGraphicsContainer graphicsContainer = new Leadtools.Drawing.RasterImageGdiPlusGraphicsContainer(img);
var g = graphicsContainer.Graphics;
g.DrawString(text, font, Brushes.MediumVioletRed, new PointF(x, y));
var size = g.MeasureString(text, font).ToSize(); // get size of text
g.DrawRectangle(Pens.CadetBlue, x, y, size.Width, size.Height);
graphicsContainer.Dispose();
}
private void DrawOnLargeImage(RasterImage img, string text, int x, int y, Font font)
{
RasterImage testImg = img.Clone(new LeadRect(0, 0, 4, 4));
Leadtools.Drawing.RasterImageGdiPlusGraphicsContainer graphicsContainer = new Leadtools.Drawing.RasterImageGdiPlusGraphicsContainer(testImg);
Size size = graphicsContainer.Graphics.MeasureString(text, font).ToSize();
graphicsContainer.Dispose();
RasterImage drawingImage = img.Clone(new LeadRect(x, y, size.Width + 1, size.Height + 1)); // take portion enough for string and rectangle
graphicsContainer = new Leadtools.Drawing.RasterImageGdiPlusGraphicsContainer(drawingImage);
var g = graphicsContainer.Graphics;
g.DrawString(text, font, Brushes.MediumVioletRed, new PointF(0, 0));
g.DrawRectangle(Pens.CadetBlue, 0, 0, size.Width, size.Height);
graphicsContainer.Dispose();
Leadtools.ImageProcessing.CombineFastCommand combine = new Leadtools.ImageProcessing.CombineFastCommand(img,
new LeadRect(x, y, drawingImage.Width, drawingImage.Height), new LeadPoint(0, 0),
Leadtools.ImageProcessing.CombineFastCommandFlags.Destination0 | Leadtools.ImageProcessing.CombineFastCommandFlags.OperationAdd);
combine.Run(drawingImage);
drawingImage.Dispose();
}
Amin Dodin
Senior Support Engineer
LEAD Technologies, Inc.
1 user thanked Amin for this useful post.
LEADTOOLS Support
Imaging
Imaging SDK Examples
HOW TO: Draw on Huge Images using C# in Windows
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.