locked
Image RRS feed

  • Question

  • User155303772 posted

    In some sites, i find this: "Please enter the letters as they appear in the image correctly" I found that this is a image.. My doubt is how can we change randomly text of an image since no text property is present for image.. Thanks

    Saturday, May 2, 2009 8:06 AM

Answers

  • User187056398 posted

    The bitmap is generated dynamically.

    Here is one version:

     

        /// ---- TextToBitmap ----------------------------
        ///
        /// using System.Drawing;
        /// using System.Drawing.Imaging;
        /// Author: Steve Wellens
        /// 
        /// <summary>
        /// Takes a Text string and returns a bitmap of the string
        /// </summary>
    
        public static Bitmap TextToBitmap(String TheText)
        {
            Font DrawFont = null;
            SolidBrush DrawBrush = null;
            Graphics DrawGraphics = null;
            Bitmap TextBitmap = null;
            try
            {
                // start with empty bitmap, get it's graphic's object
                // and choose a font
                TextBitmap = new Bitmap(1, 1);
                DrawGraphics = Graphics.FromImage(TextBitmap);
                DrawFont = new Font("Arial", 16);
    
                // see how big the text will be
                int Width = (int)DrawGraphics.MeasureString(TheText, DrawFont).Width;
                int Height = (int)DrawGraphics.MeasureString(TheText, DrawFont).Height;
    
                // recreate the bitmap and graphic object with the new size
                TextBitmap = new Bitmap(TextBitmap, Width, Height);
                DrawGraphics = Graphics.FromImage(TextBitmap);
    
                // get the drawing brush and where we're going to draw
                DrawBrush = new SolidBrush(Color.Black);
                PointF DrawPoint = new PointF(0, 0);
    
                // clear the graphic white and draw the string
                DrawGraphics.Clear(Color.White);
                DrawGraphics.DrawString(TheText, DrawFont, DrawBrush, DrawPoint);
    
                return TextBitmap;
            }
            finally
            {
                // don't dispose the bitmap, the caller needs it.
                DrawFont.Dispose();
                DrawBrush.Dispose();
                DrawGraphics.Dispose();
            }
        }
    
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, May 2, 2009 9:16 AM