2

我有一个 Windows 窗体

this.BackColor = Color.Red

this.TransparencyKey = Color.Red

在这个表单上有一个 PictureBox(带有透明角的 png 图像),

PictureBox.SizeMode = Normal.

然后我将 PictureBox 的 SizeMode 设置为 StretchImage 并得到其他结果:
你可以在这里看到

(对不起,我只能放一个超链接)

您可以看到红色点/点,但它不是 Color.Red,因为它是表单的透明度键。

我尝试通过实现透明表单、透明控件来去除这些“红点”。无论如何,我想问一下我的最后一点 - 我试图覆盖“OnPaintBackground”方法,当我实现了一些类似下面的代码时:

e.Graphics.FillRectangle(Brushes.Red, ClientRectangle);
TextureBrush brush = ImageHelper.ScaleImage(BackgroundImage, ClientRectangle.Width, ClientRectangle.Height);
e.Graphics.FillRectangle(brush, ClientRectangle);

在将其放入 TextureBrush 之前,我将缩放的位图保存到文件中 - 这个 png 缩放图像不包含“红色”点,但它们是在表格上绘制的。

有人知道它为什么会发生并告诉我一些解决方法。

最好的祝福。

4

1 回答 1

1

发生这种情况是因为绘制图像的 GDI+ 不知道红色正在变得透明。

因此,它将图像的边界与红色背景混合,创建不透明的微红色(但不是完全红色)像素。

要解决这个问题,您需要制作一个分层窗口

编辑

使用以下本机方法:

static class NativeMethods {
    public const int LayeredWindow = 0x80000;//WS_EX_LAYERED

    #region Drawing
    [DllImport("User32.dll", SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool UpdateLayeredWindow(IntPtr handle, IntPtr screenDc, ref Point windowLocation, ref Size windowSize, IntPtr imageDc, ref Point dcLocation, int colorKey, ref BlendFunction blendInfo, UlwType type);

    [DllImport("gdi32.dll")]
    public static extern IntPtr CreateCompatibleDC(IntPtr hDC);

    [DllImport("User32.dll")]
    public static extern IntPtr GetDC(IntPtr hWnd);

    [DllImport("User32.dll")]
    public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);

    [DllImport("gdi32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool DeleteDC(IntPtr hdc);

    [DllImport("gdi32.dll")]
    public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);

    [DllImport("gdi32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool DeleteObject(IntPtr hObject);
    #endregion
}
struct BlendFunction {
    public byte BlendOp;
    public byte BlendFlags;
    public byte SourceConstantAlpha;
    public byte AlphaFormat;
}
enum UlwType : int {
    None = 0,
    ColorKey = 0x00000001,
    Alpha = 0x00000002,
    Opaque = 0x00000004
}

覆盖表单的CreateParams

protected override CreateParams CreateParams {
    get {
        CreateParams createParams = base.CreateParams;
        createParams.ExStyle |= NativeMethods.LayeredWindow;
        return createParams;
    }
}

在 中调用以下函数OnShown

static Point Zero;
[SuppressMessage("Microsoft.Usage", "CA1806:DoNotIgnoreMethodResults")]
void UpdateWindow() {
    IntPtr screenDC = NativeMethods.GetDC(IntPtr.Zero);
    IntPtr imageDC = NativeMethods.CreateCompatibleDC(screenDC);
    IntPtr gdiBitmap = IntPtr.Zero;
    IntPtr oldBitmap = IntPtr.Zero;

    try {
        gdiBitmap = image.GetHbitmap(Color.FromArgb(0));                //Get a GDI handle to the image.
        oldBitmap = NativeMethods.SelectObject(imageDC, gdiBitmap);     //Select the image into the DC, and cache the old bitmap.

        Size size = image.Size;                                         //Get the size and location of the form, as integers.
        Point location = this.Location;

        BlendFunction alphaInfo = new BlendFunction { SourceConstantAlpha = 255, AlphaFormat = 1 }; //This struct provides information about the opacity of the form.

        NativeMethods.UpdateLayeredWindow(Handle, screenDC, ref location, ref size, imageDC, ref Zero, 0, ref alphaInfo, UlwType.Alpha);
    } finally {
        NativeMethods.ReleaseDC(IntPtr.Zero, screenDC);                 //Release the Screen's DC.

        if (gdiBitmap != IntPtr.Zero) {                                 //If we got a GDI bitmap,
            NativeMethods.SelectObject(imageDC, oldBitmap);             //Select the old bitmap into the DC
            NativeMethods.DeleteObject(gdiBitmap);                      //Delete the GDI bitmap,
        }
        NativeMethods.DeleteDC(imageDC);                                //And delete the DC.
    }
    Invalidate();
}
于 2010-02-23T15:26:20.793 回答