views:

57

answers:

1

I am developing the smart device application in C#. I am new to the windows mobile. I have added the background image to the form in my application by using the following code. I want to make this image transparent so that other controls on my windows form will be displayed properly.

protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Bitmap CreateCustomerImage = new Bitmap(@"/Storage Card/background.png");
            e.Graphics.DrawImage(CreateCustomerImage, 0, 0);
        }

The background image has blue color. When the application runs the controls such as label, text box & other controls displayed in white color. How to solve this problem? Can you provide me any code or link through which I can solve the above issue?

A: 

I can't make out if you want to make your controls transparent or an image, but if you want to make an image transparent when drawing it here's how.

You need to set a particular pixel as the transparent color as Windows Mobile doesn't support it "natively". You need to create an ImageAttributes instance that you use when drawing the image. The example below uses the pixel in the upper left corner as the "transparent color".

private readonly ImageAttributes imgattr;
.ctor() {
    imgattr = new ImageAttributes();
    Color trns = new Bitmap(image).GetPixel(0, 0);
    imgattr.SetColorKey(trns, trns);
}

protected override void OnPaint(PaintEventArgs e) {
    e.Graphics.DrawImage(image,
                         new Rectangle(0, 0, Width, Height),
                         0, 0, image.Width, image.Height,
                         GraphicsUnit.Pixel,
                         imgattr);
}
Patrick