1

I'm just starting out with VB.net after many years of VB 6.0 and I thought I'd get started with some graphics.

Just for fun I'm using BitBlt to draw a bitmap I have loaded as a resource and draw it on a form. I can load an existing image from a picturebox and bitblt it, which works fine. I can also load the bitmap from resources and store it into the picturebox, but when I take the bitmap, turn it into a GDI Graphics object and BitBlt it onto the form, all I get is a black square that is the same size as the bitmap.

Here is my code:

    Dim srcBmp As New Bitmap(Me.GetType, "colorwheel.bmp")
    Dim drect As New Rectangle(0, 0, 233, 233)
    'srcGrp = PictureBox1.CreateGraphics 'This works.
    srcGrp = Graphics.FromImage(srcBmp)     'This doesn't 
    targetGrp = Me.CreateGraphics 'destination graphics

    srcHdc = srcGrp.GetHdc
    TargetHdc = targetGrp.GetHdc

    BitBlt(TargetHdc, 0, 0, 233, 233, srcHdc, 0, 0, SRCCOPY)

    srcGrp.ReleaseHdc(srcHdc)
    targetGrp.ReleaseHdc(TargetHdc)
    targetGrp.Dispose()
    srcBmp.Dispose()
    srcGrp.Dispose()

The reasons I want to use bitblt are a) I've used it before, so it was my first choice. b) I know that it is possible to use a black and white mask to mask out some areas as transparent, which is what I need. and b) the graphics may be moving around frequently.

Is there a better way of drawing graphics with a transparent background onto a form or picturebox? e.g. a png with an alpha channel?

4

1 回答 1

2

解决了。

1) 加载位图 2) 从位图获取 Hbitmap 3) 创建空白 DC 流 4) 从 DC 流创建兼容 DC

        Dim srcBmp As New Bitmap(Me.GetType, "colorwheel.png")
        Dim hbm As IntPtr = srcBmp.GetHbitmap()
        Dim sdc As IntPtr = GetDC(IntPtr.Zero)
        Dim hdc As IntPtr = CreateCompatibleDC(sdc)

然后,您可以使用 hdc 进行 bitblt。发现 bitblt 在 .net 中的工作方式与在 vb6 中的工作方式不同。我将尝试使用 DrawImage 方法。

于 2013-05-08T01:06:16.577 回答