0

我制作了一个程序,允许用户在图片框图像上画线,但现在需要保存这些线以便以后打开。这是我当前绘制线条的代码:

 public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            pictureBox1.Image = new Bitmap(pictureBox1.Width, pictureBox1.Height);
        }
        int Drawshape;




        private Point p1, p2;
        List<Point> p1List = new List<Point>();
        List<Point> p2List = new List<Point>();

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {


        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            Drawshape = 5;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Drawshape = 2;
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (Drawshape == 5)
            {
                if (p1.X == 0)
                {
                    p1.X = e.X;
                    p1.Y = e.Y;
                }
                else
                {
                    p2.X = e.X;
                    p2.Y = e.Y;

                    p1List.Add(p1);
                    p2List.Add(p2);

                    pictureBox1.Invalidate();
                    p1.X = 0;
                }
            }
        }

        private void pictureBox1_ParentChanged(object sender, EventArgs e)
        {

        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            Graphics G = Graphics.FromImage(pictureBox1.Image);
            if (Drawshape == 5)
            {
                using (var p = new Pen(Color.Blue, 4))
                {
                    for (int x = 0; x < p1List.Count; x++)
                    {
                        G.DrawLine(p, p1List[x], p2List[x]);
                    }
                }
            }

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            pictureBox1.Invalidate();
        }

        private void Save_Click(object sender, EventArgs e)
        {



        } 
    }
}

我不知道如何保存这些行并在用户想要的时候再次打开它们。我已经打开并保存了文件对话框,但不确定如何让它们完成我希望他们做的工作。请帮忙。

谢谢

4

3 回答 3

2

如果要保存显示在图片框中的图像,以及运行时可能在其顶部绘制的任何线条,可以Control.DrawToBitmap使用.

我无法确定您是否还询问如何使用 aSaveFileDialog来确定用户想要保存文件的位置,或者您是否已经弄清楚了该部分,但这非常简单。

这是一个完整解决方案的示例。首先,保存对话框提示用户(标题为“保存图像”并默认过滤为位图图像 (*.bmp))。如果他们单击“确定”,则图片框中显示的图像将绘制到一个临时位图,并且该临时位图将保存到他们指定的位置。如果他们单击“取消”,则不会保存文件并且该方法会直接退出。

private void Save_Click(object sender, EventArgs e)
{
    //Show a save dialog to allow the user to specify where to save the image file
    using (SaveFileDialog dlgSave = new SaveFileDialog())
    {
        dlgSave.Title = "Save Image";
        dlgSave.Filter = "Bitmap Images (*.bmp)|*.bmp|All Files (*.*)|*.*";
        if (dlgSave.ShowDialog(this) == DialogResult.OK)
        {
            //If user clicked OK, then save the image into the specified file
            using (Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height))
            {
                picturebox1.DrawToBitmap(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height));
                bmp.Save(dlgSave.FileName);
            }
        }
    }
}
于 2010-11-17T11:36:46.647 回答
0

看一下

图片框.Image 属性

Image.Save 方法(字符串,ImageFormat)

Image.FromFile 方法(字符串,布尔值)

于 2010-11-17T11:09:28.893 回答
0

目前尚不清楚您想要什么...您要保存结果图像还是点列表?

如果要保存图像,只需使用pictureBox1.Image.Save(fileName).

如果要保存点列表,可以使用序列化(它应该与二进制或 XML 序列化一起使用)

于 2010-11-17T11:10:17.023 回答