【问题标题】:How to add border with text on image in c#?如何在 C# 中的图像上添加带有文本的边框?
【发布时间】:2020-08-11 06:36:19
【问题描述】:

我正在从字符串图像创建图像。我想添加左边框和下边框,还想添加带边框的文本。所以我已经通过谷歌进行了研发,但没有得到适当的解决方案。 我得到如下图像:

但是我需要如下图(左侧半边框,下半边框,然后是下半边框后的唯一 ID):

那么如何实现它。我在 c# 中工作,我的代码如下所示:

string fullName = name.Trim();
Bitmap bitmap = new Bitmap(1, 1, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
string fontName = _fontfamily + ".ttf";
PrivateFontCollection privateFontCollection = new PrivateFontCollection();
privateFontCollection.AddFontFile(Server.MapPath("~/Content/fontCss/" + fontName));
FontFamily ff = privateFontCollection.Families[0];
Font font = new Font(ff, 25, FontStyle.Regular, GraphicsUnit.Pixel);
Graphics graphics = Graphics.FromImage(bitmap);
int width = (int)graphics.MeasureString(fullName, font).Width;
int height = (int)graphics.MeasureString(fullName, font).Height;
bitmap.MakeTransparent(Color.Transparent);
bitmap = new Bitmap(bitmap, new Size(width, height));
graphics = Graphics.FromImage(bitmap);
graphics.Clear(Color.Transparent);
graphics.SmoothingMode = SmoothingMode.AntiAlias;
graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
graphics.DrawString(fullName, font, Brushes.Black, 0, 0);
string fileName = Guid.NewGuid().ToString() + ".png";

var newImage = new Bitmap(bitmap.Width, bitmap.Height + 50);
var gr = Graphics.FromImage(newImage);
gr.DrawImageUnscaled(bitmap, 0, 0);
gr.DrawString("uniqueId", SystemFonts.DefaultFont, Brushes.Black, new RectangleF(0, bitmap.Height, bitmap.Width, 50));
newImage.Save(Server.MapPath("~/UploadedDocuments/") + fileName, ImageFormat.Png);

那么我们如何才能得到我这里显示的具有唯一 id 并且左侧和底部有半边框的图像?

【问题讨论】:

    标签: c# image graphics bitmap digital-signature


    【解决方案1】:

    您可以从这个示例开始。

    var img = DrawText("Tonton", "Unique Id(12345)");
    
    private Image DrawText(String text, String subText)
        {
            var textColor = Color.Black;
            var backColor = Color.Transparent;
    
            var textFont = new Font("Arial", 15);
            Brush textBrush = new SolidBrush(textColor);
    
            var subTextFont = new Font("Arial", 7);
            Brush subTextBrush = new SolidBrush(textColor);
    
            //first, create a dummy bitmap just to get a graphics object
            Image img = new Bitmap(1, 1);
            Graphics drawing = Graphics.FromImage(img);
    
            //measure the string to see how big the image needs to be
            SizeF textSize = drawing.MeasureString(text, textFont);
            SizeF subTextSize = drawing.MeasureString(subText, subTextFont);
    
            //free up the dummy image and old graphics object
            img.Dispose();
            drawing.Dispose();
    
            //create a new image of the right size
            img = new Bitmap(((int)textSize.Width + (int)subTextSize.Width) - ((int)textSize.Width / 3), (int)textSize.Height + 15);
    
            drawing = Graphics.FromImage(img);
            drawing.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit;
    
            //paint the background
            drawing.Clear(backColor);
            
            //Draw Text
            drawing.DrawString(text, textFont, textBrush, 5, 0);
    
            //Drawing Line
            var linePen = new Pen(Color.Black, 3);
            var height = (int)textSize.Height;
            var width = (int)textSize.Width;
    
            drawVerticalLine(drawing, linePen, 1, 0, height + 5);
            drawHorizontalLine(drawing, linePen, 0, height + 5, width / 3);
    
            //Darw Subtext
            drawing.DrawString(subText, subTextFont, subTextBrush, width / 3, height);
    
            drawing.Save();
    
            textBrush.Dispose();
            drawing.Dispose();
    
            return img;
        }
    
        private void drawHorizontalLine(Graphics graphics, Pen pen, int x, int y, int width)
        {
            graphics.DrawLine(pen, x, y, x+width, y);
        }
    
        private void drawVerticalLine(Graphics graphics, Pen pen, int x, int y, int height)
        {
            graphics.DrawLine(pen, x, y, x, y+height);
        }
    

    输出

    我希望它有所帮助。编码愉快。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-13
      • 1970-01-01
      • 1970-01-01
      • 2016-08-17
      • 2017-10-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多