PDA

Click to See Complete Forum and Search --> : CreateMaskImage


CiberTHuG
Feb 22nd, 2001, 03:08 PM
I've recently been assigned to a project using exclusively VB and ASP. Amoung my tasks, I'm to help certify this program for use under W2K.

I apologize for not being the worlds biggest VB fan, but I have a problem, and thought maybe someone could point me in the right direction.

They are using CreateMaskImage to create a mask of a bmp to overlay on a colored picture box. CreateMaskImage was gleened from a forum like this aparently, and is simply making calls into GDI32 to create the mask. Once the program has the mask, it uses a couple of PictureBox.PaintPicture steps to fit the bmp onto the colored box.

This works just fine under NT. It doesn't work under W2K.

I am looking for the reason, and a work around. The work around is more important, since I can sleep at night with out knowing why. But I want to know what has changed between the two OSes to cause this. Is there something different in GDI32, or in the way W2K handles Blt's?

We would appreciate any guidance and reference you can offer.

Feb 22nd, 2001, 05:44 PM
Here is a samll mask generating code I just wrote up. It should work in Window NT as well as 95/98/ME

Private Declare Function SetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal crColor As Long) As Long
Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long

Private Sub Command1_Click()

For x = 0 To picture1.Width / Screen.TwipsPerPixelX
For y = 0 To picture1.Width / Screen.TwipsPerPixelY
cr = GetPixel(picture1.hdc, x, y)
If cr <> vbWhite Then cr = vbBlack
SetPixel picture1.hdc, x, y, cr
Next y
Next x

End Sub

CiberTHuG
Feb 23rd, 2001, 07:45 AM
Thanks Megatron,

But it doesn't work. It doesn't even pretend to work, I get the same results with CreateMaskImage. This whole thing works on NT, but not on W2K.

Just incase anyone cares, here is CreateMaskImage. It is old code that you can still find posted on the web by doing a simple search on it.


' Creates a memory DC
Private Declare Function CreateCompatibleDC Lib "gdi32" ( _
ByVal hDC As Long _
) As Long
' Creates a bitmap in memory:
Private Declare Function CreateCompatibleBitmap Lib "gdi32" ( _
ByVal hDC As Long, _
ByVal nWidth As Long, ByVal nHeight As Long _
) As Long
' Places a GDI Object into DC, returning the previous one:
Private Declare Function SelectObject Lib "gdi32" _
(ByVal hDC As Long, ByVal hObject As Long _
) As Long
' Deletes a GDI Object:
Private Declare Function DeleteObject Lib "gdi32" _
(ByVal hObject As Long _
) As Long
' Copies Bitmaps from one DC to another, can also perform
' raster operations during the transfer:
Public 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

' Sets the backcolour of a device context:
Private Declare Function SetBkColor Lib "gdi32" (ByVal hDC As Long, ByVal crColor As Long) As Long

Public Function CreateMaskImage( _
ByRef picFrom As PictureBox, _
ByRef picTo As PictureBox, _
Optional ByVal lTransparentColor As Long = -1 _
) As Boolean

Dim lhDC As Long
Dim lhBmp As Long
Dim lhBmpOld As Long


' Make picTo the same size as picFrom and clear it:
With picTo
.Width = picFrom.Width
.Height = picFrom.Height
.Cls
End With

' Create a monochrome DC & Bitmap of the
' same size as the source picture:
lhDC = CreateCompatibleDC(0)
If (lhDC <> 0) Then
lhBmp = CreateCompatibleBitmap(lhDC, picFrom.ScaleWidth \ Screen.TwipsPerPixelX, picFrom.ScaleHeight \ Screen.TwipsPerPixelY)
If (lhBmp <> 0) Then
lhBmpOld = SelectObject(lhDC, lhBmp)

' Set the back 'colour' of the monochrome
' DC to the colour we wish to be transparent:
If (lTransparentColor = -1) Then lTransparentColor = picFrom.BackColor
SetBkColor lhDC, lTransparentColor

' Copy from the from picture to the monochrome DC
' to create the mask:
BitBlt lhDC, 0, 0, picFrom.ScaleWidth \ Screen.TwipsPerPixelX, picFrom.ScaleHeight \ Screen.TwipsPerPixelY, picFrom.hDC, 0, 0, SRCCOPY

' Now put the mask into picTo:
BitBlt picTo.hDC, 0, 0, picFrom.ScaleWidth \ Screen.TwipsPerPixelX, picFrom.ScaleHeight \ Screen.TwipsPerPixelY, lhDC, 0, 0, SRCCOPY
picTo.Refresh

' Clear up the bitmap we used to create
' the mask:
SelectObject lhDC, lhBmpOld
DeleteObject lhBmp
End If
' Clear up the monochrome DC:
DeleteObject lhDC
End If


End Function

CiberTHuG
Feb 27th, 2001, 09:11 AM
I figured you guys would get a kick out of this...

The project was built on an ThinkPad 570 running NT. When we ported it over to a 600 running Win2K, we ran into a problem with the graphics. It was failing to correctly create the masks, so when it merged the image and the background, all we got was the image.

Finally we fixed it when they let me have to machines sitting side by side. The problem was not in the code, but in the Win2K's depth of color.

The code pulls an image with a gray background onto a VB picture control, and then turns the gray background white and creates a mask of the image. On the Win2K box, the gray in the bmp's background was a different gray than the picture control's background. The change to white was happered, and the mask was created incorrectly.

Instead of setting the picture box's background to "Window's button Gray" we set it to C0C0C0. Now it works.

-Travis