public Form1()
{
InitializeComponent();
//label存入Picturebox
pictureBox1.Controls.Add(label1);
pictureBox1.Controls.Add(label2);
} private void button1_Click(object sender, EventArgs e)
{
//Picturebox+label合并转换成图片
foreach (Label l in pictureBox1.Controls)
{ using (Brush brush = new SolidBrush(l.ForeColor))
using (Graphics g = Graphics.FromImage(pictureBox1.Image))
{
Rectangle rect = new Rectangle(l.Left - pictureBox1.Left, l.Top - pictureBox1.Top, l.Width, l.Height);
if (l.BackColor != Color.Transparent)
{
using (Brush bgBrush = new SolidBrush(l.BackColor))
{
g.FillRectangle(bgBrush, rect);
}
}
g.DrawString(l.Text, l.Font, brush, rect, StringFormat.GenericTypographic);
}
}
pictureBox1.Image.Save("d:\\abc.png", System.Drawing.Imaging.ImageFormat.Png); //文字转换成图片
//string str =label1.Text;
//Graphics g = Graphics.FromImage(new Bitmap(1, 1));
//Font font = new Font("宋体", 9);
//SizeF sizeF = g.MeasureString(str, font); //测量出字体的高度和宽度
//Brush brush; //笔刷,颜色
//brush = new SolidBrush(label1.ForeColor);
//PointF pf = new PointF(0, 0);
//Bitmap img = new Bitmap(Convert.ToInt32(sizeF.Width), Convert.ToInt32(sizeF.Height));
//g = Graphics.FromImage(img);
//g.DrawString(str, font, brush, pf);
////输出图片
//img.Save("d://123.jpg", System.Drawing.Imaging.ImageFormat.Gif); }