【问题标题】:c# write text on bitmapc#在位图上写文字
【发布时间】:2011-06-10 20:07:16
【问题描述】:

我有以下问题。我想以 c# windows 形式制作一些图形。 我想将位图读取到我的程序中,然后在该位图上写一些文本。最后我希望这张图片加载到图片框。这是我的问题。我该怎么做?

例如,它必须如何工作:

Bitmap a = new Bitmap(@"path\picture.bmp");
a.makeTransparent();
// ? a.writeText("some text", positionX, positionY);
pictuteBox1.Image = a;

可以吗?

【问题讨论】:

    标签: c# drawing


    【解决方案1】:
    Bitmap bmp = new Bitmap("filename.bmp");
    
    RectangleF rectf = new RectangleF(70, 90, 90, 50);
    
    Graphics g = Graphics.FromImage(bmp);
    
    g.SmoothingMode = SmoothingMode.AntiAlias;
    g.InterpolationMode = InterpolationMode.HighQualityBicubic;
    g.PixelOffsetMode = PixelOffsetMode.HighQuality;
    g.DrawString("yourText", new Font("Tahoma",8), Brushes.Black, rectf);
    
    g.Flush();
    
    image.Image=bmp;
    

    【讨论】:

    • g.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit; //添加此行以使文本更好看。
    • @rockXrock:实际上你想设置TextRenderingHint = TextRenderingHint.AntiAliasGridFit。每像素单比特模式为锯齿状:msdn.microsoft.com/en-us/library/a619zh6z(v=vs.110).aspx
    • 所有这些答案中缺少的一件事是“我怎么知道字符串是否适合?” (或“我怎样才能使位图完美地适合字符串?”)。答案是使用Graphics.MeasureString
    【解决方案2】:

    非常老的问题,但今天刚刚为一个应用构建了这个,发现其他答案中显示的设置不会产生干净的图像(可能是在以后的 .Net 版本中添加了新选项)。

    假设您希望文本位于位图的中心,您可以这样做:

    // Load the original image
    Bitmap bmp = new Bitmap("filename.bmp");
    
    // Create a rectangle for the entire bitmap
    RectangleF rectf = new RectangleF(0, 0, bmp.Width, bmp.Height);
    
    // Create graphic object that will draw onto the bitmap
    Graphics g = Graphics.FromImage(bmp);
    
    // ------------------------------------------
    // Ensure the best possible quality rendering
    // ------------------------------------------
    // The smoothing mode specifies whether lines, curves, and the edges of filled areas use smoothing (also called antialiasing). 
    // One exception is that path gradient brushes do not obey the smoothing mode. 
    // Areas filled using a PathGradientBrush are rendered the same way (aliased) regardless of the SmoothingMode property.
    g.SmoothingMode = SmoothingMode.AntiAlias;
    
    // The interpolation mode determines how intermediate values between two endpoints are calculated.
    g.InterpolationMode = InterpolationMode.HighQualityBicubic;
    
    // Use this property to specify either higher quality, slower rendering, or lower quality, faster rendering of the contents of this Graphics object.
    g.PixelOffsetMode = PixelOffsetMode.HighQuality;
    
    // This one is important
    g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
    
    // Create string formatting options (used for alignment)
    StringFormat format = new StringFormat()
    {
        Alignment = StringAlignment.Center,
        LineAlignment = StringAlignment.Center
    };
    
    // Draw the text onto the image
    g.DrawString("yourText", new Font("Tahoma",8), Brushes.Black, rectf, format);
    
    // Flush all graphics changes to the bitmap
    g.Flush();
    
    // Now save or use the bitmap
    image.Image = bmp;
    

    参考文献

    【讨论】:

    • 在我将GraphicsUnit.Pixel 添加到字体结构之前,我无法通过这个(或任何其他方法)获得成功的结果,我可能会错过什么?
    • @mkb 您需要展示自己的代码以查看可能出现的问题。这是我们系统中的工作生产代码。谢谢
    • 我需要添加var solidBrush = new SolidBrush (Color.White); g.FillRectangle (solidBrush, rectf); 来创建背景为白色和文本为黑色的位图。否则它完全是黑色的。
    • @PauloGuimarães 该示例假定在现有位图图像上绘图。听起来您从没有图像或黑色图像开始。显然,您需要调整颜色以适应您的情况。
    【解决方案3】:

    您需要使用Graphics 类才能在位图上书写。

    具体来说,DrawString 方法之一。

    Bitmap a = new Bitmap(@"path\picture.bmp");
    
    using(Graphics g = Graphics.FromImage(a))
    {
      g.DrawString(....); // requires font, brush etc
    }
    
    pictuteBox1.Image = a;
    

    【讨论】:

      【解决方案4】:
      var bmp = new Bitmap(@"path\picture.bmp");
      using( Graphics g = Graphics.FromImage( bmp ) )
      {
          g.DrawString( ... );
      }
      
      picturebox1.Image = bmp;
      

      【讨论】:

        【解决方案5】:

        如果你想wrap你的文字,那么你应该把你的文字画成一个矩形

        RectangleF rectF1 = new RectangleF(30, 10, 100, 122);
        e.Graphics.DrawString(text1, font1, Brushes.Blue, rectF1);
        

        见:https://msdn.microsoft.com/en-us/library/baw6k39s(v=vs.110).aspx

        【讨论】:

          猜你喜欢
          • 2014-10-29
          • 2013-08-27
          • 2013-04-07
          • 1970-01-01
          • 1970-01-01
          • 2011-08-20
          • 2012-07-04
          • 2020-12-06
          • 1970-01-01
          相关资源
          最近更新 更多