-1

I have used (visual studio, windows form, c#) to create some drawings.

My goal is to add save button to save the drawing as it is and when I open the saved file in future I can continue my old work...

What happen now is every time I open visual studio I have to redraw every thing .

TaW
  • 53,122
  • 8
  • 69
  • 111
  • 1
    You can put all your drawing in array dan save this array in a file. Next time, you need only to load this file and redraw it again. –  Jul 09 '16 at 12:02
  • I am using Bitmap and rectangle to draw images and circles, I don't know how to save them as array :( – Mohanad Al Nuaimi Jul 09 '16 at 12:05
  • 1
    That was only an idea. You can use also object structure and serialize it to a file, for example using BinaryFormatter. –  Jul 09 '16 at 12:14
  • do you have an example to do that ? I am still consider myself a beginner in c# – Mohanad Al Nuaimi Jul 09 '16 at 12:19
  • You need to decide if you want to save the Bitam in a file or the data. The data need to be collected in a List of a helpful class. Make sure to make it serializable so you can easily save and load it. For a full example this is too broad. To see a discussion of the data collection see [here](http://stackoverflow.com/questions/28714411/update-a-drawing-without-deleting-the-previous-one/28716887?s=6|0.1765#28716887) – TaW Jul 09 '16 at 12:31

1 Answers1

1

The first task is collecting the data you draw in a List<T>. For code on how to collect them see (all) my comments here or Reza's answer here.

Here is an example to save & load simple PointF lists you can use to draw curves:

using System.IO;
using System.Xml.Serialization;

// all drawn curve points are collected here:
List<List<PointF>> curves = new List<List<PointF>>();



private void SaveButton_Click(object sender, EventArgs e)
{

    XmlSerializer xmls = new XmlSerializer(typeof(List<List<PointF>>));
    using (Stream writer = new FileStream(yourDrawingFileName, FileMode.Create))
    {
        xmls.Serialize(writer, curves);
        writer.Close();
    }
}

private void LoadButton_Click(object sender, EventArgs e)
{
    if (File.Exists(yourDrawingFileName))
    {

        XmlSerializer xmls = new XmlSerializer(typeof(List<List<PointF>>));
        using (Stream reader = new FileStream(yourDrawingFileName, FileMode.Open))
        {
            var curves_ = xmls.Deserialize(reader);
            reader.Close();
            curves = (List<List<PointF>>) curves_;
            Console.Write(curves.Count + " curves loaded.");
        }
    }
    yourPanelOrPictureBoxOrForm.Invalidate;
}

If you want to save a more complex class of drawing actions replace PointF by yourClass. Make sure the class is serializable! (Points are fine, ints and strings of course as well; Colors need a little help..)

For hints on how to design a more complex draw action class see here

Community
  • 1
  • 1
TaW
  • 53,122
  • 8
  • 69
  • 111