在绘图程序中保存位图

时间:2017-08-18 16:13:40

标签: c# winforms bitmap

我正在制作一个完全用c#编程的MS绘画程序。这是非常基本的,但我偶然发现了一个问题。所以我看到另一篇关于MS paint模拟的SO帖子。它是关于如何将最终结果保存为.bmp文件。我尝试使用提供的解决方案和答案,并且它有效。

保存文件。但是当它保存时,它只保存了空白面板(我正在制作一个表单应用程序)。我只看到一个处理此问题的SO帖子,但我无法合并以允许用户进行交互。以下是我的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

    namespace Paint
    {
    public partial class Form1 : Form
    {
    Graphics g;
    Pen p = new Pen(Color.Black, 1);
    Point sp = new Point(0, 0);
    Point ep = new Point(0, 0);
    int k = 0;

    public Form1()
    {
        InitializeComponent();
    }

    private void pictureBox2_Click(object sender, EventArgs e)
    {
        p.Color = red.BackColor;
        default1.BackColor = red.BackColor;


    }

    private void blue_Click(object sender, EventArgs e)
    {
        p.Color = blue.BackColor;
        default1.BackColor = blue.BackColor;
    }

    private void green_Click(object sender, EventArgs e)
    {
        p.Color = green.BackColor;
        default1.BackColor = green.BackColor;
    }



    private void panel2_MouseDown(object sender, MouseEventArgs e)
    {
        sp = e.Location;
        if (e.Button == MouseButtons.Left)
            k = 1;
    }

    private void panel2_MouseUp(object sender, MouseEventArgs e)
    {
        k = 0;
    }

    private void panel2_MouseMove(object sender, MouseEventArgs e)
    {
        if (k == 1)
        {
            ep = e.Location;
            g = panel2.CreateGraphics();
            g.DrawLine(p, sp, ep);

        }
        sp = ep;
    }



    private void panel2_MouseLeave(object sender, EventArgs e)
    {
        k = 0;        }

    private void panel2_Paint(object sender, PaintEventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {
        SaveFileDialog dialog = new SaveFileDialog();
        if (dialog.ShowDialog() == DialogResult.OK)
        {
            int width = Convert.ToInt32(panel2.Width);
            int height = Convert.ToInt32(panel2.Height);
            Bitmap bmp = new Bitmap(width, height);
            panel2.DrawToBitmap(bmp, new Rectangle(0, 0, width, height));
            bmp.Save(dialog.FileName, System.Drawing.Imaging.ImageFormat.Bmp);
        }
    }
}
}

所以我的问题是......我如何在我的c#表单应用程序中成功保存.bmp图像,就像我如何将它保存为空白一样。在此先感谢:)

修改

所以我尝试了第一个答案,我也尝试了个人在评论中提出的想法和一些如何,而不仅仅是保存空白画布。应用程序字面上只保存黑色图像。这是我最终得到的代码。我哪里出错了?

using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks; 
using System.Windows.Forms;

namespace Paint
{
public partial class Form1 : Form
{
    Graphics g;
    Graphics h;
    Pen p = new Pen(Color.Black, 1);
    Point sp = new Point(0, 0);
    Point ep = new Point(0, 0);
    int k = 0;
    Bitmap bmp =null;

    public Form1()
    {
        InitializeComponent();
    }

    private void pictureBox2_Click(object sender, EventArgs e)
    {
        p.Color = red.BackColor;
        default1.BackColor = red.BackColor;


    }

    private void blue_Click(object sender, EventArgs e)
    {
        p.Color = blue.BackColor;
        default1.BackColor = blue.BackColor;
    }

    private void green_Click(object sender, EventArgs e)
    {
        p.Color = green.BackColor;
        default1.BackColor = green.BackColor;
    }



    private void panel2_MouseDown(object sender, MouseEventArgs e)
    {
        sp = e.Location;
        if (e.Button == MouseButtons.Left)
            k = 1;
    }

    private void panel2_MouseUp(object sender, MouseEventArgs e)
    {
        k = 0;
    }

    private void panel2_MouseMove(object sender, MouseEventArgs e)
    {
        if (k == 1)
        {
            ep = e.Location;
            int width = Convert.ToInt32(panel2.Width);
            int height = Convert.ToInt32(panel2.Height);
            Bitmap bmp = new Bitmap(width, height);

            g = Graphics.FromImage(bmp);
            g.DrawLine(p, sp, ep);
            h = panel2.CreateGraphics();
            h.DrawLine(p, sp, ep);
        }
        sp = ep;
    }



    private void panel2_MouseLeave(object sender, EventArgs e)
    {
        k = 0;        }

    private void panel2_Paint(object sender, PaintEventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {
        SaveFileDialog dialog = new SaveFileDialog();
        if (dialog.ShowDialog() == DialogResult.OK)
        {
            /*
            Bitmap bmp = Bitmap.FromHbitmap(panel2.CreateGraphics().GetHdc());
            panel2.DrawToBitmap(bmp, new Rectangle(0, 0, width, height));*/
            int width = panel2.Width;
            int height = Convert.ToInt32(panel2.Height);
            if (bmp == null)

                bmp = new Bitmap(width, height);
            bmp.Save(dialog.FileName, System.Drawing.Imaging.ImageFormat.Jpeg);
        }
    }
}

}

2 个答案:

答案 0 :(得分:0)

这是一个修订版,几乎就是我在评论中告诉你的那些..:

public partial class Form2 : Form
{
    Graphics g;
    Graphics h;
    Pen p = new Pen(Color.Black, 1);
    Point sp = new Point(0, 0);
    Point ep = new Point(0, 0);
    int k = 0;
    Bitmap bmp =null;

    public Form2()
    {
        InitializeComponent();
        bmp = new Bitmap(panel2.ClientSize.Width, panel2.ClientSize.Height);
        g = Graphics.FromImage(bmp);
        g.Clear(panel2.BackColor);
    }

    private void pictureBox2_Click(object sender, EventArgs e)
    {
        p.Color = red.BackColor;
        default1.BackColor = red.BackColor;
     }

    private void color_Click(object sender, EventArgs e)
    {
        Control ctl = sender as Control;
        p.Color = ctl.BackColor;
        default1.BackColor = ctl.BackColor;
    }

    private void panel2_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            ep = e.Location;
            g.DrawLine(p, sp, ep);
            h = panel2.CreateGraphics();
            h.DrawLine(p, sp, ep);
        }
        sp = ep;
    }

    private void panel2_MouseDown(object sender, MouseEventArgs e)
    {
        sp = e.Location;
    }


    private void button1_Click(object sender, EventArgs e)
    {
        SaveFileDialog dialog = new SaveFileDialog();
        if (dialog.ShowDialog() == DialogResult.OK)
        {
            bmp.Save(dialog.FileName, System.Drawing.Imaging.ImageFormat.Jpeg);
        }
    }
}

一些注意事项:

  • 我将调色板控件的所有点击映射为一个。
  • 我通过移动中的按钮测试消除了标记k
  • 我保留了缓存的图形g;这通常不推荐,但是我们一直在绘制一个相同的位图,这是好的。
  • 我删除了位图bmp的所有重复声明。
  • 我不知道图片框的功能,所以我没有触及代码。

该决定的缺点:

  • 由于您没有跟踪所有动作,因此您无法做好撤消
  • 由于所有线条都是单独绘制的,因此您可以使用更宽和/或半透明的笔获得良好的效果,因为重叠的端点看起来很糟糕。

为了更好地解决简单的doodeling问题,请参阅here,在研究完之后,您可以解决更好的解决方案,这将允许您使用所有类型的绘图工具here ..

答案 1 :(得分:-1)

使用Graphics.GetHdc Method并将其保存为:

Bitmap bitMap = Bitmap.FromHbitmap(g.GetHdc());
bitMap.Save(dialog.FileName, System.Drawing.Imaging.ImageFormat.Bmp);