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.


            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));

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

            // Encoding the RenderBitmapTarget as a PNG file.
            PngBitmapEncoder png = new PngBitmapEncoder();
            using (Stream stm = File.Create('new.png'))

Share with

Related FAQs

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

Please submit your question and answer.