c# - How to save shapes which I draw on a Panel as binary -


i have mini paint program . want create save button saves panel details(shapes , been drawing) binary file. did this:

 savefiledialog sfd = new savefiledialog();  binaryformatter bf = new binaryformatter();  var stream = new binaryreader(file.open(sfd.filename,filemode.create));  bf.serialize(stream,object); 

but has error using object invalid in bf.serialize. how should this?

you don't need serialize panel, panel not serializable. can consider either of these options:

  1. you can draw on bitmap , save bitmap, or draw control image , save image. way flatten shapes single image , after loading image not editable shapes anymore. paint.
  2. you can make shapes serializable , serialize list of serializable shapes in file. can deserialize them again. way can load shapes , let user edit them, example visio.

i shared 2 examples here:

  • save image example: saves drawings painted on panel bitmap image file.

  • serialization example: in example i've created serializable shape classes containing properties coordinates , color, created save , load method allows serialize shapes in file , deserialize them file , show them again. can extend example add functionality hit-testing , moving shapes. can use xml serializer instead of binary serializer.

save image example

to keep things simple in example save panel paintings in file.

if put painting logic in paint event of panel, can use drawtobitmap save image of control file:

private void button1_click(object sender, eventargs e) {     using (var bm = new bitmap(panel1.width, panel1.height))     {         panel1.drawtobitmap(bm, new rectangle(0, 0, bm.width, bm.height));         bm.save(@"d:\panel.bmp", system.drawing.imaging.imageformat.bmp);      } } private void panel1_paint(object sender, painteventargs e) {     e.graphics.fillrectangle(brushes.red, 0, 0, 100, 100);     e.graphics.fillrectangle(brushes.blue, 50, 50, 100, 100); } 

serialization example

you can create serializable shapes lineshape , rectangleshape deriving serializable shape class. store properties of shapes in class, these classes contain drawing logic:

[serializable] public abstract class shape {     public abstract void draw(graphics g);     public override string tostring() { return gettype().name; } }  [serializable] public class lineshape : shape {     public lineshape(){ color = color.blue; width = 2; }     public point point1 { get; set; }     public point point2 { get; set; }     public int width { get; set; }     public color color { get; set; }     public override void draw(graphics g)     {         using (var pen = new pen(color, width))             g.drawline(pen, point1, point2);     } }  [serializable] public class rectangleshape : shape {     public rectangleshape() { color = color.red; }     public rectangle rectangle { get; set; }     public color color { get; set; }     public override void draw(graphics g)     {         using (var brush = new solidbrush(color))             g.fillrectangle(brush, rectangle);     } } 

you can create shapeslist class hold shapes , contain logic saving , loading shapes. logic painting shapes on surface:

[serializable] public class shapeslist : list<shape> {     public void save(string file)     {         using (stream stream = file.open(file, filemode.create))         {             binaryformatter bin = new binaryformatter();             bin.serialize(stream, this);         }     }     public void load(string file)     {         using (stream stream = file.open(file, filemode.open))         {             binaryformatter bin = new binaryformatter();             var shapes = (shapeslist)bin.deserialize(stream);             this.clear();             this.addrange(shapes);         }     }     public void draw(graphics g)     {         this.foreach(x => x.draw(g));     } } 

then can use these shapes , shapes list way:

shapeslist shapes; private void form3_load(object sender, eventargs e) {     shapes = new shapeslist();     shapes.add(new rectangleshape() { rectangle = new rectangle(0, 0, 100, 100),          color = color.green });     shapes.add(new rectangleshape() { rectangle = new rectangle(50, 50, 100, 100),          color = color.blue });     shapes.add(new lineshape() { point1 = new point(0, 0), point2 = new point(150, 150),          color = color.red });     this.panel1.invalidate(); }  private void button1_click(object sender, eventargs e) {     shapes.save(@"d:\shapes.bin");     shapes.clear();     this.panel1.invalidate();     messagebox.show("shapes saved successfully.");     shapes.load(@"d:\shapes.bin");     this.panel1.invalidate();     messagebox.show("shapes loaded successfully."); } private void panel1_paint(object sender, painteventargs e) {     e.graphics.smoothingmode = system.drawing.drawing2d.smoothingmode.antialias;     shapes.draw(e.graphics); } 

Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -