Another One Another One - 3 months ago 33
C# Question

C#, How do i save picture using savefiledialog for paint-like program

I've tried many methods including bitmap converting and etc.
Here's my code. Would love it someone will explain to me how to save it and why. Thanks!

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?
}

}
}

Answer

After you edited your question I suggest to do the following:

  • create a Bitmap member in your Form with the same dimensions as the panel
  • create the Graphics G from that bitmap
  • in your panel1_MouseMove draw on that Graphics (effectivly drawing into the bitmp`
  • in your panel1_Paint draw that bitmap on the panel and
  • in your button1_Click_2 save that bitmap:

Code example:

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);
        }
    }
}
Comments