0

I am working on a project where I let the users to draw some shapes and save the whole drawing panel(c# window form) ,so that they can open the panel later to edit their drawing.

I am thinking of saving all other things(such as name and type of the drawing,) in a json file But do not know how to save the drawn shapes and more importanly how to display them again.

[enter image description here]

the white panel is for drawing.( part i want to save)

New Village- to clear all the drawing

Save village- to save all the shapes drawn on the white part

Open village-to open a previously stored state of drawn shapes.

Drawing method

g= drawing_panel.CreateGraphics();
        village_name.Text = "Okay";
        pr.setName(village_name,drawing_panel);
        
        Pen p = new Pen(Color.Black);
        g.DrawEllipse(p, 10, 10, 100, 100);
        g.DrawLine(p, 150, 150, 300, 300);

Serializing method

   BlogSites bsObj = new BlogSites()
        {
            Name = "C-sharpcorner",
            Description = "Share Knowledge"
  
        };

        // Convert BlogSites object to JOSN string format  
        string jsonData = JsonConvert.SerializeObject(bsObj);
Blockchain Kid
  • 315
  • 2
  • 9
  • 1
    If you use class objects to describe the shapes a User can draw, you have no problems in serializing these objects. You should show how you generate, draw and store your shapes. – Jimi Sep 12 '20 at 11:49
  • Do some research about binary serialization and the Command pattern. It will be useful. As @Jimi said, without the code for generate & draw, we can't help you much further. – aamartin2k Sep 12 '20 at 12:55
  • Stop here: `g= drawing_panel.CreateGraphics();`. This is already wrong in a catastrophic way (c'mon, that's exaggerated! -- No it's not). Learn how to draw using the `Paint` event handler's (or `OnPaint()` method override) `PaintEventargs` object. It provides the `e.Graphics` object used to draw on a device context. Before this part is not setup correctly, don't go any further. ++ You need to handle objects (classes, *models*, whatever you call these intermediate structures) that describe your shapes. Pass `e.Graphics` to a public method that draws a specific shape in that Graphics context. – Jimi Sep 12 '20 at 13:22
  • + Are you sure about .Net Core here? You also appear to be trying to use Json.Net instead of System.Text.Json. If you have Visual Studio 2019 and you built a WinForms Project using .Net Core, switch to .Net Framework for now. – Jimi Sep 12 '20 at 13:48
  • @Jimi `System.Text.Json` is fine but in the question code isn't related to it. Looks like there's `Newtonsoft.Json` in use. .NET Core 3.1 is also has more power than almost outdated .NET Framework 4.x. Bad advice with no reason. – aepot Sep 12 '20 at 15:02
  • @aepot Bad advice? This is not related to .Net Core 3.1, it's related to an hypothetical WinForms Project built with .Net Core. As you probably know, the .Net Core version of WinForms is under construction, incomplete, partially unstable, some parts go missing or behave *erratically* between versions (quite expected and not really a hidden notion). -- If you use .Net Core 3.1+, System.Text.Json - though not a *must* - is built in and quite performant, already matching or outperforming Json.Net. -- What part of *[If WinForms] using .Net Core, switch to .Net Framework for now* is bad advice? – Jimi Sep 12 '20 at 15:30
  • @Jimi not a point to discuss. It's not a real thing but opinion. It's better to provide it as opinion with explicit remark. That's it. Maybe you're right, maybe not. Winforms is bad in .NET Core? Ok, what exactly is bad? Nevermind. Anyway you have no exact reason to suggest avoiding .NET Core here. If I'm wrong, please provide one. Looks like _never use `async`, it's not stable in .NET Core, use sync instead_ - same thing. And there'a really bad TAP methods in .NET Core but I have a [link](https://stackoverflow.com/q/63217657/12888024). Have you similar one related to the question? – aepot Sep 12 '20 at 15:51
  • @aepot A link (one?) about WinForms Projects built using .Net Core? One *reason*? The web is littered with this information. WinForms for .Net Core is not ready, nobody has ever stated that it is, it's in Preview. This is really an universal notion. No *opinions* here. -- .Net Framework is supported and will be supported until Windows does. *FOR NOW*, to build a WinForms Project, .Net Framework is preferred. (Or, you could build two projects side-by-side, to help in the development). When One .Net is out, we'll see. – Jimi Sep 12 '20 at 15:58
  • @Jimi and no single link above. Thus, it's just an another opinion. I don't and don't suggest OP to trust you. – aepot Sep 12 '20 at 16:04
  • @aepot *The web is littered with this information* + *It's in Preview*. Not really difficult to google about it. I'm stopping here, there's nothing else to say. – Jimi Sep 12 '20 at 16:06

0 Answers0