A Simple VB - NET Application For Image Editing Using csXImage ActiveX Control
A Simple VB - NET Application For Image Editing Using csXImage ActiveX Control
The following pages show some of the functions that are included in the demo project, with brief
explanation about how these could be used in your own projects.
First, let us look at the demo application in action. The VB project is based on a single form,
fMain. After opening and running the project, the form is displayed with its menu bar. After
selecting File/Open, an image file of one of the formats supported by csXImage can be selected
and opened. The image is loaded into the control on the form and displayed.
The status bar at the bottom of the form displays information about the image: the filename, size
and colour depth. A combo box on the left of the form is now active, with the message 'Select
effect to apply...'. The drop down list displays a selection of the image
manipulation/enhancement functions available in csXImage. We can, for example, select
'Brightness':
A value can now be entered, and the brightness of the image adjusted by clicking 'Apply'. The
colours Red, Green and Blue can be individually selected to tint the image with a particular
colour, or by leaving all three selected, the overall brightness is adjusted.
Original image
Brightened image
A second example shows how an image can be tinted with a specific colour by using the
brightness method acting on only one or two of the colour channels. Here, a greyscale image is
brightened using only red and blue, with green unchanged.
Original image
Tinted image
This is equivalent to the following lines of VB code. Note the need to change the colour format
before using the brightness method. This is because the original image is in a greyscale format
which does not allow colours other than greyscale ones to be introduced into the image.
AxImageBox1.ColorFormat = csXImageTrial.TxColorFormat.cf24bit
AxImageBox1.Brightness(70, TRUE, FALSE, TRUE)
Sharpen
Images which are slightly blurred can be sharpened using the 'Sharpen' or 'SharpenBy' methods.
These give the same effect, the only difference being that 'SharpenBy' gives an option to the user
to define the amount of sharpening to be done. 'Sharpen' uses a default for this parameter.
In the VB demo, the Sharpen option uses the default settings. Using the same image as above,
the effect of the 'Sharpen' method is shown below.
Original image
Sharpened image
AxImageBox1.Sharpen()
Several options are available to the user to modify the way that the merge is carried out. These
options are best demonstrated by showing the different effects achieved when a simple
monochrome image, containing black text on a white background, is merged into a full colour
photograph.
The two images being used for this demonstration are shown below. All the following examples
were prepared using the VB.NET demo application, with the photograph on the left as the main
image (loaded into the csXImage control before using the MergeFile method) and the text image
on the right as the second image.
Main image
First let's look at the effect of allowing transparency in the second image. If a straightforward
merge is carried out, with no transparency, the second image will be stamped onto the first
image, hiding all the pixels of the first image covered by the rectangle of the second image. This
is shown on the left below. If transparency is allowed in the second image, and the transparent
colour is specified as white, then only the black text will be merged, with the remaining pixels of
the original image retained. This is shown on the right. The equivalent VB code to give these
effects is shown beside each image.
With AxImageBox1
.MergeStyle =
csXImageTrial.TxWMStyle.wmCentre
.MergeTransparent = FALSE
.MergeFile(MergeFileName)
End With
With AxImageBox1
.MergeStyle =
csXImageTrial.TxWMStyle.wmCentre
.MergeTransparent = TRUE
.MergeTransparentColor =
System.Drawing.Color.White
.MergeTransparency = 0
.MergeFile(MergeFileName)
End With
In the above examples, the first image has been simply stamped onto the main image.
Alternatively, it can be made more transparent by setting a value for the MergeTransparency
property (0 to 100%). This can give a "watermark" effect. In the images below, a value of 80%
has been used so the second image appears lightly blended into the main image.
Four examples are given, showing the four different values for the MergeStyle property. This
property determines how the second image is positioned on the main image.
With AxImageBox1
.MergeStyle =
csXImageTrial.TxWMStyle.wmSingle
.MergeTransparent = TRUE
.MergeTransparentColor =
System.Drawing.Color.White
.MergeTransparency = 80
.MergeFile(MergeFileName)
End With
With AxImageBox1
.MergeStyle =
csXImageTrial.TxWMStyle.wmTile
.MergeTransparent = TRUE
.MergeTransparentColor =
System.Drawing.Color.White
.MergeTransparency = 80
.MergeFile(MergeFileName)
End With
With AxImageBox1
.MergeStyle =
csXImageTrial.TxWMStyle.wmCentre
.MergeTransparent = TRUE
.MergeTransparentColor =
System.Drawing.Color.White
.MergeTransparency = 80
.MergeFile(MergeFileName)
End With
With AxImageBox1
.MergeStyle =
csXImageTrial.TxWMStyle.wmWrap
.MergeTransparent = TRUE
.MergeTransparentColor =
System.Drawing.Color.White
.MergeTransparency = 80
.MergeFile(MergeFileName)
End With
In some cases, the image already loaded into the csXImage control is the one that needs to be
used as the second image, with the main image being available on disk. Another property,
MergeReverse, can be used to specify that the images will be merged in the opposite order.
The image resulting from a merge operation always retains the colour format and dimensions of
the main image.
When the pixel information option is activated in the VB demo (using 'Tools/Pixel Info' from the
menu), a cross-hair cursor is displayed when the mouse pointer is over the image. Pixel
information is then read and displayed, as in the screenshot below.
In using this event to provide pixel data we have two problems to consider: (1) The event is fired
whenever the cursor is over the control, even if it is not over the image. In these cases we do not
want information displaying or the cursor changing. (2) The image may be scrolled, which
means the X and Y co-ordinates returned by the OnMouseMove event will not correspond to the
co-ordinates of the pixel in the image.
These problems can be overcome by using a group of properties that provide read-only
information about the status and positioning of the scroll bars. By checking the scroll bar
positions and modifying the X and Y co-ordinates as necessary the correct result can be obtained.
OverImage = True
With AxImageBox1
.
.
.
End If
End Sub
In the code above we use a Boolean 'OverImage' to determine whether or not the cursor is over
the image. This variable is initially set to true, then a number of checks are made to see if it
should be changed to False, or if the co-ordinates should be modified. First we check in the
horizontal direction. If there is no horizontal scroll bar, then the variable is only set to false if the
cursor is further to the right than the width of the image. If there is a horizontal scroll bar, then
we check that the cursor is inside the width of the scroll bar, and if necessary, adjust for the
scroll bar position. The same checks are then repeated in the vertical direction.
Finally, we retrieve and display the pixel information, if the cursor is confirmed to be over the
image.
If OverImage Then
Me.Cursor = Cursors.Cross
lblPixelX.Text = "X: " & e.x
lblPixelY.Text = "Y: " & e.y
PixelCol = AxImageBox1.get_PixelColor(e.x, e.y)
lblPixelRed.Text = "Red: " & Str(PixelCol.R)
lblPixelGreen.Text = "Green: " & Str(PixelCol.G)
lblPixelBlue.Text = "Blue: " & Str(PixelCol.B)
lblPixelCol.BackColor = PixelCol
Else
Me.Cursor = Cursors.Default
lblPixelX.Text = "X: n/a"
lblPixelY.Text = "Y: n/a"
lblPixelRed.Text = "Red: n/a"
lblPixelGreen.Text = "Green: n/a"
lblPixelBlue.Text = "Blue: n/a"
lblPixelCol.BackColor = Me.BackColor
End If
Using this code, the cursor can be moved over the image, with information about the colour of
the pixel under the mouse cursor displayed alongside, as in the screenshot above.
Removing the red glare from the eye is very simple in applications that use csXImage. The
method is demonstrated in the VB.NET demo program supplied in the csXImage download.
(1) A region around the eye, usually an ellipse or a circle, is selected. This requires just one line
of code.
AxImageBox1.MouseSelectEllipse()
(2) After the user completes the selection using the mouse, two further lines of code adjust the
red intensity for pixels where it is the dominant colour.
AxImageBox1.UseSelection = True
AxImageBox1.ReduceRedEye()