How to make the background image transparent in windows mobile?

喜你入骨 提交于 2019-12-24 06:36:10

问题


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?


回答1:


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);
}


来源:https://stackoverflow.com/questions/3793261/how-to-make-the-background-image-transparent-in-windows-mobile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!