Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 How do I colour key in gdi32?
Index -> Programming, Visual Basic and Other Basics -> Visual Basic Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
CodeMonkey2000




PostPosted: Sat Oct 20, 2007 12:20 pm   Post subject: How do I colour key in gdi32?

I don't know what this is called in VB, what I want is to set transparent colours, so when I blit the image, the white pixels get ignored.
Sponsor
Sponsor
Sponsor
sponsor
Nick




PostPosted: Sat Oct 20, 2007 4:16 pm   Post subject: RE:How do I colour key in gdi32?

u mean similair's to turing's picMerge?
Euphoracle




PostPosted: Sat Oct 20, 2007 5:17 pm   Post subject: RE:How do I colour key in gdi32?

No, he means set a transparency key so any pixel of that color is not drawn. PicMerge is just a method of drawing it, whereas it simply leaves the empty pixels as they were before drawing the image, thus giving you the "transparent" effect.
CodeMonkey2000




PostPosted: Sun Oct 21, 2007 3:26 pm   Post subject: RE:How do I colour key in gdi32?

Well I found something on Microsoft's site that might help, you have to use vbMergeCopy. But the problem is I don't know how to use it.

EDIT: I found another function, it's called transparentblt. It's a wee bit slow though. Is there an alternative?
Brightguy




PostPosted: Mon Oct 22, 2007 2:13 am   Post subject: Re: How do I colour key in gdi32?

Yeah, there's TransparentBlt (which apparently has a memory leak on Win98) or you can do it with two calls to BitBlt. First, prepare a copy of your image with black pixels everywhere you want transparency. Then, prepare the "mask": your image with white pixels everywhere you want transparency and black pixels where you don't. You can use these images to create transparency as follows:

First, blit the mask onto the background using the raster operation AND (a bitwise AND on the pixel values). The white areas are 1 in binary so they don't change (x AND 1 = x), while the black areas are 0s in binary so are set to 0 (x AND 0 = 0). Thus, you end up with a black "hole" in the background in the shape of your image.
Then, blit the first image onto the background using the raster operation OR (a bitwise OR on the pixel values). The black areas are 0 in binary so they don't change (x OR 0 = x), so you can blit your image onto the black hole without changing anything, and without changing any pixels outside of the hole.

If want to be really 1337, you can automate the process of creating the mask, given an image and a background colour. I've updated my "double buffering" example program to do this. To try it, just change the filename and picBackground constants as appropriate (I'll attach an example image with this post). I've also made it so you can change the displayed background colour.

VisualBASIC:
Option Explicit

Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
Private Declare Function CreateBitmap Lib "gdi32" (ByVal nWidth As Long, ByVal nHeight As Long, ByVal nPlanes As Long, ByVal nBitCount As Long, lpBits As Any) As Long
Private Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hdc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As Long) As Long
Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Private Declare Function ExtFloodFill Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal crColor As Long, ByVal wFillType As Long) As Long
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetObject Lib "gdi32.dll" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, ByRef lpObject As Any) As Long
Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
Private Declare Function SetBkColor Lib "gdi32" (ByVal hdc As Long, ByVal crColor As Long) As Long

Private Type BITMAP
    bmType As Long
    bmWidth As Long
    bmHeight As Long
    bmWidthBytes As Long
    bmPlanes As Integer
    bmBitsPixel As Integer
    bmBits As Long
End Type

Private Const filename As String = "C:\doggie2.gif"
Private Const picBackground As Long = &H000100
Private Const backColour As Long = vbBlue
Private backDC As Long, picDC As Long, maskDC As Long, picInfo As BITMAP

Private Sub Form_Load()
    'Damn twips
    Me.ScaleMode = vbPixels

    'Load the picture into the device context and retrieve info
    picDC = CreateCompatibleDC(GetDC(0))
    SelectObject picDC, LoadPicture(filename)
    GetObject LoadPicture(filename), Len(picInfo), picInfo
   
    'Picture background colour
    SetBkColor picDC, picBackground

    'Create mask
    Dim maskBitmap As Long
    maskDC = CreateCompatibleDC(GetDC(0))
    maskBitmap = CreateBitmap(picInfo.bmWidth, picInfo.bmHeight, 1, 1, ByVal 0)
    SelectObject maskDC, maskBitmap
    BitBlt maskDC, 0, 0, picInfo.bmWidth, picInfo.bmHeight, picDC, 0, 0, vbSrcCopy
    BitBlt picDC, 0, 0, picInfo.bmWidth, picInfo.bmHeight, maskDC, 0, 0, vbSrcInvert
    DeleteObject maskBitmap
   
    'Create device context for the background
    backDC = CreateCompatibleDC(GetDC(0))
End Sub

Private Sub Form_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
    'Draw picture on background
    Form_MouseMove Button, Shift, x, y
End Sub

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
    If Button > 0 Then
        'Draw picture on background
        BitBlt backDC, x - Int(picInfo.bmWidth / 2), y - Int(picInfo.bmHeight / 2), picInfo.bmWidth, picInfo.bmHeight, maskDC, 0, 0, vbSrcAnd
        BitBlt backDC, x - Int(picInfo.bmWidth / 2), y - Int(picInfo.bmHeight / 2), picInfo.bmWidth, picInfo.bmHeight, picDC, 0, 0, vbSrcPaint
        Form_Paint
    End If
End Sub

Private Sub Form_Paint()
    'Draw background on form
    BitBlt Me.hdc, 0, 0, Me.ScaleWidth, Me.ScaleHeight, backDC, 0, 0, vbSrcCopy
End Sub

Private Sub Form_Resize()
    'Create bitmap of proper size for the background
    Dim backBitmap As Long
    backBitmap = CreateCompatibleBitmap(GetDC(0), Me.ScaleWidth, Me.ScaleHeight)
    SelectObject backDC, backBitmap

    'Create background colour brush
    Dim backBrush As Long
    backBrush = CreateSolidBrush(backColour)
    SelectObject backDC, backBrush

    'Fill black area with brush colour
    ExtFloodFill backDC, 0, 0, vbBlack, 1

    'Draw picture in the middle of background
    BitBlt backDC, Int((Me.ScaleWidth - picInfo.bmWidth) / 2), Int((Me.ScaleHeight - picInfo.bmHeight) / 2), picInfo.bmWidth, picInfo.bmHeight, maskDC, 0, 0, vbSrcAnd
    BitBlt backDC, Int((Me.ScaleWidth - picInfo.bmWidth) / 2), Int((Me.ScaleHeight - picInfo.bmHeight) / 2), picInfo.bmWidth, picInfo.bmHeight, picDC, 0, 0, vbSrcPaint
    Form_Paint

    'Cleanup memory
    DeleteObject backBitmap
    DeleteObject backBrush
End Sub

Private Sub Form_Unload(Cancel As Integer)
    'Cleanup memory
    DeleteDC picDC
    DeleteDC maskDC
    DeleteDC backDC
End Sub



DOGGIE2.gif
 Description:
 Filesize:  24.43 KB
 Viewed:  198 Time(s)

DOGGIE2.gif


bellopapo




PostPosted: Mon Mar 09, 2009 5:22 am   Post subject: Re: How do I colour key in gdi32?

hi
i tried the example ...
it's ok but image has a lot of "gosts", that are the old images when doggie2.gif go in another location.
so .. is it possible to display on screen ONLY ONE doggie2.gif after it is moved ?
many thanks

peppe
Brightguy




PostPosted: Wed Mar 11, 2009 10:33 pm   Post subject: Re: How do I colour key in gdi32?

Sure, you just want to clear the background before each new doggie is drawn. Add something like...
VisualBASIC:
        BitBlt backDC, 0, 0, Me.ScaleWidth, Me.ScaleHeight, 0, 0, 0, &H42
        ExtFloodFill backDC, 0, 0, vbBlack, 1

...right before "Draw picture on background" in Form_MouseMove.

And I just realized ExtFloodFill is really slow (that's "onboard graphics accelerator" for you). If you don't care about the background colour you can remove that.
Display posts from previous:   
   Index -> Programming, Visual Basic and Other Basics -> Visual Basic Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 7 Posts ]
Jump to:   


Style:  
Search: