Using the location, to specify where to display a picture,
you can pass the x and y values as the second and the third arguments,
respectively, of the Graphics.DrawImage() method. Here is an example:
Private Sub btnPictureClicked(ByVal sender As Object, _
ByVal e As EventArgs) _
Handles btnPicture.Click
Dim bmpPicture As Bitmap
bmpPicture = New Bitmap("woman.jpg")
Dim graph As Graphics = CreateGraphics()
graph.DrawImage(bmpPicture, 10, 12)
End Sub
As opposed to integers, you can also pass the location's
values as floating point numbers. This is done using the following version of
the Graphics.DrawImage() method:
Public Sub DrawImage(image As Image, x As Single, y As Single)
Here is an example:
Private Sub btnPictureClicked(ByVal sender As Object, _
ByVal e As EventArgs) _
Handles btnPicture.Click
Dim bmpPicture As Bitmap
bmpPicture = New Bitmap("woman.jpg")
Dim graph As Graphics = CreateGraphics()
graph.DrawImage(bmpPicture, 10.5F, 12.2F)
End Sub
You can also specify the location as a Point. If the
properties of the Point are integers, you can use the following flavor of
the Graphics.DrawImage() method:
Public Sub DrawImage(image As Image, point As Point)
If the values of the Point are floating point
numbers, you can use the following version:
Public Sub DrawImage(image As Image, point As PointF)
Practical
Learning: Positioning a Picture
|
|
- In the Class Name combo box, select (Form1 Events)
- In the Method Name combo box, select Paint and implement the event as
follows:
Private Sub Form1_Paint(ByVal sender As Object, _
ByVal e As System.Windows.Forms.PaintEventArgs) _
Handles Me.Paint
If strPicture <> "" Then
Dim bmpPicture As Image = Image.FromFile(strPicture)
e.Graphics.DrawImage(bmpPicture, 10, 40)
PictureIsLoaded = True
End If
End Sub
|
- Execute the application
- Try opening a picture
- Close the form and return to your programming environment
A picture usually appears as a geometric figure such as a
rectangle:
Through some manipulations, a picture can appear
non-rectangular. Regardless of the perception, a picture is meant to fit a
rectangular figure. As such, a picture has a size, represented by a width and a
height, in pixels. The width of a picture is the distance from its left to its
right borders. The height of a picture is the distance, in pixels, between its
top and its bottom borders. The size of a picture can be illustrated as follows:
When creating or designing a Bitmap object, you can
specify its primary size. To do this, you can use the following constructor:
Public Sub New(width As Integer, height As Integer)
The width and the height arguments are as we
illustrated them above. Here is an example of using this constructor:
Private Sub btnPictureClicked(ByVal sender As Object, _
ByVal e As EventArgs) _
Handles btnPicture.Click
Dim bmpSample As Bitmap
bmpSample = New Bitmap(450, 625)
End Sub
If you have a picture, you can find out what its size is. To
assist you with knowing the width of a picture, the Image class, the
parent of the Bitmap class, provides the Width property, which is
of type integer. In the same way, to give you the height of a picture,
the Image class is equipped with a property named Height, which
also is an int type. Here is an example of getting the dimensions of a
picture:
Private Sub btnShowPicture_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnShowPicture.Click
Dim width As Integer
Dim height As Integer
Dim dlgOpen As OpenFileDialog
dlgOpen = New OpenFileDialog()
If dlgOpen.ShowDialog() = Windows.Forms.DialogResult.OK Then
Dim strFilename As String = dlgOpen.FileName
Dim bmpPicture As Bitmap = New Bitmap(strFilename)
Dim graph As Graphics = CreateGraphics()
graph.DrawImage(bmpPicture, 120, 12)
width = bmpPicture.Width
height = bmpPicture.Height
TextBox1.Text = width.ToString()
TextBox2.Text = height.ToString()
End If
End Sub
To get both the width and the height as one object, the
Image class provides to its children, such as Bitmap, the Size
property. As you may guess, the Size property is of type Size.
Besides the Width, the Height, and the Size
properties, to get the size of a bitmap, you can access the
PhysicalDimensions property of the Image class. This property is of
type SizeF but its values depend on the type of picture.
The Transparency of a Bitmap
|
|
In the previous lesson, we saw that a graphic could be made
of thousands to millions of colors. When you display a picture in your
application, you can ask Microsoft Windows to "see through" one particular
color. Seeing through is referred to as transparency. This is an operation
regularly needed in graphics applications, the operating system is already
equipped to select a default color it considers for transparency. In most cases,
you can use that color. Otherwise, for your particular application, you can
specify what color you want to use as "see through".
To support picture transparency, the Bitmap class is
equipped with the MakeTransparent() method that is overloaded with two
versions. The first version uses the following syntax:
Public Sub MakeTransparent
When you call this method, it lets the operating system
choose the color used for transparency. In most cases, the operating system
selects white as the transparency color. Consequently, when you display the
picture, wherever a white spot or area is shown on the picture, that area would
disappear to show behind it. Here is an example:
Private Sub FormPaint(ByVal sender As Object, _
ByVal e As PaintEventArgs) _
Handles MyBase.Paint
Dim bmpBuilding As Bitmap
Dim bmpMonument As Bitmap
bmpBuilding = New Bitmap("building.gif")
e.Graphics.DrawImage(bmpBuilding, 0, 0)
bmpMonument = New Bitmap("monument.jpg")
bmpMonument.MakeTransparent()
e.Graphics.DrawImage(bmpMonument, 200, 260)
End Sub
This would produce:
Instead of using the default transparency color of the
operating system, you can specify your own color. To support this, the Bitmap
class provides another version of the MakeTransparent() method. Its
syntax is:
Public Sub MakeTransparent(transparentColor As Color)
With this method, instead of letting the operating system
determine the transparency color, you pass your own as argument. Here is an
example:
Private Sub FormPaint(ByVal sender As Object, _
ByVal e As PaintEventArgs) _
Handles MyBase.Paint
Dim bmpFlying As Bitmap
Dim bmpGlobe As Bitmap
bmpFlying = New Bitmap("flying.jpg")
bmpGlobe = New Bitmap("globe.jpg")
e.Graphics.DrawImage(bmpFlying, 0, 0)
bmpGlobe.MakeTransparent(Color.Black)
e.Graphics.DrawImage(bmpGlobe, 20, 120)
End Sub
This would produce:
Practical
Learning: Using the Transparency of a Picture
|
|
- To create a new program, on the main menu, click File -> New ->
Project...
- In the Templates list, click Windows Application
- Set the Name to ImageFloater and click OK
- From the Components section of the Toolbox, click Timer and click the
form
- While the timer is still selected under the form, in the Properties
window, set its Enabled property to True and its Interval to
1000
- Copy the following pictures (click the left picture to open the real one
in the browser) in the ImageFloater\ImageFloater\bin\debug folder inside the
current project
- Under the form, double-click the time1 control and implement its event
as follows:
Public Class Form1
Private Sub Timer1_Tick(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Timer1.Tick
Dim bmpArea As Bitmap = New Bitmap(Width, Height)
Dim graphArea As Graphics = Graphics.FromImage(bmpArea)
Dim bmpDiamond As Bitmap = New Bitmap("diamond.bmp")
bmpDiamond.MakeTransparent()
Dim bmpHouse As Bitmap = New Bitmap("house.bmp")
graphArea.DrawImage(bmpHouse, 0, 0)
Dim Rnd As Random = New Random()
Dim rndLeft As Integer = Rnd.Next(bmpHouse.Width)
Dim rndTop As Integer = Rnd.Next(bmpHouse.Height)
graphArea.DrawImage(bmpDiamond, rndLeft, rndTop)
Dim Painter As Graphics = Graphics.FromHwnd(Handle)
Painter.DrawImage(bmpArea, 0, 0)
End Sub
End Class
|
- Execute the application to see the result
|
|