使用 C# 和 GDI+ 完全没有问题
这是简短的版本:
Bitmap合适的尺寸和分辨率Graphics.DrawImage和Graphics.DrawString创建图形PNG是这样可以保持文本清晰详情如下:
您发布的图片没有 DPI 设置和 1004x638 像素;将其设置为 150dpi 会导致物理尺寸为 170x108 毫米。让我们假设这没问题..
所以我们用这些参数创建一个位图:
Size keyCardSize = new Size (1004, 638);
float Dpi = 150f;
Bitmap bmp = new Bitmap(keyCardSize.Width, keyCardSize.Height);
bmp.SetResolution(Dpi, Dpi);
Run Code Online (Sandbox Code Playgroud)
查看您的模板,我们看到 6 个图像(徽标、照片和 4 个图标)和 7 个文本块(名称、职位、部门和四个联系人文本)。
绘制图像或文本的基本命令Graphics G如下:
G.DrawImage(bitmap, location);
G.DrawString(text, font, brush, location);
Run Code Online (Sandbox Code Playgroud)
在我们的例子中,这适用于徽标和照片,因为它们是左对齐的。您需要做的就是测量位置并创建位置点。确保您以正确的尺寸和分辨率创建图像, 或者调整 dpi 以弥补不同的像素尺寸!
更新:我已经包含了代码来调整照片dpi以使宽度适合模板中的 30 毫米框架。这假设比例是正确的和/或我们更关心宽度而不是高度..如果我们需要匹配两者,要么获得正确的比例,要么要求裁剪代码;-)
测量我们其余的项目有点复杂,因为它们都是右对齐的。以下是如何对字符串 txt1 执行此操作的示例:
SizeF s1 = G.MeasureString(txt1, font1, PointF.Empty, StringFormat.GenericTypographic);
G.DrawString(txt1, font1, brush1, new Point((int)(rightBorder - s1.Width), y1));
Run Code Online (Sandbox Code Playgroud)
您可以(并且必须)测量Y每个位置的顶部 ( ),但左侧 ( X) 必须按上述方式计算!
注意需要告诉测量命令你要使用的字体;忽略其他参数!测量右边界后,这些命令将适用于必要的数据和图形对象。
filename您的记录变得动态..4&5。请参阅此代码示例:
private void Button_Click(object sender, EventArgs e)
{
string FontFamily = "Verdana";
string fileName = "d:\\template.png";
Size keyCardSize = new Size (1004, 638); // 170x108mm
float Dpi = 150f;
Bitmap bmp = new Bitmap(keyCardSize.Width, keyCardSize.Height);
bmp.SetResolution(Dpi, Dpi);
// test data:
Bitmap photo = new Bitmap(@"D:\scrape\sousers\SOU_JonSkeet.jpeg");
// I have measured the photo should be 30mm wide
// so with 25.4mm to the inch we calculate a fitting dpi for it:
float photoDpi = photo.Width * 25.4f / 30f;
photo.SetResolution(photoDpi , photoDpi );
Font font1 = new Font(FontFamily, 23f);
Font font2 = new Font(FontFamily, 12f);
Font font3 = new Font(FontFamily, 14f);
using (Graphics G = Graphics.FromImage(bmp))
using (SolidBrush brush1 = new SolidBrush(Color.MediumVioletRed))
using (SolidBrush brush2 = new SolidBrush(Color.Gray))
{
G.Clear(Color.White);
G.InterpolationMode = InterpolationMode.HighQualityBicubic;
G.PageUnit = GraphicsUnit.Millimeter;
G.SmoothingMode = SmoothingMode.HighQuality;
StringFormat sf = StringFormat.GenericTypographic;
int lBorder= 10;
int rBorder= 160;
int y1 = 10;
int y2 = 25;
//..
int y4 = 60;
//..
//G.DrawImage(logo, imgLoc1);
G.DrawImage(photo, new Point(lBorder, y4));
//G.DrawImage(img3, imgLoc3);
//G.DrawImage(img4, imgLoc4);
//G.DrawImage(img5, imgLoc5);
// test data:
string txt1 = "Jon Skeet";
string txt2 = "C Sharp Evangelist";
//..
SizeF s1 = G.MeasureString(txt1, font1, PointF.Empty, sf);
SizeF s2 = G.MeasureString(txt2, font2, PointF.Empty, sf);
//..
G.DrawString(txt1, font1, brush1, new Point((int)(rBorder- s1.Width), y1));
G.DrawString(txt2, font2, brush2, new Point((int)(rBorder- s2.Width), y2));
//G.DrawString(txt3, font2, brush2, txtLoc3);
//..
//G.DrawString(txt7, font3, brush2, txtLoc7);
G.FillRectangle(brush1, new RectangleF(rBorder - 1.5f, 52f, 1.5f, 46f));
}
bmp.Save(fileName, ImageFormat.Png);
font1.Dispose();
font2.Dispose();
font3.Dispose();
photo.Dispose();
//..
}
Run Code Online (Sandbox Code Playgroud)
我希望这能让你继续前进。如果您有任何疑问,请随时提问..
这是初步结果:

事后的想法:您可能希望通过使用模板作为开始来简化整个代码:您可以将徽标、图标和垂直条都放在适当的位置,只需要绘制一个图像和文本..!