在图像库上绘制文本

时间:2012-01-29 00:07:27

标签: c# asp.net

是否有任何开源库可以在C#中将文本绘制成图像?我一整天都在TextRenderergraphics.DrawString(),但我从未接近获得不错的结果,我尝试了Smoothing,Interpolation,TextRenderHint的所有组合,但质量始终是半体面的。

以下是一些图片,这是我最好的成就:

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;

格式为Png,背景为透明,方法为TextRenderer.Drawtext()。看起来像文字的厚度是错误的,我认为它是平滑的错误,当我尝试粗体文​​本它保持几乎相同,但只有字体大小〜10px。

2 个答案:

答案 0 :(得分: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 :(得分: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线程似乎可以避免这些问题。