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

时间:2021-03-22 00:26:37

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?

有没有办法,在CF2.0中使图片透明?我必须在文本框上放置一个小图像,但它必须是透明的,这样用户无论如何都可以看到文本。你有好主意吗?

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.

我想显示一个小图像(图像不存在,我必须制作ist,所以我完全打开所有格式),这是文本框右端的X.通过单击该X,文本框内的文本将被删除...就像在iPhone上一样。但我无法在我的项目中构建自己的控件,因为有很多TextBox,它们都是自定义控件,上面有Windows TextBox,它将进行大量工作并测试将所有这些控件切换到自定义控件。所以我有想法制作一个小型面板,Picturebox,无论如何,它位于文本框之上。但它必须是透明的。操作系统是带有CF 2.0的Windows CE 5.0。

2 个解决方案

#1


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.

我是通过从PictureBox派生一个类并处理OnPaint来实现的。关键是传递给DrawImage的ImageAttributes对象。我假设像素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);
    }
}

#2


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.

1.)如果您的图像具有应完全透明的特定部分,则可以使用ImageAttributes.SetColorKey()设置单个透明颜色,然后将其传递给Graphics.DrawImage。您的图像需要有一种颜色(例如Color.Cyan),它将被绘制为完全透明。

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.

2.)如果您希望整个图像部分透明,例如对于淡入/淡出效果,您可以P / Invoke AlphaBlend()函数,如此处所示。

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).

3.)如果您的图像内置了透明度信息,例如需要在各种背景颜色上呈现的透明PNG图像,这些以前的方法不起作用,您需要使用基于COM的IImage界面。此页面上记录了.NETCF的COM互操作(在该页面上搜索“IImage界面”)。

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.

选项3是最灵活的,但它也涉及最多的实施工作。如果您跟进有关要透明绘制的图像类型和目标平台的更多信息,我们可能会提供更多帮助。

#1


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.

我是通过从PictureBox派生一个类并处理OnPaint来实现的。关键是传递给DrawImage的ImageAttributes对象。我假设像素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);
    }
}

#2


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.

1.)如果您的图像具有应完全透明的特定部分,则可以使用ImageAttributes.SetColorKey()设置单个透明颜色,然后将其传递给Graphics.DrawImage。您的图像需要有一种颜色(例如Color.Cyan),它将被绘制为完全透明。

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.

2.)如果您希望整个图像部分透明,例如对于淡入/淡出效果,您可以P / Invoke AlphaBlend()函数,如此处所示。

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).

3.)如果您的图像内置了透明度信息,例如需要在各种背景颜色上呈现的透明PNG图像,这些以前的方法不起作用,您需要使用基于COM的IImage界面。此页面上记录了.NETCF的COM互操作(在该页面上搜索“IImage界面”)。

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.

选项3是最灵活的,但它也涉及最多的实施工作。如果您跟进有关要透明绘制的图像类型和目标平台的更多信息,我们可能会提供更多帮助。