Click here to Skip to main content
15,879,326 members
Articles / Programming Languages / Visual Basic
Article

Enhanced PictureBox Control with Area Selectability

Rate me:
Please Sign up or sign in to vote.
3.74/5 (13 votes)
2 Mar 20043 min read 88.9K   3.1K   41   10
Enhanced PictureBox control with area selectability.

Sample Image

Introduction

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.

Background

Following links contain background information in this article:

  1. Windows Forms Controls Created with Visual Basic .NET or Visual C# .NET
  2. With DrawImage by Rod Stephens
  3. 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.

  1. 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 and SelectArea. 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 EnPicbox control.

    VB
    EnPixbox1.SourceImage = "1.bmp" 'input image from a file
    Picturebox1.Image = EnPixbox1.SelectArea 
    'Output Selected Area to a Picturebox control

    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 SourceImage.

  2. End User Interface of EnPicbox

    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:

VB
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: CInt and Int. Int function consistently round decimal data type to ground, that is, Int(2.5)=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 d2i in 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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionC# Enhanced PictureBox Control with Area Selectability Pin
Altezs1-Aug-13 15:40
Altezs1-Aug-13 15:40 
GeneralGreat article Pin
Mike Stiles18-May-10 5:48
Mike Stiles18-May-10 5:48 
QuestionThis will be a good question Pin
Programm3r13-Mar-08 23:50
Programm3r13-Mar-08 23:50 
GeneralThanks! Pin
idfurhds14-Aug-07 4:14
idfurhds14-Aug-07 4:14 
Generalgood article Pin
Member 61054410-Feb-07 22:43
Member 61054410-Feb-07 22:43 
Generali need your help Plz Pin
Ahmed El-Badry25-Jan-07 1:43
Ahmed El-Badry25-Jan-07 1:43 
QuestionDemo project corrupt? Pin
NewbieDude21-Feb-06 22:05
NewbieDude21-Feb-06 22:05 
AnswerRe: Demo project corrupt? Pin
ming100022-Feb-06 1:50
ming100022-Feb-06 1:50 
GeneralCINT Pin
Anonymous1-Oct-05 13:33
Anonymous1-Oct-05 13:33 
GeneralIntegers Pin
Eric P Schneider28-Apr-04 4:47
Eric P Schneider28-Apr-04 4:47 
If you use Option Explicit the IDE will force you to specify conversion method.


Schneider

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.