gpt4 book ai didi

c# - 透明图像上的红点

转载 作者:太空宇宙 更新时间:2023-11-03 14:32:20 26 4
gpt4 key购买 nike

我有一个 Windows 窗体

this.BackColor = Color.Red

this.TransparencyKey = Color.Red

并且在这个表单上有一个PictureBox(带透明边角的png图片),

PictureBox.SizeMode = Normal.

然后我将 PictureBox 的 SizeMode 设置为 StretchImage 并得到其他结果:
you can see it here

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

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

我尝试实现透明表单、透明控件以删除这些“红色”点。无论如何,我想问一下我的最后一点——我试图重写“OnPaintBackground”方法,当我实现一些类似下面的代码时:

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

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

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

最好的问候。

最佳答案

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

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

要解决这个问题,您需要制作一个 layered window .

编辑:

使用以下原生方法:

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

关于c# - 透明图像上的红点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2319264/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com