一. 图片合成
- 确认两张素材图片的像素尺寸,决定合成方法(拼接/叠加),根据方法,计算出合成新图片的尺寸。
将图2放入图1右下黑色区域方法如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private Bitmap bit1;//(图片1)
private Bitmap bit2;//(图片2)
//合并图片
private void button1_Click(object sender, EventArgs e)
{
Bitmap bit = new Bitmap(344, 384);//新建大小为344×384大小的画布
Graphics graphics = Graphics.FromImage(bit);
graphics.DrawImage(bit1, 0, 0, 344, 384); //绘制图片,并指定其位置,
//DrawImage方法内参数(图片名(变量类型:Image),图片左上角x坐标(int),图片左上角y坐标(int),图片宽度(int),图片高度(int))
graphics.DrawImage(bit2, 115,344,229,40);
this.pictureBox3.Image = bit;//最终合并成功的图片
}
}
二. 图片添加文字
在上述图片中再添加文字的方法:
private Bitmap bit1;//上提示图片(图片1)
private Bitmap bit2;//验证码图片(图片2)
//合并图片
private void button1_Click(object sender, EventArgs e)
{
Bitmap bit = new Bitmap(344, 384);//新建大小为200×230大小的画布
Graphics graphics = Graphics.FromImage(bit);
graphics.DrawImage(bit1, 0, 0, 344, 384); //绘制图片,并指定其位置
graphics.DrawImage(bit2, 115,344,229,40);
DrawNumber(pictureBox3, graphics);//调用添加文字方法,在pictureBox3中显示
this.pictureBox3.Image = bit;//最终合并成功的图片
}
//图片1
private void pictureBox1_DoubleClick(object sender, EventArgs e)
{
if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
{
string path = this.openFileDialog1.FileName;//获得图片路径
bit1 = new Bitmap(path);
this.pictureBox1.Image = bit1;
}
}
//图片2
private void pictureBox2_DoubleClick(object sender, EventArgs e)
{
if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
{
string path = this.openFileDialog1.FileName;//获得图片路径
bit2 = new Bitmap(path);
this.pictureBox2.Image = bit2;
}
}
//图片上添加文字方法
//要添加的文字内容与位置
public static void DrawNumber(PictureBox pictureBox, Graphics g)
{
DrawString(g, "点击左边的字", 115, 300);
}
//添加的文字字体,字号,颜色等信息
private static void DrawString(Graphics g, string s, float x, float y)
{
g.DrawString(s, new System.Drawing.Font("????", 16, FontStyle.Bold), new System.Drawing.SolidBrush(System.Drawing.Color.Red), x, y);
}