views:

1474

answers:

2

Hello,

is there a way, to make a picture transparent in CF2.0? I have to lay a small Image over a textbox, but it has to be transparent so the User can see the text anyway. Do you have an Idea?

Thank you very much

twickl

Edit:

Thanks for your answers, I will check those links out! To complete my Post, here is what I´m trying to do:

I want to show a small image (the image does not exist yet and I have to make ist, so I´m totaly open for all formats) that is an X on the right end of a textbox. By clicking that X the Text inside the textbox will be erased...like on the iPhone. But I can not build my own control becuse in my Project are so many TextBoxes that are allready custom Controls with the windows TextBox on it, that it will be to much work and testing to switch all of them to custom controls. So I have the Idea to make a small Panel, Picturebox, whatever, that lays above the Textbox. But it has to be transparent. The OS is Windows CE 5.0 with CF 2.0 on it.

+3  A: 

I did it by deriving a class from PictureBox and handling OnPaint. The key is the ImageAttributes object passed to DrawImage. I'm assuming pixel 0,0 is the transparent color, but you could handle that differently.

public partial class TransparentPictureBox : PictureBox
{
    private Color tColor;

    public TransparentPictureBox()
    {
        InitializeComponent();
    }

    public new Image Image
    {
        get { return base.Image; }
        set
        {
            if (value == base.Image)
                return;

            if (value != null)
            {
                Bitmap bmp = new Bitmap(value);
                tColor = bmp.GetPixel(0, 0);
                this.Width = value.Width;
                this.Height = value.Height;
            }
            base.Image = value;
        }
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        e.Graphics.Clear(this.BackColor);

        if (Image == null)
            return;

        ImageAttributes attr = new ImageAttributes();

        // Set the transparency color.
        attr.SetColorKey(tColor, tColor);

        Rectangle dstRect = new Rectangle(0, 0, base.Image.Width, base.Image.Height);
        e.Graphics.DrawImage(base.Image, dstRect, 0, 0, base.Image.Width, base.Image.Height, GraphicsUnit.Pixel, attr);
    }
}
ratchetr
+4  A: 

Depending on what kind of transparency you need, you might choose any of these options:

1.) If you have an image with a specific portion that should be entirely transparent, you can use ImageAttributes.SetColorKey() to set a single transparent color and then pass this to Graphics.DrawImage. Your image will need to have one color (e.g. Color.Cyan) that will be drawn completely transparent.

2.) If you'd like the entire image to be partially transparent, e.g. for a fade in/fade out effect, you can P/Invoke the AlphaBlend() function, as demonstrated here.

3.) If you have an image with transparency information built in, e.g. a transparent PNG image that needs to be rendered on a variety of background colors, these previous methods will not work and you need to use the COM based IImage interface. The COM interop from .NETCF is documented on this page (search for "IImage interface" on that page).

Option 3 is the most flexible, but it also involves the most implementation effort. If you follow up with more information about the kind of image you want to draw transparently and your target platform, we might be able to help more.

Daniel Weaver