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:
System::Void btnDisplayPicture_Click(System::Object^ sender,
System::EventArgs^ e)
{
Graphics ^ graph = CreateGraphics();
Bitmap ^ bmpPicture = gcnew Bitmap(L"woman.bmp");
graph->DrawImage(bmpPicture, 40, 25);
}
As opposed to integers, you can also pass the location
values as floating point numbers. This is done using the following version of
the Graphics::DrawImage() method:
public:
void DrawImage(Image^ image, float x, float y);
Here is an example:
System::Void Form1_Paint(System::Object^ sender,
System::Windows::Forms::PaintEventArgs^ e)
{
Bitmap ^ bmpPicture =
static_cast<Bitmap ^>(Image::FromFile(L"professionals.png"));
e->Graphics->DrawImage(bmpPicture, 10.50f, 12.20F);
}
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:
void DrawImage(Image^ image, Point point);
If the values of the Point are floating point
numbers, you can use the following version:
public:
void DrawImage(Image^ image, PointF point);
Practical
Learning: Positioning a Picture
|
|
- Click the body of the form
- In the Properties window, click the Events button and double-click Paint
- Implement the event as follows:
System::Void Form1_Paint(System::Object^ sender,
System::Windows::Forms::PaintEventArgs^ e)
{
if( strPicture != L"" )
{
Image ^ bmpPicture = Image::FromFile(this->strPicture);
e->Graphics->DrawImage(bmpPicture, 10, 40);
this->PictureIsLoaded = true;
}
}
|
- 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 a Bitmap object, you can specify its
primary size. To do this, you can use the following constructor:
public:
Bitmap(int width, int height);
The width and the height arguments are as we
illustrated them above. Here is an example of using this constructor:
System::Void btnCreateBitmap_Click(System::Object^ sender,
System::EventArgs^ e)
{
Bitmap ^ bmpSample = gcnew Bitmap(450, 625);
}
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:
System::Void btnLoadPicture_Click(System::Object^ sender,
System::EventArgs^ e)
{
OpenFileDialog ^ dlgOpen = gcnew OpenFileDialog;
if( dlgOpen->ShowDialog() == ::DialogResult::OK )
{
String ^ strFilename = dlgOpen->FileName;
Bitmap ^ bmpPicture = gcnew Bitmap(strFilename);
Graphics ^ graph = CreateGraphics();
graph->DrawImage(bmpPicture, 120, 12);
int width = bmpPicture->Width;
int height = bmpPicture->Height;
txtWidth->Text = width.ToString();
txtHeight->Text = height.ToString();
}
}
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:
void 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:
System::Void Form1_Paint(System::Object^ sender,
System::Windows::Forms::PaintEventArgs^ e)
{
Bitmap ^ bmpBuilding = gcnew Bitmap(L"building.bmp");
e->Graphics->DrawImage(bmpBuilding , 0, 0);
Bitmap ^ bmpMonument = gcnew Bitmap(L"monument.bmp");
bmpMonument ->MakeTransparent();
e->Graphics->DrawImage(bmpMonument , 200, 260);
}
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:
void MakeTransparent(Color transparentColor);
With this method, instead of letting the operating
system determine the transparency color, you pass your own as argument. Here is
an example:
System::Void Form1_Paint(System::Object^ sender,
System::Windows::Forms::PaintEventArgs^ e)
{
Bitmap ^ bmpFlying = gcnew Bitmap(L"flying.bmp");
e->Graphics->DrawImage(bmpFlying, 0, 0);
Bitmap ^ bmpGlobe = gcnew Bitmap(L"globe.bmp");
bmpGlobe->MakeTransparent(Color::Black);
e->Graphics->DrawImage(bmpGlobe, 20, 120);
}
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 Forms 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 folder inside the ImageFloater folder
- Under the form, double-click the time1 control and implement its event as
follows:
System::Void timer1_Tick(System::Object^ sender,
System::EventArgs^ e)
{
Bitmap ^ bmpArea = gcnew Bitmap(Width, Height);
Graphics ^ graphArea = Graphics::FromImage(bmpArea);
Bitmap ^ bmpDiamond = gcnew Bitmap(L"diamond.bmp");
bmpDiamond->MakeTransparent();
Bitmap ^ bmpHouse = gcnew Bitmap(L"house.bmp");
graphArea->DrawImage(bmpHouse, 0, 0);
Random ^ rnd = gcnew Random;
int rndLeft = rnd->Next(bmpHouse->Width);
int rndTop = rnd->Next(bmpHouse->Height);
graphArea->DrawImage(bmpDiamond, rndLeft, rndTop);
Graphics ^ painter = Graphics::FromHwnd(Handle);
painter->DrawImage(bmpArea, 0, 0);
}
- Execute the application to see the result
|
|