How to add text to image

378 views Asked by At

I scaned a image with LEADTOOLS18. and show it in RasterImageViewer.

I want to add text in image.

I use this code.

rasterImageViewer.BeginUpdate();
var container = new RasterImageGdiPlusGraphicsContainer(rasterImageViewer.Image);
container.Graphics.SmoothingMode = SmoothingMode.HighQuality;
Font font = new Font(new FontFamily("Tahoma"), 12);
var point = new PointF(200, 200);
container.Graphics.DrawString("LEADTOOLS", font, new SolidBrush(Color.Red), point);
rasterImageViewer.EndUpdate();
rasterImageViewer.UpdateLayout();

but don't add text!!!

1

There are 1 answers

0
LEADTOOLS Support On

Your code is correct, but it's missing something. When you modify the viewer's Image member, which is a Leadtools.RasterImage, this is not the image that actually gets displayed. It must first get converted to a WPF ImageSource then stored in the viewer's Source property.

When you use one of LEADTOOLS image processing classes, it automatically does this conversion, such as this code:

Leadtools.ImageProcessing.Effects.AverageCommand average = new AverageCommand(3);
average.Run(rasterImageViewer.Image);

However, when you use Graphics commands to draw on the image, you need to inform the control that the Image has changed so that the Source gets updated. One way is to Raise the RasterImage.Changed event like this directly after your code:

rasterImageViewer.Image.OnChanged(new
    Leadtools.RasterImageChangedEventArgs(Leadtools.RasterImageChangedFlags.Data));

Another way is to perform the conversion yourself like this:

var src = Leadtools.Windows.Media.RasterImageConverter.ConvertToSource(rasterImageViewer.Image,
    Leadtools.Windows.Media.ConvertToSourceOptions.None);
rasterImageViewer.Source = null;
rasterImageViewer.Source = src;