formatc
formatc

Reputation: 4323

Drawing Text on Image library

Is there any open source library for drawing text to image in C#? I have been strugling with TextRenderer and graphics.DrawString() whole day but I never got close to getting decent results, I tried every combination of Smoothing, Interpolation, TextRenderHint but quality is always semi-decent.

Here are some images and that is best I achived:

enter image description here

How it needs to look like:

enter image description here

This really looks good but with some strings seems like character spacing is wrong with some letters and the string leans.

Settings are:

objGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
              objGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
              objGraphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.GammaCorrected;
              objGraphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
              objGraphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half;
              objGraphics.TextContrast = 0;

Format is Png and background is transparent, method is TextRenderer.Drawtext(). Seems like thickness of text is wrong, I assume it's something wrong with smoothing, when I try to bold text it stays almost the same, but only with font size of ~10px.

Upvotes: 3

Views: 4092

Answers (2)

James
James

Reputation: 3928

Using System.Drawing classes in ASP.NET is not supported.

Specifically, if you use it, under load from multiple threads, you will experience exceptions like this one:

Win32Exception: The operation completed successfully
at MS.Win32.HwndWrapper..ctor(Int32 classStyle, Int32 style, Int32 exStyle, Int32 x, Int32 y,     Int32 width, Int32 height, String name, IntPtr parent, HwndWrapperHook[] hooks)
at System.Windows.Media.MediaContextNotificationWindow..ctor(MediaContext ownerMediaContext)
at System.Windows.Media.MediaContext..ctor(Dispatcher dispatcher)

That said, we discovered that marshaling all drawing operations to a single STA thread seemed to avoid these issues.

UPDATE: It's been five years and we still have no problem with this approach.

Upvotes: 1

Demig0d
Demig0d

Reputation: 158

Here's what I use to add a Copyright watermark to photos uploaded to my website:

    //Add Watermark to photo.
    private System.Drawing.Image CreateWatermark(System.Drawing.Image imgPhoto, string Copyright)
    {
        Graphics g = Graphics.FromImage(imgPhoto);

        g.SmoothingMode = SmoothingMode.HighQuality;
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
        g.PixelOffsetMode = PixelOffsetMode.HighQuality;

        foreach (PropertyItem pItem in imgPhoto.PropertyItems)
        {
            imgPhoto.SetPropertyItem(pItem);
        }

        int phWidth = imgPhoto.Width;
        int phHeight = imgPhoto.Height;

        //create a Bitmap the Size of the original photograph
        Bitmap bmPhoto = new Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb);

        bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

        //load the Bitmap into a Graphics object 
        Graphics grPhoto = Graphics.FromImage(bmPhoto);

        //------------------------------------------------------------
        //Step #1 - Insert Copyright message
        //------------------------------------------------------------

        //Set the rendering quality for this Graphics object
        grPhoto.SmoothingMode = SmoothingMode.AntiAlias;

        //Draws the photo Image object at original size to the graphics object.
        grPhoto.DrawImage(
            imgPhoto,                               // Photo Image object
            new Rectangle(0, 0, phWidth, phHeight), // Rectangle structure
            0,                                      // x-coordinate of the portion of the source image to draw. 
            0,                                      // y-coordinate of the portion of the source image to draw. 
            phWidth,                                // Width of the portion of the source image to draw. 
            phHeight,                               // Height of the portion of the source image to draw. 
            GraphicsUnit.Pixel);                    // Units of measure 

        //-------------------------------------------------------
        //to maximize the size of the Copyright message we will 
        //test multiple Font sizes to determine the largest posible 
        //font we can use for the width of the Photograph
        //define an array of point sizes you would like to consider as possiblities
        //-------------------------------------------------------
        int[] sizes = new int[] { 16, 14, 12, 10, 8, 6, 4 };

        Font crFont = null;
        SizeF crSize = new SizeF();

        //Loop through the defined sizes checking the length of the Copyright string
        //If its length in pixles is less then the image width choose this Font size.
        for (int i = 0; i < 7; i++)
        {
            //set a Font object to Arial (i)pt, Bold
            crFont = new Font("arial", sizes[i], FontStyle.Bold);
            //Measure the Copyright string in this Font
            crSize = grPhoto.MeasureString(Copyright, crFont);

            if ((ushort)crSize.Width < (ushort)phWidth)
                break;
        }

        //Since all photographs will have varying heights, determine a 
        //position 5% from the bottom of the image
        int yPixlesFromBottom = (int)(phHeight * .05);

        //Now that we have a point size use the Copyrights string height 
        //to determine a y-coordinate to draw the string of the photograph
        float yPosFromBottom = ((phHeight - yPixlesFromBottom) - (crSize.Height / 2));

        //Determine its x-coordinate by calculating the center of the width of the image
        float xCenterOfImg = (phWidth / 2);

        //Define the text layout by setting the text alignment to centered
        StringFormat StrFormat = new StringFormat();
        StrFormat.Alignment = StringAlignment.Near;

        //define a Brush which is semi trasparent black (Alpha set to 153)
        SolidBrush semiTransBrush2 = new SolidBrush(System.Drawing.Color.FromArgb(153, 0, 0, 0));

        //Draw the Copyright string
        grPhoto.DrawString(Copyright,                 //string of text
            crFont,                                   //font
            semiTransBrush2,                           //Brush
            new PointF(xCenterOfImg + 1, yPosFromBottom + 1),  //Position
            StrFormat);

        //define a Brush which is semi trasparent white (Alpha set to 153)
        SolidBrush semiTransBrush = new SolidBrush(System.Drawing.Color.FromArgb(153, 255, 255, 255));

        //Draw the Copyright string a second time to create a shadow effect
        //Make sure to move this text 1 pixel to the right and down 1 pixel
        grPhoto.DrawString(Copyright,                 //string of text
            crFont,                                   //font
            semiTransBrush,                           //Brush
            new PointF(xCenterOfImg, yPosFromBottom),  //Position
            StrFormat);                               //Text alignment
        imgPhoto = bmPhoto;
        return imgPhoto;
    }

Upvotes: 8

Related Questions