问题描述
我正在根据字符串图像创建图像。我想添加左边框和下边框,还想添加带边框的文本。所以我已经在Google上进行了研发,但没有为此找到合适的解决方案。 我得到的图像如下:
但是我需要下面的图像(左侧的半边框,底部的半边框,然后在底部的半边框之后有唯一的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);
string fileName = Guid.NewGuid().ToString() + ".png";
var newImage = new Bitmap(bitmap.Width,bitmap.Height + 50);
var gr = Graphics.FromImage(newImage);
gr.DrawImageUnscaled(bitmap,0);
gr.DrawString("uniqueId",SystemFonts.DefaultFont,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,height + 5);
drawHorizontalLine(drawing,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,int height)
{
graphics.DrawLine(pen,y+height);
}
我希望它会有所帮助。编码愉快。