Live Chat Icon For mobile
Live Chat Icon

How can I stamp some text on an image?

Platform: WPF| Category: Image

The DrawingVisual can be used with a ‘BitmapImage’ instance and then rendered using ‘RenderBitmap’ class for creating custom rendered images. If text needs to be added, then use the ‘FomattedText’ instance.

The following code snippet shows how to render an image with a text.

[C#]

            BitmapImage bitImage = new BitmapImage()
            {
                UriSource = new Uri(@'C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Winter.jpg', UriKind.Relative),
                DecodePixelWidth = 200,
                DecodePixelHeight = 200
            };

            FormattedText text = new FormattedText('Winter', new CultureInfo('en-us'), FlowDirection.RightToLeft, new Typeface('Calibri', FontStyles.Normal,             FontWeights.Normal, new FontStretch()), this.FontSize, Brushes.White);

            DrawingVisual drawingVisual = new DrawingVisual();
            DrawingContext drawingContext = drawingVisual.RenderOpen();
            drawingContext.DrawImage(bitImage, new Rect(0, 0, bitImagebi.Width, bitImagebi.Height));
            drawingContext.DrawText(text, new Point(bitImage.Height / 2, 0));
            drawingContext.Close();

            RenderTargetBitmap renderBmap = new RenderTargetBitmap(bitImage.PixelWidth, bitImage.PixelHeight, 96, 96, PixelFormats.Pbgra32);
            renderBmap.Render(drawingVisual);

            // Encoding the RenderBitmapTarget as a PNG file.
            PngBitmapEncoder png = new PngBitmapEncoder();
            png.Frames.Add(BitmapFrame.Create(rtb));
            using (Stream stm = File.Create('new.png'))
            {
                png.Save(stm);
            }

Share with

Share on twitter
Share on facebook
Share on linkedin

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.