How to create a monocrome bitmap with win32api

Hello all,

I’m trying to convert a visual basic algorithm that copy a bitmap into
another performing a transparency effect. The transparence is the fined
by a chosen color. In orther to do that, it’s necessary to create a mask
to be used during some win32api bitblt operatios. That mask is a
mocromatic bitmap which has just 1 bit per pixel. Now comes, the
problem, how to create in ruby this kind of bitmap with the win32api
function CreateBitmap? That function prototype is:

Public Declare Function CreateBitmap Lib “gdi32” Alias “CreateBitmap”
(ByVal nWidth As Long, ByVal nHeight As Long, ByVal nPlanes As Long,
ByVal nBitCount As Long, lpBits As Any) As Long

And when it’s used in visual basic to generate the monocromatic bitmap,
it is called like that:

hMaskBmp = CreateBitmap(Width, Height, 1, 1, ByVal 0&)

The problem is the last value: Byval 0&. This is a “special” convention
by vb for DLL function calls to pass an empty value for “pointer”
values. The & in the end indicates that the data type of the 0 should be
“long” and not integer or byte.

How to do the same thing in ruby? I tried several things but none
worked. Could someone help me with that? The code that I last tried is
at follow.

class Bitmap

RtlMoveMemory_pi =‘kernel32’, ‘RtlMoveMemory’, ‘pii’, ‘i’)
FindWindow =‘user32’, ‘FindWindow’, ‘pp’, ‘i’)
GetDC =‘user32’, ‘GetDC’, ‘i’, ‘i’)
ReleaseDC =‘user32’, ‘ReleaseDC’, ‘ii’, ‘i’)
BitBlt =‘gdi32’, ‘BitBlt’, ‘iiiiiiiii’, ‘i’)
CreateCompatibleBitmap =‘gdi32’,
‘CreateCompatibleBitmap’, ‘iii’, ‘i’)
CreateCompatibleDC =‘gdi32’, ‘CreateCompatibleDC’, ‘i’,
DeleteObject =‘gdi32’, ‘DeleteObject’, ‘i’, ‘i’)
GetDIBits =‘gdi32’, ‘GetDIBits’, ‘iiiiipi’, ‘i’)
SelectObject =‘gdi32’, ‘SelectObject’, ‘ii’, ‘i’)
SetDIBits =‘gdi32’, ‘SetDIBits’, ‘iiiiipi’, ‘i’)
TransparentBlt =‘msimg32’, ‘TransparentBlt’,
SetBkColor =‘gdi32’, ‘SetBkColor’, ‘ii’, ‘i’)
CreateBitmap =‘gdi32’, ‘CreateBitmap’, ‘iiiip’, ‘i’)
DeleteDC =‘gdi32’, ‘DeleteDC’, ‘i’, ‘i’)

def address
unless @address"\0"*4, id*2+16, 4), a.unpack(‘L’)[0]+8, 4), a.unpack(‘L’)[0]+16, 4)
@address = a.unpack(‘L’)[0]
return @address

def transpblt2(x, y, src_bmp, src_rect, color_obj)
hDC1, hBM1, info = *self.BltApiInit
hDC2, hBM2 = *src_bmp.BltApiInit
dsthDC = hDC1
srchDC = hDC2

crTransparent = +*256 +*65536

#First create some DC's. These are our gateways to assosiated

bitmaps in RAM
maskDC = #DC for the mask
tempDC = #DC for temproary data

#Then we need the bitmaps. Note that we create a monochrome bitmap

#this is a trick we use for creating a mask fast enough:

#Bitmap for mask
info2 = [40,width,height,1,1,0,0,0,0,0,0].pack('LllSSLLllLL')
hMaskBmp =, src_rect.height, 1, 1,

# Bitmap for temporary data
hTempBmp =, src_rect.width,

#..then we can assign the bitmaps to the DCs
hMaskBmp =, hMaskBmp)
hTempBmp =, hTempBmp)

#Now we can create a mask..First we set the background color to the
#transparent color then we copy the image into the monochrome

#When we are done, we reset the background color of the original
crTransparent =, crTransparent) maskDC, 0, 0, src_rect.width, src_rect.height, srchDC,
src_rect.x, src_rect.y, 0xCC0020)
crTransparent =, crTransparent)

#'The first we do with the mask is to MergePaint it into the

#this will punch a WHITE hole in the background exactly were we want
#graphics to be painted in. tempDC, 0, 0, src_rect.width, src_rect.height, maskDC,
0, 0, 0xCC0020) dsthDC, x, y, src_rect.width, src_rect.height, tempDC,
0, 0, 0xBB0226) #merpaint

#Now we delete the transparent part of our source image. To do this
#we must invert the mask and MergePaint it into the source image.

#transparent area will now appear as WHITE. maskDC, 0, 0, src_rect.width, src_rect.height, maskDC,
0, 0, 0x330008) #vbNotSrcCopy tempDC, 0, 0, src_rect.width, src_rect.height, srchDC,
0, 0, 0xCC0020)#vbSrcCopy tempDC, 0, 0, src_rect.width, src_rect.height, maskDC,
0, 0, 0xBB0226)#vbMergePaint

#Both target and source are clean, all we have to do is to AND them

together!, x, y, src_rect.width, src_rect.height, tempDC,
0, 0, 0x8800C6)#vbSrcAnd

#Now all we have to do is to clean up after us and free system

#-------------------------------------------------------------------------------, hBM1, 0, height, address, info, 0);;

def BltApiInit
info = [40,width,height,1,32,0,0,0,0,0,0].pack(‘LllSSLLllLL’)
hWndDC =‘RGSS Player’, 0))
hMemDC =
hMemBM =, width, height), hWndDC), hMemBM), hMemBM, 0, height, address, info, 0)
return hMemDC, hMemBM, info

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs