-3

I am a beginner of c#. I'm wish to save the picture to local disk from pictureBox1, so what's the step that i require to do?

private void button1_Click(object sender, EventArgs e)
    {

        Graphics g;
        int i = 9;
        int k;
        String[] letter1 = new String[9] { "b", "W", "b", "w", "B", "w", "B", "w", "B" };
        g= pictureBox1.CreateGraphics();
        Pen b = new Pen(Color.Black, 1.0f);
        Pen B = new Pen(Color.Black, 2.0f);
        Pen w = new Pen(Color.White, 1.0f);
        Pen W = new Pen(Color.White, 2.0f);

        for (int j = 0; j <= 8; j++)
        {
            String array = letter1[j];
            if (array.Equals("b") || array.Equals("w"))
            {
                i = i + 1;
                k = 50;
                if (array.Equals("b"))
                {
                    g.DrawLine(b, i, 10, i, k);
                }

            }
            else
                if (array.Equals("B") || array.Equals("W"))
                {
                    i = i + 2;
                    k = 51;
                    if (array.Equals("B"))
                        g.DrawLine(B, i, 10, i, k);
                }


        }
J. Steen
  • 15,470
  • 15
  • 56
  • 63
user1500886
  • 3
  • 1
  • 2
  • 1
    possible duplicate of [C# : How to save a picturebox control as a jpeg file after it's edited](http://stackoverflow.com/questions/1063505/c-sharp-how-to-save-a-picturebox-control-as-a-jpeg-file-after-its-edited) – CodeCaster Jul 04 '12 at 08:11

2 Answers2

2

If you create an empty Bitmap object and draw that instead then you can display the bitmap in the PictureBox and save it to disk when you are done.

Take a look at this question

    Bitmap bt;

    private void Form_Load(object sender, EventArgs e)
    {
        bt = new Bitmap(100,100);
        pictureBox1.Image = bt;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Graphics g = Graphics.FromImage(bt);

        int i = 9;
        int k;
        String[] letter1 = new String[9] { "b", "W", "b", "w", "B", "w", "B", "w", "B" };

        Pen b = new Pen(Color.Black, 1.0f);
        Pen B = new Pen(Color.Black, 2.0f);
        Pen w = new Pen(Color.White, 1.0f);
        Pen W = new Pen(Color.White, 2.0f);

        for (int j = 0; j <= 8; j++)
        {
            String array = letter1[j];
            if (array.Equals("b") || array.Equals("w"))
            {
                i = i + 1;
                k = 50;
                if (array.Equals("b"))
                {
                    g.DrawLine(b, i, 10, i, k);
                }

            }
            else
                if (array.Equals("B") || array.Equals("W"))
                {
                    i = i + 2;
                    k = 51;
                    if (array.Equals("B"))
                        g.DrawLine(B, i, 10, i, k);
                }

        }
        pictureBox1.Refresh();
        pictureBox1.Image.Save("c:\\test.bmp");

    }
Community
  • 1
  • 1
JonC
  • 978
  • 2
  • 7
  • 28