C# 运用 System.Drawing 绘制卡片 (解决:去掉DrawString写出的字体黑边)

该功能配合卡片彩色打印机和RFID卡制作人员信息卡片,主要用于人员身份信息的核实。

引用 system.drawing

g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; //去掉字体黑边

效果图和源代码如下:
在这里插入图片描述

using System;
using System.Drawing;

namespace TestImage
{
   
    class Program
    {
        static void Main(string[] args)
        {

            Image image = Image.FromFile(@"C:\Users\MelanceXin\source\repos\TestImage\TestImage\bin\Debug\image.png");

            Image codeImage = Image.FromFile(@"C:\Users\MelanceXin\source\repos\TestImage\TestImage\bin\Debug\code.jpg");

            Person person = new Person
            {
                name = "曾振帅",
                org_name = "天津某某某某有限公司",
                personal_type_name = "现场负责人",
                work_type_name = "高压安装修造作业"
            };

            //保存制好之后的图片的完整路径
            string empcard_path = @"C:\Users\MelanceXin\source\repos\TestImage\TestImage\bin\Debug\pic\empcard.jpg";

            bool b = CreateImageModel(person, image, codeImage, empcard_path);


            image.Dispose();
            codeImage.Dispose();

            string result = b ? "制卡成功!!!!!" : "制卡失败!!";

            Console.WriteLine(result);
            Console.ReadKey();

        }


        /// <summary>
        /// 绘制卡片
        /// </summary>
        /// <param name="picimage"></param>
        /// <param name="codeimage"></param>
        /// <param name="empcard_path"></param>
        /// <returns></returns>
        public static bool CreateImageModel(Person person, Image picimage, Image codeimage, string empcard_path)
        {
            Image image = Image.FromFile(@"C: \Users\MelanceXin\source\repos\TestImage\TestImage\bin\Debug\0.png");
            Bitmap bitmap = new Bitmap(image);
            Graphics g = Graphics.FromImage(image);
            try
            {
                bitmap = new Bitmap(image, new Size(1024, 638));
                bitmap.SetResolution(300, 300);
                g = Graphics.FromImage(bitmap);

                //标题
                StringFormat format = new StringFormat();
                format.LineAlignment = StringAlignment.Center;  // 更正: 垂直居中
                format.Alignment = StringAlignment.Center;      // 水平居中
                StringFormat format1 = new StringFormat();
                format1.LineAlignment = StringAlignment.Center;  // 更正: 垂直居中
                format1.Alignment = StringAlignment.Near;      // 水平居左
                StringFormat format2 = new StringFormat();
                format2.LineAlignment = StringAlignment.Center;  // 更正: 垂直居中
                format2.Alignment = StringAlignment.Far;      // 水平居右


                //证件照
                Image id_image = picimage;
                RectangleF id_rec = new Rectangle(new Point(130, 140), new Size(250, 320));
                g.DrawImage(id_image, id_rec);
                //二维码
                Image qrcode_image = codeimage;
                //Image qrcode_image = Image.FromFile(@_empinfo.Qrcode_path);
                RectangleF qrcode_rec = new Rectangle(new Point(630, 240), new Size(320, 320));
                g.DrawImage(qrcode_image, qrcode_rec);


                g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;   //去掉字体黑边

                //文字信息
                //人员姓名
                RectangleF nameval_rec = new Rectangle(new Point(20, 470), new Size(480, 60));
                g.DrawString(person.name, new Font("黑体", 12f, (FontStyle.Bold)), new SolidBrush(Color.FromArgb(95, 95, 95)), nameval_rec, format);
                //公司名称
                RectangleF coval_rec = new Rectangle(new Point(20, 510), new Size(480, 100));
                //g.DrawString(person_.org_name, new Font("黑体", 7f), Brushes.LightGray, coval_rec, format);
                g.DrawString(person.org_name, new Font("宋体", 7f), new SolidBrush(Color.FromArgb(95, 95, 95)), coval_rec, format);
                //人员类别
                RectangleF type_rec = new Rectangle(new Point(600, 50), new Size(340, 60));
                g.DrawString(person.personal_type_name, new Font("微软雅黑", 10f), Brushes.White, type_rec, format2);
                工种
                RectangleF worktype_rec = new Rectangle(new Point(600, 110), new Size(340, 50));
                g.DrawString(person.work_type_name, new Font("微软雅黑", 7f), Brushes.White, worktype_rec, format2);


                bitmap.Save(empcard_path);
            }
            finally
            {
                image.Dispose();
                bitmap.Dispose();
                g.Dispose();
            }
            return true;
        }

    }
    public class Person
    {
        public string name;
        public string org_name;
        public string personal_type_name;
        public string work_type_name;

    }
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
C#是一种通用的面向对象编程语言,它具有强大的绘图功能。在C#中,可以使用多种方式进行绘图,包括使用Windows Forms、WPF、GDI+等库。 如果你想在C#中进行绘图,可以使用Windows Forms库。Windows Forms提供了一组用于创建图形用户界面的类和控件,其中包括了绘图相关的功能。你可以创建一个继承自Form类的窗体,并在其Paint事件中编写绘图代码。通过Graphics对象,你可以使用各种方法和属性来绘制图形、文本、图像等。 以下是一个简单的示例代码,演示了如何在C#中使用Windows Forms进行绘图: ```csharp using System; using System.Drawing; using System.Windows.Forms; public class MyForm : Form { public MyForm() { // 设置窗体大小和标题 Size = new Size(400, 300); Text = "绘图示例"; } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); // 获取绘图对象 Graphics g = e.Graphics; // 创建画刷和画笔 Brush brush = new SolidBrush(Color.Red); Pen pen = new Pen(Color.Blue, 2); // 绘制矩形 Rectangle rect = new Rectangle(50, 50, 200, 100); g.FillRectangle(brush, rect); g.DrawRectangle(pen, rect); // 绘制文本 Font font = new Font("Arial", 12); string text = "Hello, C#"; g.DrawString(text, font, Brushes.Black, 100, 120); // 释放资源 brush.Dispose(); pen.Dispose(); font.Dispose(); } } public class Program { public static void Main() { Application.Run(new MyForm()); } } ``` 这段代码创建了一个继承自Form类的窗体,重写了其OnPaint方法,在其中进行绘图操作。在窗体上绘制了一个红色矩形和一段文本
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MelanceXin

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值