使用本机 win32 gdi 绘制一条透明线



我正在使用win32 gdi本机API绘制线条。现在,我想把这条线画成透明的。我已经在颜色中设置了 alpha 通道属性。但是,将 Alpha 通道设置为颜色不会将线条绘制为透明。我读过关于Alpha Blend Api的信息,但无法找出解决方案。

var hdc = g.GdiDeviceContext;
var srcHdc = CreateCompatibleDC(hdc);
var clipRegion = CreateRectRgn(x, y, x + width, y + height);
SelectClipRgn(hdc, clipRegion);
var pen = CreatePen(PenStyle.Solid, LineWidth, (uint)ColorTranslator.ToWin32(colour));

if (pen != IntPtr.Zero)
{
var oldPen = SelectObject(hdc, pen);
Polyline(hdc, points, points.Length);
SelectObject(hdc, oldPen);
DeleteObject(pen);
}
SelectClipRgn(hdc, IntPtr.Zero);
AlphaBlend(hdc, x, y, width, height, srcHdc, x, y, width, height, new BlendFunction(0x00, 0, 0x7f, 0x00));
DeleteObject(clipRegion);

我试图把界限画成透明的。

var srcHdc = CreateCompatibleDC(hdc);

这将创建内存设备上下文。这是正确的第一步。但是内存直流还没有准备好。它还需要内存位图。

SelectObject(hdc, pen);
Polyline(hdc, points, points.Length);

这将利用Windows设备上下文。但是我们希望利用存储设备上下文,然后使用AlphaBlend将内存绘制到HDC

请参阅以下示例:

int w = 100;
int h = 100;
//create memory device context
var memdc = CreateCompatibleDC(hdc); 
//create bitmap
var hbitmap = CreateCompatibleBitmap(hdc, w, h);
//select bitmap in to memory device context
var holdbmp = SelectObject(memdc, hbitmap);
//begine drawing:
var hpen = CreatePen(0, 4, 255);
var holdpen = SelectObject(memdc, hpen);
Rectangle(memdc, 10, 10, 90, 90);
//draw memory device (memdc) context on to windows device context (hdc)
AlphaBlend(hdc, 0, 0, w, h, memdc, 0, 0, w, h, new BLENDFUNCTION(0, 0, 128, 0));
//clean up:
SelectObject(memdc, holdbmp);
SelectObject(memdc, holdpen);
DeleteObject(hbitmap);
DeleteObject(hpen);
DeleteDC(memdc);

最新更新