Click here to Skip to main content
Click here to Skip to main content

Enhanced PictureBox Control with Area Selectability

By , 2 Mar 2004
Rate this:
Please Sign up or sign in to vote.

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.

    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:

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

About the Author

ming1000

United States United States
No Biography provided

Comments and Discussions

 
QuestionC# Enhanced PictureBox Control with Area Selectability PinmemberAltezs1-Aug-13 15:40 
GeneralGreat article PinmemberMike Stiles18-May-10 5:48 
QuestionThis will be a good question Pinmember Programm3r 13-Mar-08 23:50 
GeneralThanks! Pinmemberidfurhds14-Aug-07 4:14 
Generalgood article PinmemberMember #61054410-Feb-07 22:43 
Generali need your help Plz PinmemberAhmed El-Badry25-Jan-07 1:43 
QuestionDemo project corrupt? PinmemberNewbieDude21-Feb-06 22:05 
AnswerRe: Demo project corrupt? Pinmemberming100022-Feb-06 1:50 
GeneralCINT PinsussAnonymous1-Oct-05 13:33 
GeneralIntegers Pinmembereschneider10028-Apr-04 4:47 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140415.2 | Last Updated 3 Mar 2004
Article Copyright 2004 by ming1000
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid