如何将图像绘制到PictureBox图像上

时间:2014-07-07 21:49:02

标签: c# winforms picturebox

我需要知道如何在PictureBox的图像上绘制多个图像。

我已经使用过此代码,但它不起作用:

    private void button3_Click(object sender, EventArgs e)
    {

Bitmap bmp = new Bitmap(pictureBox2.Image);

Graphics g = Graphics.FromImage(bmp);

g.DrawImage(new Bitmap(@"C:\Users\Mena\Desktop\1.png"), new Point(182, 213));

pictureBox2.Image = bmp;
    }

1 个答案:

答案 0 :(得分:1)

通过一些更改,您的代码可以正常工作:

private void button3_Click(object sender, EventArgs e)
{
    Bitmap bmp = new Bitmap(pictureBox2.Image);

    // whatever your plans where, we don't know ;-)
    // RectangleF rectf = new RectangleF(640F, 1100F, 0, 0);

    Graphics g = Graphics.FromImage(bmp);

    // DrawImage needs an image, not a string
    g.DrawImage(new Bitmap(@"C:\Users\Mena\Desktop\1.png"), new Point(182, 213));

    // flush is for finishing write operations
    // dispose is the command to get rid of GDI elements:
    g.Dispose();

    pictureBox2.Image = bmp;
}

建议的写作方式是:

private void button3_Click(object sender, EventArgs e)
{
    Bitmap bmp = new Bitmap(pictureBox2.Image);
    using (Graphics g = Graphics.FromImage(bmp))
    {
        g.DrawImage(new Bitmap((@"C:\Users\Mena\Desktop\1.png"), new Point(182, 213));
    }
    pictureBox2.Image = bmp;
}