源程序下载地址:
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();
}