【问题标题】:unable to save images from picturebox to datagridview无法将图片从图片框保存到 datagridview
【发布时间】:2014-03-04 17:16:01
【问题描述】:

我看过this 教程,介绍如何使用光标捕获屏幕。 现在我添加了一个计时器和 datagridview,我想将每个捕获保存在 datagridview 中。这是我所做的:

private void Display(Bitmap desktop)
{
    Graphics g;
    Rectangle r;
    if (desktop != null)
    {
        r = new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height);
        g = pictureBox1.CreateGraphics();
        g.DrawImage(desktop, r);
        g.Flush();

            Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height, g);

        dataGridView1.Rows.Add(bmp);
    }
}

但我得到的只是这样的白色图像:

我无法保存picturebox 上出现的内容并将其添加到datagridview

【问题讨论】:

    标签: c# winforms visual-studio datagridview


    【解决方案1】:

    使用 CreateGraphics 是屏幕上的临时绘图,因此图像不会传输到位图。

    尝试直接画图:

    private void Display(Bitmap desktop) {
      if (desktop != null) {
        Bitmap bmp = new Bitmap(pictureBox1.ClientSize.Width, 
                                pictureBox1.ClientSize.Height);
        using (Graphics g = Graphics.FromImage(bmp)) {
          g.DrawImage(desktop, Point.Empty);
        }
        pictureBox1.Image = bmp;
        dataGridView1.Rows.Add(bmp);
      }
    }
    

    【讨论】:

    • 它确实将捕获保存在datagridview,但如何在每次添加到数据网格视图之前在picturebox 中查看它
    • @MagedEWilliam 用 PictureBox Image 属性更新了帖子,显示了相同的位图。
    • 很好,但是如何以一半的结果做到jepg
    • @MagedEWilliam 这听起来像是一个不同的问题。不确定half the resolution 是什么意思,但你可以做一个bmp.Save(filename, ImageFormat.Jpeg);
    • 哦,设置为 1366x768,我需要是 683、384
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-23
    • 2012-08-08
    相关资源
    最近更新 更多