簡體   English   中英

在C#中保存用戶繪制的位圖圖形

[英]Save User Drawn Bitmap Graphics in c#

我制作了一個程序,允許用戶在圖片框圖像上繪制線條,但現在需要保存這些線條以便以后打開。 這是我當前繪制線條的代碼:

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



        } 
    }
}

我不知道如何保存這些行,並且在用戶想要的稍后時間再次打開它們。 我已經開放並保存了文件,但不知道如何讓他們完成我希望他們做的工作。 請幫忙。

謝謝

如果要保存圖片框中顯示的圖像,以及在運行時可能已在其上繪制的任何行,可以使用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);
            }
        }
    }
}

目前尚不清楚你想要什么...你想保存結果圖像或點列表嗎?

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

如果要保存點列表,可以使用序列化(它應該與二進制或XML序列化一起使用)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM