Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Graphics on a panel from a bitmap - how to set origin ?

Status
Not open for further replies.

StevenK

Programmer
Jan 5, 2001
1,294
GB
Following the heartache of redrawing graphics onto a panel (a series of rectangles in the simple case) I've adopted the idea of making use of a bitmap to draw onto and then load the panel with this graphic each time it is drawn.
This works as follows (following suggestions from previous posts) :


Bitmap offScreenBmp;
Graphics offScreenDC;


offScreenBmp = new Bitmap(panel1.Width, panel1.Height);
offScreenDC = Graphics.FromImage(offScreenBmp);

// Perform various DrawRectangle / FillRectangle against 'offScreenDC'

// .. and when done drawing rectangles, etc.
Graphics clientDC = panel1.CreateGraphics();
clientDC.DrawImage(offScreenBmp, panel1.Left, panel1.Top);


Also in the event code for panel1_Paint I have (as above) :
Graphics clientDC = panel1.CreateGraphics();
clientDC.DrawImage(offScreenBmp, panel1.Left, panel1.Top);

... which resets the graphic as required.

This seems to work well and saves the workload of redrawing the graphic each time focus is lost / covered / etc. when dealing with panel1.

However this causes me a problem when I have panel1 set to AutoScroll and the user is not in the top left hand corner (origin) of the panel.
The graphic is drawn 'as is' such that the graphic's top left hand corner is in the current top left 'displayed' position within the panel - albeit that the actual top left origin of the panel may have scrolled up / left.

How can I get the graphic to start with an origin as required rather than the visual origin ?

Any suggestions would be appreciated here.
Thanks in advance

Steve
 
Try this:
clientDC.DrawImage(offScreenBmp, 0, 0);
instead of:
clientDC.DrawImage(offScreenBmp, panel1.Left, panel1.Top);

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top