2012-01-29 74 views
3

是否有任何開源庫在C#中繪製文本到圖像?我一直在努力與TextRenderergraphics.DrawString()整天,但我從來沒有接近得到體面的結果,我試過平滑,插值,TextRenderHint的每一個組合,但質量總是半體面的。在圖像庫上繪圖文本

這裏有一些圖片,這是最好的,我achived:

enter image description here

它需要怎樣的樣子:

enter image description here

這確實看起來不錯,但有一些字符串看起來像字符間距對某些字母和字符串傾斜是錯誤的。

設置有:

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; 

格式是巴和背景是透明的,方法是TextRenderer.Drawtext()。似乎文本的粗細是錯誤的,我認爲這是平滑的錯誤,當我嘗試粗體文​​本時,它保持幾乎相同,但只有字體大小爲〜10px。

+0

你使用Winforms嗎?你打算使用哪個版本的.NET? – ose 2012-01-29 00:16:48

+0

我實際上使用ASP.NET 4.0 – formatc 2012-01-29 00:27:22

+0

我已將ASP.NET標記添加到您的帖子。今後這樣做會讓您的問題更多地從具有ASP.NET專業知識的人員那裏獲得知名度 – ose 2012-01-29 00:29:52

回答

6

這是我使用的版權水印添加到上傳到我的網站的照片:

//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; 
    } 
+1

首先感謝看,它看起來非常好!無論如何,我找到解決方案我的問題是,我在透明背景上繪製文本,而用任何背景色填充時都解決了這個問題。但查看這些代碼可能會解決一些可能遇到的問題。 – formatc 2012-01-31 11:12:35

0

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

特別是,如果你使用它,負荷下的多個線程,你將體驗到類似異常這一個:

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) 

這就是說,我們發現將所有繪圖操作封送到單個STA線程似乎避免 這些問題。