C#实现在图片上添加文字和二维码

源程序下载地址:
https://download.csdn.net/download/u012577474/11224784
原始素材:
在这里插入图片描述
我的公众号二维码图片:
在这里插入图片描述

运行程序后的效果图:
在这里插入图片描述

源代码如下:

添加图片方法:
DrawImage(Image, 画图位置x, 画图位置y, 图片宽, 图片高);
添加文字方法:
DrawString(文字, 字体, 画笔, 位置);

//窗口加载事件
     private void Form1_Load(object sender, EventArgs e)
            {
                pictureBox1.Image = Image.FromFile("C:/Users/Administrator/Desktop/c#picture/1.jpg"); //设置背景图片
                string imgPath = "C:/Users/Administrator/Desktop/c#picture/IT爱好者.jpg";  //要插入的二维码图片路径
                Image QRcodePic; //用来存储读取的二维码图片
                //读取二维码图片文件流
                FileStream fileStream = new FileStream(imgPath, FileMode.Open, FileAccess.Read);
                int byteLength = (int)fileStream.Length;    //二维码图片字节数
                byte[] fileBytes = new byte[byteLength];    //根据图片字节数创建一个存储该图片的字节数组
                fileStream.Read(fileBytes, 0, byteLength);  //读取二维码图片到数组
                fileStream.Close();                 //关闭文件流,解除对外部文件的锁定
                //取得二维码图片image对象
                QRcodePic = Image.FromStream(new MemoryStream(fileBytes));  
                Graphics g = Graphics.FromImage(pictureBox1.Image);  //创建背景图片的Graphics对象(调用该对象在背景图片上绘图)
                //在背景图片上插入二维码图片
                g.DrawImage(QRcodePic, 500, 120, QRcodePic.Width, QRcodePic.Height);
                //在背景照片上添加文字 
                PointF drawPoint = new PointF(55.0F, 160.0F);//
                AddFont(g, drawPoint, "Name:Yfw");
                drawPoint = new PointF(55.0F, 220.0F);//
                AddFont(g, drawPoint, "MoviePark:IT爱好者");
                drawPoint = new PointF(55.0F, 280.0F);//
                AddFont(g, drawPoint, "Blog:blog.csdn.net/u012577474");
                //刷新pictureBox1
                pictureBox1.Refresh(); 
            }
     /*在图片上添加文字
     * Graphics g  ,目标Graphics对象     
     * string data  ,准备添加的字符串
     */
	    private void AddFont(Graphics g, PointF drawPoint, string data)
	    {
		     //   Graphics g = Graphics.FromImage(pictureBox1.Image);
		        SolidBrush mybrush;
		        mybrush = new SolidBrush(Color.Red);  //设置默认画刷颜色
		        Font myfont;
		        myfont = new Font("黑体", 24,FontStyle.Bold);         //设置默认字体格式   
		        g.DrawString(data, myfont, mybrush, drawPoint); //图片上添加文字
		        pictureBox1.Refresh();
	
	    }
  • 3
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Yfw&武

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

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

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

打赏作者

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

抵扣说明:

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

余额充值