2010-05-07 4 views
1

J'ai la page .ashx suivante qui prend quelques paramètres de chaîne de requête et retourne un bitmap avec le texte spécifié écrit dessus. Le problème que j'ai, c'est que je ne fais que régler manuellement la taille initiale de l'image bitmap à 100 X 100 alors que ce que je veux vraiment, c'est que le bitmap soit assez grand pour inclure tout le texte qui y est écrit. Comment puis-je faire cela?Comment ajuster la taille de bitmap créé par programme pour faire correspondre le texte dessiné dessus?

public void ProcessRequest (HttpContext context) { 
    context.Response.ContentType = "image/png"; 
     string text = context.Request.QueryString["Text"]; 
    //set FontName 
    string fontName; 
    if (context.Request.QueryString["FontName"] != null) 
    { 
     fontName = context.Request.QueryString["FontName"]; 
    } 
    else 
    { 
     fontName = "Arial"; 
    } 
    //Set FontSize 
    int fontEms; 
    if (context.Request.QueryString["FontSize"] != null) 
    { 
     string fontSize = context.Request.QueryString["FontSize"]; 
     fontEms = Int32.Parse(fontSize); 

    } 
    else 
    { 
     fontEms = 12; 
    } 

    //Set Font Color 
    System.Drawing.Color color; 
    if (context.Request.QueryString["FontColor"] != null) 
    { 
     string fontColor = context.Request.QueryString["FontColor"]; 
     color = System.Drawing.ColorTranslator.FromHtml(fontColor); 
     context.Response.Write(color.ToString()); 
    } 
    else 
    { 
     color = System.Drawing.Color.Red; 
    } 
    using (System.Drawing.Text.PrivateFontCollection fnts = new System.Drawing.Text.PrivateFontCollection()) 
    using (System.Drawing.FontFamily fntfam = new System.Drawing.FontFamily(fontName)) 
    using (System.Drawing.SolidBrush brush = new System.Drawing.SolidBrush(color)) 
    using (System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(100, 100)) 
    { 
     using (System.Drawing.Font fnt = new System.Drawing.Font(fntfam, fontEms)) 
     { 
      fnts.AddFontFile(System.IO.Path.Combine(@"C:\Development\Fonts\", fontName)); 
      System.Drawing.Graphics graph = System.Drawing.Graphics.FromImage(bmp); 
      graph.DrawString(text, fnt, brush, new System.Drawing.Point(0, 0)); 
      string imgPath = System.IO.Path.Combine(@"C:\Development\MyPath\Images\Text", System.IO.Path.GetRandomFileName()); 
      bmp.Save(imgPath); 
      context.Response.WriteFile(imgPath); 
     } 
    } 
} 

Mise à jour: Je liquidée créer Bitmap plus grand que je ne jamais besoin, obtenir les graphiques de cette bitmap et utiliser la méthode MeasureString pour obtenir la taille du texte que je vais écrire et culture cette partie de l'image bitmap dans un nouveau bitmap comme ça.

using (System.Drawing.Text.PrivateFontCollection fnts = new System.Drawing.Text.PrivateFontCollection()) 
    using (System.Drawing.FontFamily fntfam = new System.Drawing.FontFamily(fontName)) 
    using (System.Drawing.SolidBrush brush = new System.Drawing.SolidBrush(color)) 
    using (System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(500, 500)) 
    { 
     using (System.Drawing.Font fnt = new System.Drawing.Font(fntfam, fontEms)) 
     { 
      fnts.AddFontFile(System.IO.Path.Combine(@"C:\Development\Fonts\", fontName)); 
      System.Drawing.Graphics graph = System.Drawing.Graphics.FromImage(bmp); 
      System.Drawing.SizeF bmpSize = graph.MeasureString(text, fnt); 
      System.Drawing.Rectangle rect = new System.Drawing.Rectangle(new System.Drawing.Point(0, 0), bmpSize.ToSize()); 
      System.Drawing.Bitmap croppedBmp = bmp.Clone(rect, bmp.PixelFormat); 
      graph = System.Drawing.Graphics.FromImage(croppedBmp); 
      graph.DrawString(text, fnt, brush, new System.Drawing.Point(0, 0)); 
      string imgPath = System.IO.Path.Combine(@"C:\Development\MyPath\Images\Text", System.IO.Path.GetRandomFileName()); 
      croppedBmp.Save(imgPath); 
      context.Response.WriteFile(imgPath); 
     } 
    } 
+0

+1 Question intéressante. =) –

Répondre

2

Je suppose qu'il ya une méthode System.Drawing.Graphics.MeasureString qui retournera la taille du texte spécifié en utilisant la police spécifiée. Puis, vous pouvez redimensionner votre image en conséquence, ou redimensionner votre texte en conséquence afin de les rendre tous les deux compatibles. Peut-être un coup d'oeil à ce question and answers vous guidera d'une manière ou d'une autre, car la question est de dimensionner le texte afin qu'il puisse tenir dans une zone. Vous semblez vouloir faire quelque chose de semblable, donc l'algorithme peut vous inspirer.

Questions connexes