Hello, this is example from Core C# and .Net book.
Draw one flag on button1 click, on button2 click draw the same flag but reverse colors. Everything works, but there is problem. Panel has to be redrawn, because now when I minimize the form image
that I drew disappears.
Java
// reverse colors privatevoidbutton3_Click(object sender, EventArgs e)
{
Graphicsg= pan.CreateGraphics();
stringfname="c:\\flag.gif";
Bitmapbmp=newBitmap(fname);
BitmapbmpMem=newBitmap(bmp.Width,bmp.Height);
GraphicsgMem= Graphics.FromImage(bmpMem);
gMem.DrawImage(bmp,0,0); // Draw flag to memoryColorcnRed= Color.FromArgb(255,214,41,33);
for (inty=0; y 240) bmpMem.SetPixel(x,y, cnRed);
else
bmpMem.SetPixel(x,y,Color.White);
}
}
g.DrawImage(bmpMem,0,0); // Display reversed flag on panel
gMem.Dispose();
g.Dispose();
}
// draw image voidbutton1_Click(object sender, EventArgs e)
{
Graphicsg= pan.CreateGraphics();
stringfname="c:\\flag.gif";
Bitmapbmp=newBitmap(fname);
g.DrawImage(bmp,0,0);
g.Dispose();
}
If someone knows how to keep this image on panel please answer.
4 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Jorge L FernandezRank #7,688 · 236 reputationPosted Nov 5, 2009, 11:48 AM EST
✓ Accepted answer0
The problem you have is that you are not repainting the panels. Please, place the code you have in the Paint event and you will get it solved. Probably you want to place some logic in the event in order to get the appropriate painting in the appropriate moment. A way is create a separated method to be invoked when clicking the button and inside the Paint event.
Hope this help. I remember myself having this problem in my first Windows App. Keep coding and we keep learning. Thanks.
If this answer solve your problem please mark your question as answered by this reply.
Robert9 reputationPosted Nov 5, 2009, 12:10 PM EST
0
Thx. I created 2 boolean variables for each button click event, in this events I call invalidate() method, I moved draw code to panel paint event, check which bool is true and do proper paint code.
0
Jorge L FernandezRank #7,688 · 236 reputationPosted Nov 5, 2009, 12:17 PM EST
Jorge L FernandezRank #7,688 · 236 reputationPosted Nov 5, 2009, 11:48 AM EST
Hope this help. I remember myself having this problem in my first Windows App. Keep coding and we keep learning. Thanks.
If this answer solve your problem please mark your question as answered by this reply.
Robert9 reputationPosted Nov 5, 2009, 12:10 PM EST
Jorge L FernandezRank #7,688 · 236 reputationPosted Nov 5, 2009, 12:17 PM EST
gu kai2 reputationPosted Dec 21, 2009, 8:43 PM EST