在c#中保存用户绘制的位图图形

发布于 2024-10-03 06:49:39 字数 2403 浏览 6 评论 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)
        {



        } 
    }
}

我不知道如何保存这些线条,并在用户需要时再次打开它们。我已经放入了打开和保存文件对话框,但不确定如何使它们完成我希望它们完成的工作。请帮忙。

谢谢

I have made a program that allows users to draw lines onto a picturebox image but now need to save these lines to be opened at a later date. This is my current code for drawing the lines:

 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)
        {



        } 
    }
}

I don't know how to save these lines and also open them again at a later time when the user wants to. I have put in open and save filedialogs but not sure how to make them do the job i want them to do. Please Help.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

╰◇生如夏花灿烂 2024-10-10 06:49:39

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

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

这是完整解决方案的示例。首先,系统会显示一个保存对话框(标题为“保存图像”并默认过滤为位图图像 (*.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);
            }
        }
    }
}

If you want to save the image that is displayed in the picture box, complete with any lines that may have been drawn on top of it during run-time, you can use the Control.DrawToBitmap method.

I can't tell for sure if you're also asking how to use a SaveFileDialog to determine where the user wants to save the file or if you've already got that part figured out, but it's very simple.

Here's an example of a complete solution. First, the user is prompted by a save dialog (entitled "Save Image" and filtering to bitmap images (*.bmp) by default). If they click OK, the image displayed in the picture box is drawn to a temporary bitmap, and that temporary bitmap is saved to the location they specified. If they click Cancel, the file is not saved and the method simply exits.

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);
            }
        }
    }
}
深海夜未眠 2024-10-10 06:49:39

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

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

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

It's not clear what you want... do you want to save the resulting image, or the list of points ?

If you want to save the image, just use pictureBox1.Image.Save(fileName).

If you want to save the list of points, you could use serialization (it should work with either binary or XML serialization)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文