c# - Bitmap from Drawing not render outside of Load_Form -


i'm trying render bitmap created drawing screen render after minimize , maximize again.

i follow these steps: using bitmaps persistent graphics in c#

but can render bitmap in screen outside of load_form.

if put code:

using system.drawing; ...  graphics graphicsobj; mybitmap = new bitmap(this.clientrectangle.width,          this.clientrectangle.height,          imaging.pixelformat.format24bpprgb); graphicsobj = graphics.fromimage(mybitmap);  pen mypen = new pen(color.plum, 3); rectangle rectangleobj = new rectangle(10, 10, 200, 200); graphicsobj.drawellipse(mypen, rectangleobj); graphicsobj.dispose(); 

in other place, example button, need minimize , maximize see image.

edit:

bmp bitmap global variable create instance in form event load_form1

bmp = new bitmap(this.clientrectangle.width,                  this.clientrectangle.height,                  system.drawing.imaging.pixelformat.format24bpprgb); 

paint event of form redraw:

private void form1_paint(object sender, painteventargs e) {     graphics graphicsobj = e.graphics;     graphicsobj.drawimage(mybitmap, 0, 0, mybitmap.width, mybitmap.height);     graphicsobj.dispose(); } 

but need draw inmediately after create drawing.

not telling bigger picture makes hard recommend best course of action let me assume 2 possible aims:

  • either want drawn onto form

  • or want display bitmap sucessively draw more , more things.

for first need code paint event this:

private void form1_paint(object sender, painteventargs e) {     rectangle rectangleobj = new rectangle(10, 10, 200, 200);     using (pen mypen = new pen(color.plum, 3))       e.graphics.drawellipse(mypen, rectangleobj); } 

if data control drawing dynamic should store them @ class level variables or lists of them , change them needed can use them in paint event.

for latter aim there various times when add bitmap.

so start creating a, probably, class level bitmap:

    public form1()     {         initializecomponent();         bmp = new bitmap(this.clientrectangle.width,                          this.clientrectangle.height);              }      bitmap bmp = null; 

and have 1 or more places draw this:

void drawalittle() {     rectangle rectangleobj = new rectangle(10, 10, 200, 200);     using (pen mypen = new pen(color.plum, 3))     using (graphics g = graphics.fromimage(bmp))     {         g.drawellipse(mypen, rectangleobj);         //..     }     this.invalidate(); } 

note how invalidate form after changing bitmap, paint event triggered.

also note if these update happen idea keep graphics object alive between calls; either making class variable bitmap or keeping locally in method updates , passing out parameter drawing method..

in form's paint event need is

private void form1_paint(object sender, painteventargs e) {       e.graphics.drawimage(bmp, 0, 0); }        

also note 32bppargb recommended default format. is used display in case, efficient use begin with..


Comments

Popular posts from this blog

jquery - How do you format the date used in the popover widget title of FullCalendar? -

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -