2010-11-10 26 views
3

Je veux rendre du texte en tant qu'image dans ASP.NET. Cela fonctionne bien, mais le texte est rendu très moche (avec des pixels gris autour du texte) même si j'active AntiAlias ​​(ou ClearType) en changeant l'option graphique TextRenderingHint.Rendu laid d'image avec DrawString-Method (ASP.NET)

Voici le code correspondant:

float width; 
    float height; 

    System.Drawing.Text.PrivateFontCollection fontcollection = new System.Drawing.Text.PrivateFontCollection(); 
    // Add the custom font families 
    fontcollection.AddFontFile(Server.MapPath("./Fonts/" + fontfile)); 


    Bitmap image = new Bitmap(10, 10); 
    Graphics graphic = Graphics.FromImage(image); 
    Font font = new Font(fontcollection.Families.First(), fontsize, style); 

    SizeF size = graphic.MeasureString(text, font); 
    width = size.Width; 
    height = size.Height; 

    image = new Bitmap(Convert.ToInt32(width), Convert.ToInt32(height)); 
    graphic = Graphics.FromImage(image); 
    graphic.FillRectangle(new SolidBrush(Color.White), 0, 0, width, height); 

    graphic.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit; 
    graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bicubic; 
    graphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; 
    graphic.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality; 

    graphic.DrawString(text, font, Brushes.Black, new PointF(0, 0)); 

    Response.ContentType = "image/jpeg"; 
    image.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg); 

Voici un lien vers l'image générée (zoom):

enter image description here

l'image zoomée:

enter image description here

Comment puis-je résoudre ça?

+0

Fournissez une image non agrandie s'il vous plaît. – leppie

+0

Lien vers l'image non agrandie: http://i.imgur.com/Ymkrl.jpg –

Répondre

2

Créez le format PNG de l'image.

La compression JPEG par défaut est inutile.

+0

Cela fonctionne maintenant, merci :) Avez-vous une explication pourquoi cela se produit lors de l'utilisation de la compression JPEG? –

+0

@raphael: JPEG est un codec avec perte. PNG n'est pas. – leppie