【发布时间】: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