c# - Saving bitmap in a paint program -


i working on ms paint program programmed entirely in c#. it's basic, have stumbled upon problem. saw post regarding ms paint mock ups. how save end result .bmp file. tried using solutions , answers provided , worked.

the file saved. when saved, saved blank panel ( im making forms app) . have seen 1 post deals issue couldn't incorporate allow user interact. following code:

using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.threading.tasks; using system.windows.forms;      namespace paint     {     public partial class form1 : form     {     graphics g;     pen p = new pen(color.black, 1);     point sp = new point(0, 0);     point ep = new point(0, 0);     int k = 0;      public form1()     {         initializecomponent();     }      private void picturebox2_click(object sender, eventargs e)     {         p.color = red.backcolor;         default1.backcolor = red.backcolor;       }      private void blue_click(object sender, eventargs e)     {         p.color = blue.backcolor;         default1.backcolor = blue.backcolor;     }      private void green_click(object sender, eventargs e)     {         p.color = green.backcolor;         default1.backcolor = green.backcolor;     }        private void panel2_mousedown(object sender, mouseeventargs e)     {         sp = e.location;         if (e.button == mousebuttons.left)             k = 1;     }      private void panel2_mouseup(object sender, mouseeventargs e)     {         k = 0;     }      private void panel2_mousemove(object sender, mouseeventargs e)     {         if (k == 1)         {             ep = e.location;             g = panel2.creategraphics();             g.drawline(p, sp, ep);          }         sp = ep;     }        private void panel2_mouseleave(object sender, eventargs e)     {         k = 0;        }      private void panel2_paint(object sender, painteventargs e)     {      }      private void button1_click(object sender, eventargs e)     {         savefiledialog dialog = new savefiledialog();         if (dialog.showdialog() == dialogresult.ok)         {             int width = convert.toint32(panel2.width);             int height = convert.toint32(panel2.height);             bitmap bmp = new bitmap(width, height);             panel2.drawtobitmap(bmp, new rectangle(0, 0, width, height));             bmp.save(dialog.filename, system.drawing.imaging.imageformat.bmp);         }     } } } 

so question is... how succesfully save .bmp image in c# forms app , in how not make save blank. in advance :)

edit

so have tried first answer , im trying ideas suggested individual in comments , how, instead of saving blank canvas. application literally saves black image. here code ended with. did go wrong?

using system;  using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.threading.tasks;  using system.windows.forms;  namespace paint { public partial class form1 : form {     graphics g;     graphics h;     pen p = new pen(color.black, 1);     point sp = new point(0, 0);     point ep = new point(0, 0);     int k = 0;     bitmap bmp =null;      public form1()     {         initializecomponent();     }      private void picturebox2_click(object sender, eventargs e)     {         p.color = red.backcolor;         default1.backcolor = red.backcolor;       }      private void blue_click(object sender, eventargs e)     {         p.color = blue.backcolor;         default1.backcolor = blue.backcolor;     }      private void green_click(object sender, eventargs e)     {         p.color = green.backcolor;         default1.backcolor = green.backcolor;     }        private void panel2_mousedown(object sender, mouseeventargs e)     {         sp = e.location;         if (e.button == mousebuttons.left)             k = 1;     }      private void panel2_mouseup(object sender, mouseeventargs e)     {         k = 0;     }      private void panel2_mousemove(object sender, mouseeventargs e)     {         if (k == 1)         {             ep = e.location;             int width = convert.toint32(panel2.width);             int height = convert.toint32(panel2.height);             bitmap bmp = new bitmap(width, height);              g = graphics.fromimage(bmp);             g.drawline(p, sp, ep);             h = panel2.creategraphics();             h.drawline(p, sp, ep);         }         sp = ep;     }        private void panel2_mouseleave(object sender, eventargs e)     {         k = 0;        }      private void panel2_paint(object sender, painteventargs e)     {      }      private void button1_click(object sender, eventargs e)     {         savefiledialog dialog = new savefiledialog();         if (dialog.showdialog() == dialogresult.ok)         {             /*             bitmap bmp = bitmap.fromhbitmap(panel2.creategraphics().gethdc());             panel2.drawtobitmap(bmp, new rectangle(0, 0, width, height));*/             int width = panel2.width;             int height = convert.toint32(panel2.height);             if (bmp == null)                  bmp = new bitmap(width, height);             bmp.save(dialog.filename, system.drawing.imaging.imageformat.jpeg);         }     } } 

}

here revised version, pretty told in comments..:

public partial class form2 : form {     graphics g;     graphics h;     pen p = new pen(color.black, 1);     point sp = new point(0, 0);     point ep = new point(0, 0);     int k = 0;     bitmap bmp =null;      public form2()     {         initializecomponent();         bmp = new bitmap(panel2.clientsize.width, panel2.clientsize.height);         g = graphics.fromimage(bmp);         g.clear(panel2.backcolor);     }      private void picturebox2_click(object sender, eventargs e)     {         p.color = red.backcolor;         default1.backcolor = red.backcolor;      }      private void color_click(object sender, eventargs e)     {         control ctl = sender control;         p.color = ctl.backcolor;         default1.backcolor = ctl.backcolor;     }      private void panel2_mousemove(object sender, mouseeventargs e)     {         if (e.button == mousebuttons.left)         {             ep = e.location;             g.drawline(p, sp, ep);             h = panel2.creategraphics();             h.drawline(p, sp, ep);         }         sp = ep;     }      private void panel2_mousedown(object sender, mouseeventargs e)     {         sp = e.location;     }       private void button1_click(object sender, eventargs e)     {         savefiledialog dialog = new savefiledialog();         if (dialog.showdialog() == dialogresult.ok)         {             bmp.save(dialog.filename, system.drawing.imaging.imageformat.jpeg);         }     } } 

a few notes:

  • i mapped clicks of palette control one.
  • i have eliminated flag k doing button test in move.
  • i have kept cached graphics g; not recommended, keep drawing 1 , same bitmap is ok.
  • i have remove duplicate declaration of bitmap bmp.
  • i don't know picturebox does, didn't touch code.

drawbacks of soution:

  • since don''t keep track of moves can't undo.
  • since lines drawn separately can't results broader and/or semi-transparent pens because overlapping endpoints bad.

for better solution of simple doodeling see here , after have studied can tackle better solution, allow use sort of drawing tools here..


Comments

Popular posts from this blog

What is happening when Matlab is starting a "parallel pool"? -

angular - DownloadURL return null in below code -

php - Cannot override Laravel Spark authentication with own implementation -