Welcome Guest! To enable all features, please Login or Register.

Notification

Icon
Error

Options
View
Last Go to last post Unread Go to first unread post
#1 Posted : Thursday, January 5, 2023 1:47:36 PM(UTC)

Amin  
Amin

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.
LEAD Logo
thanks 1 user thanked Amin for this useful post.
noramila on 1/2/2024(UTC)
 

Try the latest version of LEADTOOLS for free for 60 days by downloading the evaluation: https://www.leadtools.com/downloads

Wanna join the discussion? Login to your LEADTOOLS Support accountor Register a new forum account.

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.

Powered by YAF.NET | YAF.NET © 2003-2024, Yet Another Forum.NET
This page was generated in 0.050 seconds.