Question

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?

Was it helpful?

Solution

worked it out.

1) load bitmap 2) get Hbitmap from bitmap 3) create blank DC stream 4) create compatible DC from DC stream

        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)

you can then bitblt with the hdc. Found that bitblt doesn't work in .net the same as it used to in vb6. I'm going to try the DrawImage method instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top