|
Comments
Did you read today's front page stories & breaking news?
SYS-CON.TV
|
Product Reviews Customizing the PictureBox Control
Customizing the PictureBox Control
By: Erik Brown
Dec. 16, 2002 12:00 AM
The Windows Forms namespace in .NET includes a number of classes for building Windows-based applications. One such class is the PictureBox control, which displays an image within a control window. This article shows how to extend the PictureBox control in a custom PhotoBox control that can preserve the aspect ratio of a displayed image, and discusses how to use this control in the Windows Forms Designer window of Visual Studio .NET. Figure 1 shows a photograph in a standard PictureBox control with the SizeMode property of StretchImage. This setting stretches and distorts the image to exactly fit the bounds of the window. Figure 2 shows the same photograph using the PhotoBox control built in this article. As you can see, our new control will preserve the relationship of objects within the image, which is what we mean by preserving the aspect ratio. It is a bit surprising that Microsoft failed to address this issue in the original PictureBox control. Fortunately, .NET controls are designed to permit extensions such as the one we present here, so a workaround is possible. For the purposes of this article I will assume you have some understanding of C# and the use of class members such as properties and events. I also presume that you know how to create a Windows Forms application and define event handlers in Visual Studio .NET.
The PictureBox Control As an example, an application might define an Open menu that uses the OpenFileDialog class to accept an image from the user. Listing 1 shows a possible Click event handler for such a menu. This code creates a Bitmap object from a selected file and assigns it to the Image property of a PictureBox control. Of course, in production code, you would want to handle any exceptions that might occur while opening the file or loading the image. You can create an application much like the one shown in Figure 1 by adding a PictureBox control to a form with its Dock property set to Fill and its SizeMode property set to StretchImage. Create a menu bar with an Open menu, and use the code from Listing 1 as a Click event handler for this menu. Run the program and open a photographic image. As you resize the form, notice how the image is stretched and distorted to exactly fit the window. The remainder of this article shows you how to create a custom control similar to the one shown in Figure 2, which can scale an image within the window. We will call this the PhotoBox control.
Creating the PhotoBox Control
public class PhotoBox : System.Windows.Forms.PictureBox To define this class in Visual Studio .NET, create a new project called MyControlLibrary as a Windows Control Library. In this library Visual Studio will define a UserControl1 class, which you should delete. Then add a new User-Control class, called PhotoBox, to the library. Note that the new class automatically appears in the Toolbox window under the Windows Forms tab whenever a form is displayed. An existing library can also be added to the Toolbox window by right-clicking the window and selecting the Customize Toolbox... option. Visual Studio .NET will derive your class from the UserControl class. Simply modify the base class in the PhotoBox.cs file to use the PictureBox class instead, as shown in the prior code excerpt.
Adding a New SizeMode Setting Note how the four members defined by the PictureBoxSizeMode enumeration are repeated in Listing 2, and how their values are assigned to the corresponding PictureBoxSizeMode values. This will permit us to use these values with either the PictureBox base class or the PhotoBox class interchangeably. Our new SizeMode setting is defined by the ScaleImage enumeration member.
Implementing the PhotoBox class
The SizeMode Property
Also important here are the attributes defined in brackets prior to the property definition. These define how a visual designer such as Visual Studio .NET presents this property in the Properties window. A short list of some useful attributes defined by the System.ComponentModel namespace, including the ones used by our PhotoBox class, follows. You can see the complete list by displaying the classes derived from the Attribute class in the online documentation for the .NET Framework SDK.
In C#, the "Attribute" section of these classes may be omitted when defining an attribute, which is how we use them in our code.
The OnPaint() Method The OnPaint() method begins by assigning a value to the base.SizeMode property and invoking the base.OnPaint() method. The base keyword accesses the base class, in this case the PictureBox class. These lines preserve the existing PictureBox paint behavior by drawing the image for all SizeMode property values other than the ScaleImage setting, and invoking any Paint event handlers registered with the class. In the case where the SizeMode property is set to the ScaleImage setting, the subsequent lines implement this setting by first clearing the drawing area, and then scaling the image to fit within the control. This uses a private ScaleToFit() method to calculate the rectangle within the control where the image should be drawn, and then the Graphics.DrawImage() method to draw the actual image on the screen. We will not discuss these methods in detail here. The ScaleToFit() method is a standard image calculation that you can examine at your leisure; the DrawImage() method is provided by the Graphics class for drawing image objects on a GDI+ drawing surface. Note that the logic for our ScaleImage setting has a slight performance problem. We set the base.SizeMode value to Normal to ensure that the PictureBox.OnPaint() method doesn't throw an exception. This has the side effect of drawing the image in Normal mode, after which our code redraws the image in ScaleImage mode. A more robust implementation might bypass the PictureBox control entirely and inherit from the Control class directly. We didn't do this here since we would then have to implement the BorderStyle property and all of the SizeMode settings, making for a rather long article. The point here is to show how custom controls can be created and integrated into the Properties window of Visual Studio .NET, which our PhotoBox implementation does quite well.
The OnResize() method
Using the PhotoBox Control Also examine the properties of the PhotoBox control in the Properties window of Visual Studio .NET, as illustrated in Figure 3. Note how our SizeMode property appears in the Behavior category, shows the ScaleImage setting as the default value (since it isn't in bold), and displays the assigned text in the description pane at the bottom of the window. When you click on the down arrow for this property, the members of the PhotoBoxSizeMode enumeration are displayed to the user.
Topics for Further Reading Reader Feedback: Page 1 of 1
Latest Cloud Developer Stories
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
|
SYS-CON Featured Whitepapers
Most Read This Week
Breaking Cloud Computing News
|
|||||||||||||||||||||||||||||||||||||||||||||||||