【问题标题】:C#, How do i save picture using savefiledialog for paint-like programC#,如何使用 savefiledialog 为类绘画程序保存图片
【发布时间】:2016-08-10 00:00:19
【问题描述】:

我尝试了很多方法,包括位图转换等。 这是我的代码。会喜欢它,有人会向我解释如何保存它以及为什么。谢谢!

public partial class Form1 : Form
{
    Graphics G;
    Pen myPen = new Pen(Color.Black);
    Point sp = new Point(0, 0);
    Point ep = new Point(0, 0);
    int ctrl = 0;

    public Form1()
    {
        InitializeComponent();
    }

    private void panel1_Paint(object sender, PaintEventArgs e)
    {

    }

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

    private void panel1_MouseUp(object sender, MouseEventArgs e)
    {
        ctrl = 0;
    }

    private void panel1_MouseMove(object sender, MouseEventArgs e)
    {
        if(ctrl == 1)
        {
            ep = e.Location;
            G = panel1.CreateGraphics();
            G.DrawLine(myPen, sp, ep);
        }
        sp = ep;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        colorDialog1.ShowDialog();
        myPen.Color = colorDialog1.Color;
        colourBtn.BackColor = colorDialog1.Color;
    }

    private void clrBtn_Click(object sender, EventArgs e)
    {
        G.Clear(colorDialog2.Color);
    }

    private void button1_Click_1(object sender, EventArgs e)
    {
        colorDialog2.ShowDialog();
        panel1.BackColor = colorDialog2.Color;
        panel1Colourbtn.BackColor = colorDialog2.Color;

    }

    private void button1_Click_2(object sender, EventArgs e)
    {
        SaveFileDialog dlgSave = new SaveFileDialog();
        dlgSave.Title = "Save Image";
        dlgSave.Filter = "Bitmap Images (*.bmp)|*.bmp|All Files (*.*)|*.*";
        if (dlgSave.ShowDialog(this) == DialogResult.OK)
        {

            using (Bitmap bmp = new Bitmap(panel1.Width, panel1.Height))
            {
               // how do i save my drawing using savefiledialog? 
            }

        }
    }

【问题讨论】:

  • BitmapSave()方法怎么样?
  • 你的问题不是保存,而是绘图!错误从这里开始:G = panel1.CreateGraphics(); - 一旦您仅从绘制事件中绘制,您就可以使用DrawToBitmap 创建可保存的位图。现在你需要重写绘图代码! - See here for more cooments and links about the very same issues
  • 你的意思是我只需要在绘画事件中编写代码吗?
  • CreateGraphics 是屏幕上的临时绘图。如果您最小化表格,它很容易被删除。始终使用控件的绘制事件或从位图生成图形对象。
  • 在工作时,接受的答案将无法执行,也不允许撤消。绘画程序应该允许在各种画布上绘画。当您有时间时,您可能想研究我提供的链接以获得更好和更可扩展的解决方案..

标签: c#


【解决方案1】:

编辑问题后我建议执行以下操作:

  • 在您的表单中创建一个与面板尺寸相同的Bitmap 成员
  • 从该位图创建Graphics G
  • 在您的panel1_MouseMove 上绘制Graphics(有效地绘制到bitmp 中`
  • 在您的panel1_Paint 在面板上绘制该位图并
  • 在您的button1_Click_2 中保存该位图:

代码示例:

public partial class Form1 : Form
{
    Bitmap bmp;
    Graphics G;
    Pen myPen = new Pen(Color.Black);
    Point sp = new Point(0, 0);
    Point ep = new Point(0, 0);
    int ctrl = 0;

    public Form1()
    {
        InitializeComponent();

        // create bitmap
        bmp = new Bitmap(panel1.Width, panel1.Height);

        // create Graphics
        G = Graphics.FromImage(bmp);
        G.Clear(Color.Black);

        // redraw panel
        panel1.Invalidate();
    }

    private void panel1_Paint(object sender, PaintEventArgs e)
    {
        // draw bitmap on panel
        if (bmp != null)
            e.Grahics.DrawImage(bmp, Point.Empty);
    }

    // shortened for clarity

    private void panel1_MouseMove(object sender, MouseEventArgs e)
    {
        if(ctrl == 1)
        {
            ep = e.Location;
            // draw onto graphics -> bmp
            G.DrawLine(myPen, sp, ep);
        }
        sp = ep;

        // redraw panel
        panel1.Invalidate();
    }

    private void button1_Click_2(object sender, EventArgs e)
    {
        SaveFileDialog dlgSave = new SaveFileDialog();
        dlgSave.Title = "Save Image";
        dlgSave.Filter = "Bitmap Images (*.bmp)|*.bmp|All Files (*.*)|*.*";
        if (dlgSave.ShowDialog(this) == DialogResult.OK)
        {
             bmp.Save(dlgSave.FileName);
        }
    }
}

【讨论】:

  • 更新了两行(panel1_Paintpanel1.Invalidate() 中的空值检查 panel1_MouseMove
  • 非常感谢您花时间为我重写代码。出于某种原因,我的 panel1 现在是黑色的。
  • @AnotherOne 比用文字解释更容易:) 而且,是的,你没有编辑,也许我第一次没有正确阅读,以为代码改变了一点......跨度>
  • 没关系,我添加了 G.Clear(Color.White) 而不是 G.Clear(Color.Black) 并修复了它!我不能感谢你!非常感谢!
  • 很抱歉再次打扰您,但是panel1的背景色没有和图纸一起保存。有什么办法吗?
【解决方案2】:

试试这个:

panel1.DrawToBitmap(bmp, new Rectangle(0, 0, panel1.Width, panel1.Height));
bmp.Save(dlgSave.FileName);

【讨论】:

  • 我之前用过那个方法,但是只保存了面板的背景色,没有保存图纸。
  • @AnotherOne 看来您的“绘图”技术是错误的。您应该在位图实例上执行绘图(可能由鼠标操作触发)并使用面板的Paint 事件在面板上绘制位图。然后你可以简单地将这个位图实例保存在你的保存按钮处理程序中。
  • 无法撤回我的投票,但是,是的,绘图技术是错误的,建议在鼠标移动期间绘制位图然后该位图在性能方面是个坏主意!仅在paintevent 中的表面上绘制,然后(并且仅在那时)DrawToBitmap 以保存将正常工作。
  • @RenéVogt,那么我应该在绘画活动中写些什么呢? G.DrawLine 还是?
  • 顺便谢谢你的回答
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-23
  • 1970-01-01
相关资源
最近更新 更多