Selecting and extracting specific area from an image are basic functionalities of image processing applications, such as photo editing. These functions act as starting point for more advanced image processing functions, say zooming or rotating images. In this article, an
EnPicbox control is presented that enables user selecting a rectangular area from an image.
EnPicbox control is based on the standard
PictureBox control and wraps necessary message handling mechanism for interacting with user selection.
Following links contain background information in this article:
- Windows Forms Controls Created with Visual Basic .NET or Visual C# .NET
- With DrawImage by Rod Stephens
- Basic Scripting Edition CInt Function
Using the code
First of all, two kinds of users are defined in the following discussion: end user and programmer. End user is the user who uses final applications containing
EnPicbox control; while programmer uses
EnPicbox control for developing an application.
The source code of
EnPicbox control is self-explanatory.
- Programming Interface
EnPicbox programming interface is pretty easy. There are only two attributes of
EnPicbox control that should be taken care of by programmers:
SourceImage attribute provides an interface to set or get currently displayed image in
EnPicbox control. In fact, it is exactly the same as the image attribute in standard
PictureBox control so that it can take any image format supported by
Bitmap class in VB.NET.
SelectArea is a
Readonly attribute that outputs a
Bitmap object according to user selection. The following code segment shows basic operations of
EnPixbox1.SourceImage = "1.bmp" Picturebox1.Image = EnPixbox1.SelectArea
EnPicbox always tries to display the whole image in its displaying area, so that the source image is resized according to the size of the display area in
EnPicBox. Thus, the appearance of source image might be distorted. More important is that the user selection does not work on the displaying area of the control but on the
- End User Interface of
End users start a selection by pressing left mouse button, adjust the selection by holding it and moving mouse, and finish one selection by releasing left mouse button. End users can confirm selection by double clicking left mouse button. Programmers please note, the current selection can be retrieved from the
SelectArea attribute only after confirmation has been done. Pressing right mouse button activates a context menu, with which end user can choose the marker width and color and they can clear marks or make confirmations with this menu.
Undependable VB Integer data type computation and conversion
It might be noticed that height and width properties are set to be
double data type in
EnPicBox control. The reason is the inconsistence of VB integer data computation and conversion. Readers can try the following code:
Dim i, j as Integer
i = 5/2
j = 3/2
The results are i = 2, and j = 2. This is the outcome of funny VB
CInt function. There are two integer data conversion functions in VB:
Int function consistently round decimal data type to ground, that is,
Int(2.9)=2. However, MS chooses
CInt for converting integer computation result to integer. The feature, or bug (it's no doubt that MS treats it as a feature and formalizes it in its documentation), of
CInt function is that it rounds to the nearest even number when there is a 0.5 in the decimal part of any decimal data type. Unfortunately, array index computation is inevitable in image processing applications; and index must be an integer data type. Therefore, I strongly recommend that programmers use decimal data type for any computation in VB (I have tested these on VB6, VB.NET 2002 and VB.NET 2003). The private function
EnPicBox solves this problem, it rounds any decimal part greater than or equal to .5 to ceiling and rounds decimal part less than .5 to ground.
Comments are welcome
I am new with .NET infrastructure. Any of your comments or suggestions is more than welcomed.