使用C#和GDI+完全没问题
这是简短的版本:
- 创建一个大小和分辨率合适的
Bitmap
- 测量您想要在其上放置的图像和文本的坐标
- 从数据库中提取数据
- 使用
Graphics.DrawImage 和Graphics.DrawString 创建图形
- 以您喜欢的格式保存,最好是
PNG,因为这样可以保持文本清晰
这里有详细信息:
- 您需要计算图像应该有多少像素。由于您希望能够打印它,您应该使用至少 150dpi 或 200dpi。
您发布的图片没有 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);
- 下面的代码将使用毫米作为单位。您可能希望将其更改为其他内容。
查看您的模板,我们看到 6 张图片(徽标、照片和 4 个图标)和 7 块文本(姓名、职位、部门和四个联系文本)。
使用Graphics G 绘制图像或文本的基本命令如下所示:
G.DrawImage(bitmap, location);
G.DrawString(text, font, brush, location);
在我们的例子中,这适用于徽标和照片,因为它们是左对齐的。您需要做的就是测量位置并创建定位点。确保您要么以正确的尺寸和分辨率创建图像或调整 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));
您可以(并且必须)测量每个位置的顶部 (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();
//..
}
我希望这能让你继续前进。如果您有任何问题,请随时提问。。
这是初步的结果:
事后的想法:您可能希望通过使用模板作为开始来简化整个代码:您可以将徽标、图标和垂直条都放在适当的位置,只需要绘制一个图像和文本......!