-3

我是c#的初学者。我希望将图片从 pictureBox1 保存到本地磁盘,那么我需要执行哪些步骤?

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


        }
4

2 回答 2

2

如果您创建一个空的 Bitmap 对象并绘制它,那么您可以在 PictureBox 中显示该位图并在完成后将其保存到磁盘。

看看这个问题

    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");

    }
于 2012-07-04T08:19:08.820 回答
0

在picturebox.image中有一个save方法

信息:http: //msdn.microsoft.com/en-us/library/system.drawing.image.save.aspx

http://msdn.microsoft.com/en-us/library/system.windows.forms.picturebox.image.aspx

于 2012-07-04T08:11:54.020 回答