如何使用getpixel从pictureBox.Image和Bitmap bmp创建新图像?

时间:2012-07-01 12:13:26

标签: c#

我试图使用Graphics.DrawImage,但我得到黑色图像,并在其上红点我正在绘制。为什么我会得到黑色图像?

这是功能:

public Bitmap SaveFromPictureBoxToBitMap(Bitmap image1, Image pbox)
        {

            Graphics g = Graphics.FromImage(image1);
            Point p = new Point(pbox.Width,pbox.Height);
            g.DrawImage(pbox, p);
            image1.Save(@"d:\PictureBoxToBitmap\ptob.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
            return newImage;
        }

image1是我在绘画事件中绘制的bmp变量。而pbox只是pictureBox1.Image和我在trackBar1滚动事件中使用此函数。

这就是我在这里添加截图:

enter image description here

在背景上而不是黑色我需要从pictureBox1获取图像,但它有效吗?

1 个答案:

答案 0 :(得分:0)

找到它:

using (Bitmap b = new Bitmap(pictureBox1.Width, pictureBox1.Height))
{
    pictureBox1.DrawToBitmap(b, new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height));
    b.Save(@"d:\testit.png", System.Drawing.Imaging.ImageFormat.Png);
}