简体   繁体   English

如何在没有第三方库的情况下在CF 2.0中制作透明图像?

[英]How to make a transparent Image in CF 2.0 without third party libraries?

is there a way, to make a picture transparent in CF2.0? 有没有办法,在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 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. 我想显示一个小图像(图像不存在,我必须制作ist,所以我完全打开所有格式),这是文本框右端的X. By clicking that X the Text inside the textbox will be erased...like on the iPhone. 通过单击该X,文本框内的文本将被删除...就像在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. 但我无法在我的项目中构建自己的控件,因为有很多TextBox,它们都是自定义控件,上面有Windows TextBox,它将进行大量工作并测试将所有这些控件切换到自定义控件。 So I have the Idea to make a small Panel, Picturebox, whatever, that lays above the Textbox. 所以我有想法制作一个小的Panel,Picturebox,无论如何,它位于Textbox之上。 But it has to be transparent. 但它必须是透明的。 The OS is Windows CE 5.0 with CF 2.0 on it. 操作系统是带有CF 2.0的Windows CE 5.0。

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. 1.)如果您的图像具有应完全透明的特定部分,则可以使用ImageAttributes.SetColorKey()设置单个透明颜色,然后将其传递给Graphics.DrawImage。 Your image will need to have one color (eg Color.Cyan) that will be drawn completely transparent. 您的图像需要有一种颜色(例如Color.Cyan)才能完全透明。

2.) If you'd like the entire image to be partially transparent, eg for a fade in/fade out effect, you can P/Invoke the AlphaBlend() function, as demonstrated here . 2。)如果想整个图像是部分透明的,例如,用于淡入/淡出效果,可以P /调用的AlphaBlend()函数,这表现在这里

3.) If you have an image with transparency information built in, eg 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. 3.)如果你有一个内置透明度信息的图像,例如需要在各种背景颜色上渲染的透明PNG图像,这些以前的方法将不起作用,你需要使用基于COM的IImage界面。 The COM interop from .NETCF is documented on this page (search for "IImage interface" on that page). 此页面上记录了.NETCF的COM互操作(在该页面上搜索“IImage界面”)。

Option 3 is the most flexible, but it also involves the most implementation effort. 选项3是最灵活的,但它也涉及最多的实施工作。 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. 如果您跟进有关要透明绘制的图像类型和目标平台的更多信息,我们可能会提供更多帮助。

I did it by deriving a class from PictureBox and handling OnPaint. 我是通过从PictureBox派生一个类并处理OnPaint来实现的。 The key is the ImageAttributes object passed to DrawImage. 关键是传递给DrawImage的ImageAttributes对象。 I'm assuming pixel 0,0 is the transparent color, but you could handle that differently. 我假设像素0,0是透明色,但你可以用不同的方式处理。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM