Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

GDI+ text looks bad

Posted on 2004-10-18
4
Medium Priority
?
379 Views
Last Modified: 2013-12-03
I have written a "textimage.aspx" page that returns an image/jpeg formatted according to the following parameters:
text string, font name, font size, font color, back color
The output usually looks fine but when using certain colors a degraded quality becomes apparent. (see http://beta.s3m.net/gdi_test.jpg)
I really need to figure out how to improve the quality... any change that would move me closer to the photoshop/fireworks-rendered text would be very helpful.

Here is the code, stripped down to the necessary parts:

////////////////////

Response.Clear();
Response.ContentType = "image/jpeg";

Bitmap myBmp = TextToImage(str,fName,fSize,fColor,bColor,trim);
EncoderParameters eps = new EncoderParameters(1);
eps.Param[0] = new EncoderParameter(Encoder.Quality, 100L);
ImageCodecInfo[] objEncoders = ImageCodecInfo.GetImageDecoders();

for(int i=0;i<objEncoders.Length;i++)
{
      if(objEncoders[i].MimeType==Response.ContentType)
      {
            myBmp.Save(Response.OutputStream,objEncoders[i],eps);
            myBmp.Dispose();
            break;
      }
}

private Bitmap CreateImage(strText, strFontName, intFontSize, strForeColor, strBackColor)
{
      Color clrForeColor = ColorTranslator.FromHtml("#"+strForeColor);
      Color clrBackColor = ColorTranslator.FromHtml("#"+strBackColor);                  

      //get font path from DB of custom fonts

      PrivateFontCollection pfc = new PrivateFontCollection();
      pfc.AddFontFile(strFontPath);

      FontStyle myStyle = FontStyle.Regular;
      if(strFontName.ToLower().IndexOf("bold")>-1){myStyle = FontStyle.Bold;}
      if(strFontName.ToLower().IndexOf("italic")>-1){myStyle = FontStyle.Italic;}

      Font font=new Font(new FontFamily(strFontName,pfc),intFontSize, myStyle);

      Bitmap bmp = new Bitmap(1, 1, PixelFormat.Format24bppRgb);
      Graphics g = Graphics.FromImage(bmp);

      SizeF imageSize = g.MeasureString(strText,font);
      int intWidth = (int)imageSize.Width;
      int intHeight = (int)imageSize.Height;

      bmp = new Bitmap(intWidth, intHeight, PixelFormat.Format24bppRgb);
      g = Graphics.FromImage(bmp);
      g.Clear(clrBackColor);
      g.TextRenderingHint = TextRenderingHint.AntiAlias;
      g.DrawString(strText, font, new SolidBrush(clrForeColor),new PointF(0,0));
      g.CompositingQuality = CompositingQuality.HighQuality;
      pfc.Dispose();                  
      g.Flush();
      g.Dispose();
                  
      return bmp;
}
0
Comment
Question by:sexyrexy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 2

Author Comment

by:sexyrexy
ID: 12344445
Sorry the function names and overloads don't match up I just mistyped when stripping it down. The necessary functionality is still obvious.
0
 
LVL 28

Accepted Solution

by:
mmarinov earned 600 total points
ID: 12344624
Hi sexyrexy,

have you tried with
Response.ContentType = "image/gif";


B..M
0
 
LVL 2

Author Comment

by:sexyrexy
ID: 12349272
Yes, I've had unreliable results with GDI's gif output... it seems to be using a preset websafe 256 palette when downconverting the Bitmap to 8-bit GIF, rather than building a palette based on the colors found in the bitmap. This makes the output splotchy and rough. (http://beta.s3m.net/gdi_gif.gif)
0
 
LVL 2

Author Comment

by:sexyrexy
ID: 12349906
mmarinov,
Thanks for suggesting a GIF type again. It prompted me to think about the fact that GDI uses a default palette and wonder whether a custom palette could be constructed based on the given bitmap, just like photoshop might. The following two articles were very helpful:

http://dotnetjunkies.com/WebLog/bsblog/archive/2004/01/26/6103.aspx
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/colorquant.asp

There are warnings that generating the palette on the fly has alot of overhead, but by using pointers (and also in my case my images have very few colors since they are just text) the CPU load is minimized.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question