Question

I have written a imageHandler.ashx in c# which draw image of a text on a fly . Its takes a parameter string txt. The Handler is called from Asp.net page. At the time of calling, if text contains the character @ or any special character .it Does not create the images of @,or any special character's given to it.

According to my R&D the problem is with a Graphics.DrawString() method some how its is unable to write special characters.so please tell me how to create a image of a special characters Code I am using :

  String signatureText =  HttpUtility.HtmlDecode(signatureText);
  System.Drawing.Text.PrivateFontCollection fontCollection = new System.Drawing.Text.PrivateFontCollection();

            FontFamily ff1 = fontCollection.Families[0];
            // Initialize graphics
            RectangleF rectF = new RectangleF(0, 0, imgWidth, imgHeight);
   Bitmap pic = new Bitmap((int) rectF.Width,imgHeight, PixelFormat.Format24bppRgb);
            Graphics g = Graphics.FromImage(pic);
            //g.SmoothingMode = SmoothingMode.Default;
           // g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;

         Font font1 = new Font(ff1, imgHeight, FontStyle.Regular, GraphicsUnit.Pixel);
         float bestFontSize = BestFontSize(g, new Size(imgWidth, imgHeight), font1, signatureText);
         Font font2 = new Font(ff1, bestFontSize, FontStyle.Regular, GraphicsUnit.Pixel); 

            // Set colors
            Color fontColor = Color.Black;
            Color rectColor = Color.White;
            SolidBrush fgBrush = new SolidBrush(fontColor);
            SolidBrush bgBrush = new SolidBrush(rectColor);

            // Rectangle or ellipse?
            g.FillRectangle(bgBrush, rectF);

           // Set font direction & alignment
            StringFormat format = new StringFormat();
            //format.FormatFlags = StringFormatFlags.DirectionRightToLeft;

            format.Alignment = StringAlignment.Center;
            format.LineAlignment = StringAlignment.Center;


            // Finally, draw the font
            g.DrawString(signatureText, font2, fgBrush, rectF, format);
            pic = (Bitmap) ImageUtility.ResizeImageWithAspectRatio(pic, imgWidth, imgHeight, true);

            pic.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);

Any help is Appreciated Thanks in Advance

Was it helpful?

Solution

DrawString() don't have any problem while writing special characters if they are provided in font file (.ttf)

Problem can be in the following line

System.Drawing.Text.PrivateFontCollection fontCollection = new System.Drawing.Text.PrivateFontCollection();

Specially with the font you get in ff1

FontFamily ff1 = fontCollection.Families[0];

In your case it means Font ff1 don't have representation for some or all special characters.

Since I don't know how many font files you are getting from fontCollection.Families, if fontCollection.Families contain more than one font file try next one.

FontFamily ff1 = fontCollection.Families[1]; // and so on.

Or try to add font file with all special characters (if System.Drawing.Text.PrivateFontCollection is absolutely necessary ).

Alternatively you can use InstalledFontCollection instead of PrivateFontCollection

System.Drawing.Text.InstalledFontCollection  fontCollection = new System.Drawing.Text.InstalledFontCollection();

Which will give you all installed fonts of the system on which application is running through its Families property InstalledFontCollection.

Easiest way is (if only one font is required, as you are using only one font in your provided code) using constructor of FontFamily class with a string type parameter FontFamily.

Consider replacing this line

FontFamily ff1 = fontCollection.Families[0];

With this one

FontFamily fontFamily1 = new FontFamily("Arial");

Arial must be installed on the system where application is running.

Hope this will help.

OTHER TIPS

DrawString doesn't have any problem with drawing special characters like @ or copyright (I just tested it and produced the image below).

enter image description here

The problem might be from your URL parameter. Try encoding your text using Server.UrlEncode when passing the text, and then Server.UrlDecode before giving it to DrawString.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top