[英]How to add border with text on image in c#?
我正在從字符串圖像創建圖像。 我想添加左邊框和下邊框,還想添加帶邊框的文本。 所以我已經通過谷歌進行了研發,但沒有得到適當的解決方案。 我得到如下圖像:
但是我需要像下面這樣的圖像(左半邊邊框,下半邊邊框,然后是下半邊邊框后的唯一 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 並且左側和底部有半邊框的圖像呢?
您可以從這個示例開始。
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);
}
我希望它有所幫助。 快樂編碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.