Home

The Characteristics of a Bitmap

 
 

The Location of a Bitmap

To represent a picture in your application, such as on a form, the primary information you must provide is its location. The location of a picture is the measure, in pixels, of its top and its left corner with regards to the object that is hosting it:

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 void btnShowPicture_Click(object sender, EventArgs e)
{
            Graphics graph = CreateGraphics();
            Bitmap bmpPicture = new Bitmap("woman1.jpg");

            graph.DrawImage(bmpPicture, 10, 12);
}

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 void DrawImage(Image image, float x, float y);

Here is an example:

private void btnShowPicture_Click(object sender, EventArgs e)
{
            Graphics graph = CreateGraphics();
            Bitmap bmpPicture = new Bitmap("woman1.jpg");

            graph.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 LearningPractical Learning: Positioning a Picture

  1. Click the body of the form
  2. In the Properties window, click the Events button and double-click Paint
  3. Implement the event as follows:
     
    private void Form1_Paint(object sender, PaintEventArgs e)
    {
            if (strPicture != "")
            {
                    Image bmpPicture = Image.FromFile(this.strPicture);
                    e.Graphics.DrawImage(bmpPicture, 10, 40);
                    PictureIsLoaded = true;
            }
    }
  4. Execute the application
     
  5. Try opening a picture
  6. Close the form and return to your programming environment

The Size of a Picture

A picture usually appears as a geometric figure such as a rectangle:

A Picture

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:

The Size of a Picture

When creating or designing 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:

private void btnShowPicture_Click(object sender, EventArgs e)
{
        Bitmap bmpSample = new 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:

private void btnShowPicture_Click(object sender, EventArgs e)
{
        OpenFileDialog dlgOpen = new OpenFileDialog();

        if (dlgOpen.ShowDialog() == DialogResult.OK)
        {
                string strFilename = dlgOpen.FileName;
                Bitmap bmpPicture = new 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();
        }
}

The Size of a Picture

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:

Building
private void Form1_Paint(object sender, PaintEventArgs e)
{
        Bitmap bmpBuilding = new Bitmap("building.bmp");
        e.Graphics.DrawImage(bmpBuilding, 0, 0);

        Bitmap bmpMonument = new Bitmap("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:

Flying Bird
private void Form1_Paint(object sender, PaintEventArgs e)
{
        Bitmap bmpFlying = new Bitmap("flying.bmp");
        e.Graphics.DrawImage(bmpFlying, 0, 0);

        Bitmap bmpGlobe = new Bitmap("globe.bmp");
        bmpGlobe.MakeTransparent(Color.Black);
        e.Graphics.DrawImage(bmpGlobe, 20, 120);
}

This would produce:

Transparent

Practical LearningPractical Learning: Using the Transparency of a Picture

  1. To create a new program, on the main menu, click File -> New -> Project...
  2. In the Templates list, click Windows Application
  3. Set the Name to ImageFloater and click OK
  4. From the Components section of the Toolbox, click Timer and click the form
  5. While the timer is still selected under the form, in the Properties window, set its Enabled property to True and its Interval to 1000
  6. 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
     
    House
  7. Under the form, double-click the time1 control and implement its event as follows:
     
    private void timer1_Tick(object sender, EventArgs e)
    {
            Bitmap bmpArea = new Bitmap(Width, Height);
            Graphics graphArea = Graphics.FromImage(bmpArea);
    
            Bitmap bmpDiamond = new Bitmap("diamond.bmp");
            bmpDiamond.MakeTransparent();
    
            Bitmap bmpHouse = new Bitmap("house.bmp");
            graphArea.DrawImage(bmpHouse, 0, 0);
    
            Random rnd = new 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);
    }
  8. Execute the application to see the result
 

Previous Copyright © 2007 FunctionX, Inc. Home