如何将pictureBox1中的当前帧/图像保存到位图文件?

时间:2012-06-30 21:56:16

标签: c# .net picturebox

在Form1的顶部,我做了:Bitmap bmp;在绘制事件中,我正在绘制pictureBox以及bmp文件:

private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
    SolidBrush brush;
    Pen p = null;
    Point connectionPointStart;
    Point connectionPointEnd;
    Graphics g = e.Graphics;
    bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
    Graphics bitmapGraphics = Graphics.FromImage(bmp);
    //g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

    moveCounter++;
    label6.Text = moveCounter.ToString();

    brush = new SolidBrush(Color.Red);
    p = new Pen(brush);
    for (int idx = 0; idx < wireObject1._point_X.Count; ++idx)
    {
        Point dPoint = new Point((int)wireObject1._point_X[idx], (int)wireObject1._point_Y[idx]);
        dPoint.X = dPoint.X - 5; // was - 2
        dPoint.Y = dPoint.Y - 5; // was - 2
        Rectangle rect = new Rectangle(dPoint, new Size(10, 10));
        g.FillEllipse(brush, rect);
        bitmapGraphics.FillEllipse(brush, rect);

        // g.FillEllipse(brush, rect);
    }

    for (int i = 0; i < wireObject1._connectionstart.Count; i++)
    {
        int startIndex = wireObject1._connectionstart[i];
        int endIndex = wireObject1._connectionend[i];

        connectionPointStart = new Point((int)wireObject1._point_X[startIndex], (int)wireObject1._point_Y[startIndex]);
        connectionPointEnd = new Point((int)wireObject1._point_X[endIndex], (int)wireObject1._point_Y[endIndex]);
        p.Width = 2;
        g.DrawLine(p, connectionPointStart, connectionPointEnd);
        bitmapGraphics.DrawLine(p, connectionPointStart, connectionPointEnd);
    }
}

我在bmp文件的paint事件中做了一个新实例。我还为bmp文件创建了一个新的图形变量。我绘制或填充椭圆到图片框的每个地方我也将它绘制到bmp文件。

现在在保存功能中我做了:

private void SavePictureBoxToBitmap()
{
    //PbToBitmap++;
    //String tempFile = @"d:\PictureBoxToBitmap\" + PbToBitmap.ToString("D6") + bmp;
    bmp.Save(@"d:\PictureBoxToBitmap\bit.bmp");
}

如果我正在做bmp.Save它将保存到硬盘上只有里面有图纸的白色bmp文件。如果我正在做pictureBox1.Image.Save它将保存到硬盘上的文件与pictureBox中的图片只有没有图纸。

我如何组合它以便我将创建一个保存行/命令,它将在硬盘上创建一个bmp文件,其中包含pictureBox图像和图纸?

2 个答案:

答案 0 :(得分:0)

如果要在Image上绘制内容,请使用静态函数FromBitmap创建Graphics元素:

Graphics gfx = Graphics.FromImage(pictureBox1.Image)

然后使用gfx实例在图片框上绘制内容,例如: gfx.DrawLine。

然后保存它,使用pictureBox1.Image.Save函数。

<强>更新

你的代码对我来说有点过于神秘,但我做了一个简单的例子,可能会回答你的问题。我创建了一个newq空表单,添加了一个图片框,然后实现了Paint函数。

    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        Graphics gfx = Graphics.FromImage(pictureBox1.Image);
        gfx.DrawLine(new Pen(Color.Red, 5), new Point(10, 10), new Point(20, 20));
        gfx.DrawLine(new Pen(Color.Red, 5), new Point(20, 10), new Point(10, 20));
        pictureBox1.Image.Save("test.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
        pictureBox1.Refresh(); // The file will be correct without this, but the update will not be shown
    }

答案 1 :(得分:0)

using (var bmp = new Bitmap(panel1.Width, panel1.Height))
{
pictureBox1.DrawToBitmap(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height));
bmp.Save("output.png", System.Drawing.Imaging.ImageFormat.Jpeg);
}

应该工作