главная продукты downloads форум тех. поддержка

 

Главная страница

GDI+ FAQ: Calculating Best-Fit Drawing Using A Transform

 

This article is not truly a GDI+ how-to but more of a general guideline on how and how not to use the PictureBox control.

 

Many people use the PictureBox control in completely the wrong way and assume that it has capabilities that it just doesn't posses. In my opinion this is largely due to poor documentation for many of the Windows Forms controls and not enough in-depth information on how to do common tasks with Windows Forms.

The salient points here are that:

  • PictureBox is a convenient control for displaying images.
  • PictureBox provides no editing facilities whatsoever.

One of the things you should never do with the PictureBox is grab its Graphics using pictureBox1.CreateGraphics() so that you can draw on it's surface. I think the Control.CreateGraphics call really ought to have been made a protected member because it's very bad form to draw asynchronously on the graphics of another control if only to preserve your own sanity as the control itself may refresh its surface at any time, destroying your work in the process. If you must paint on a control, for example if you want to make an overlay that doesn't change the actual image, add an event handler to it's Paint event and paint using the Graphics provided in the PaintEventArgs. This will ensure that you have an opportunity to refresh your portion of the graphics whenever the control does it's portion.

 

A popular misconception is that drawing on the PictureBox either in the Paint event or through the stolen Graphics object will in fact draw on the underlying image. This is not the case. (This subject crops up about once every two days on the Microsoft newsgroups). 

 

Drawing on the PictureBox only affects the pixels on the screen, not the pixels in the image. You can however, obtain the Graphics for the image using...

 

Graphics g=Graphics.FromImage(myPictureBox.Image); //or...

Dim g as Graphics = Graphics.FromImage(myPictureBox.Image)

 

Anything you draw on the picture using the Graphics returned from such a call will be permanent and can even be saved to disk. Remember to Invalidate() the PictureBox after you're finished so that it refreshes the screen after you've made your changes.

 

Back to the GDI+ FAQ

 

e-mail:

 

bousoft@mail.ru