Drawing method with any specified transparent color

Source: Internet
Author: User
Transparent
Transparent bitmap rendering methods on the Internet, most of the use of a mask in advance, this method is fast, but it is too cumbersome, flexibility is poor.
Any specified transparent color, of course often to use, to this end, the API provides a function TransparentBlt, but this function, very regrettable, VB API Browser does not take it makes sense, because it has a serious memory hole in the Win98, if you have 98 system, you can try:
For I=1 to 20000
TransparentBlt .....
Next
The same picture, in my XP 16 milliseconds can be completed, but in 98 under 14 seconds, and, the hint system resources are not enough, when the machine!

Here I write a function is to replace the TransparentBlt one way, of course, the speed will be slower, but in any system can be trusted to use.

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 Declare Function getcurrentobject Lib "GDI32" (ByVal hdc as Long, ByVal Uobjecttype as long) as long
Private Declare Function getobj Lib "gdi32" Alias "GetObjectA" (ByVal hobject as Long, ByVal ncount as Long, lpobject as A NY) as Long
Private Declare Function setbkcolor Lib "GDI32" (ByVal hdc as Long, ByVal crcolor as long) as long

Private Declare Function createbitmap Lib "gdi32" (ByVal nwidth as Long, ByVal nheight as Long, ByVal nplanes as Long, byv Al Nbitcount as Long, lpbits as any) as long
Private Declare Function getpixel Lib "GDI32" (ByVal hdc as Long, ByVal X as Long, ByVal y as Long) as long
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 stretchblt Lib "GDI32" (ByVal hdc 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 nsrcwidth as Long, ByVal NSRC Height as Long, ByVal Dwrop as long) as long

Public Function Tranblt (desthdc as Long, X as long, y as Long, w as Long, h as long, srchdcorbmp as Long, Optional SRCX as Long, Optional Srcy as Long, Optional SRCW as Long, Optional SrcH as Long, Optional TC as Long =-1, Optional isbmp as Bo Olean) as Long
' The Srchdcorbmp parameter can be passed in either HDC or BMP object handle,
The ' isbmp parameter is true when the srchdcorbmp represents the handle of a BMP object, which is a false representation of HDC
' Returns a transparent color when the return value succeeds and returns-1 if unsuccessful
Dim THDC (3) as Memhdc
Dim J as long, OC as long, I as long, Bm as BITMAP, CC as Long, NEWDC as Long
Dim SW As long, SH as long, sbmp as long, shdc as long, obm as long, newx as long, newy as Long
If desthdc = 0 or srchdcorbmp = 0 or W = 1 and h = 1 Then GoTo fail
If Isbmp Then ' If you pass in a BMP handle, create a temporary DC for it
Sbmp = Srchdcorbmp
THDC (3) = NEWMYHDC (desthdc, 0, 0, srchdcorbmp)
SHDC = THDC (3). HDC
Else
SHDC = Srchdcorbmp
If SRCW = 0 Then sbmp = getcurrentobject (SHDC, 7)
End If
If shdc = 0 Or sbmp = 0 Then GoTo fail
If SRCW = 0 Then ' If no source map size, you need to get the entire source map size
Getobj Sbmp, Len (BM), BM
SW = BM.BMWIDTH-SRCX
SH = Bm.bmheight-srcy
Else
SW = SRCW
SH = SrcH
End If
If SW < 1 Or SH < 1 Then GoTo fail
If TC =-1 Then
CC = GetPixel (SHDC, SRCX, Srcy) ' The first pixel in the upper-left corner is used as the background color of the source diagram for Transparent
Else
CC = TC
End If
If W <> SW Or h <> SH Then
THDC (2) = Newmyhdc (Desthdc, W, h)
StretchBlt THDC (2). HDC, 0, 0, W, H, Shdc, SRCX, Srcy, SW, SH, vbsrccopy
' The source diagram is scaled first, and the following steps are the same.
NEWDC = THDC (2). HDC
Else
NEWDC = Shdc
NEWX = SRCX
Newy = Srcy
End If
BitBlt Desthdc, X, Y, W, H, NEWDC, Newx, Newy, Vbsrcinvert
' The source map is first reversed (XOR) into the target graph, if the source diagram background is black, this step can save

' Here's how to make a mask diagram
i = CreateBitmap (W, H, 1, 1, ByVal 0&) ' Create monochrome bitmap
THDC (0) = NEWMYHDC (desthdc, 0, 0, i) ' Creates a new DC for the monochrome diagram and chooses
THDC (1) = Newmyhdc (Desthdc, W, h) ' Create a separate color map and DC for storing mask map
OC = SetBkColor (NEWDC, cc) ' Change the background color of the source diagram to a transparent color
BitBlt thdc (0). HDC, 0, 0, W, H, NEWDC, Newx, Newy, vbsrccopy
' first draw the source graph into a monochrome DC, resulting in only positive and negative mask, background color (transparent color) is black, others are white
SetBkColor NEWDC, OC ' restore source map background color, not necessary, but this is a good habit.
BitBlt THDC (1). HDC, 0, 0, W, H, thdc (0). HDC, 0, 0, vbsrccopy
' Monochrome DC must be copied into the color DC for subsequent and operations
' Mask diagram completed and placed in color DC

BitBlt Desthdc, X, Y, W, H, THDC (1). HDC, 0, 0, Vbsrcand ' Standard transparent drawing: Select to draw the mask diagram with and operations,
BitBlt Desthdc, X, Y, W, H, NEWDC, Newx, Newy, Vbsrcinvert ' then paint the source graph in reverse color (XOR) once

Delmyhdc THDC (0)
DELMYHDC THDC (1)

If THDC (2). HDC <> 0 Then delmyhdc thdc (2)
If THDC (3). HDC <> 0 Then DELMYHDC thdc (3)
Tranblt = CC
Exit Function
Fail
If THDC (3). HDC <> 0 Then DELMYHDC thdc (3)
Tranblt =-1
End Function

Private Function Newmyhdc (dhdc as Long, w as Long, h as long, Optional Bm as Long) as Memhdc
With NEWMYHDC
. HDC = CreateCompatibleDC (DHDC)
If Bm = 0 Then
. BMP = CreateCompatibleBitmap (DHDC, W, h)
Else
. BMP = Bm
End If
. OBM = SelectObject (. HDC,. BMP)
End With
End Function
Private Function Delmyhdc (myhdc as MEMHDC, Optional nobmp as Boolean) as Memhdc
With MYHDC
If. HDC <> 0 and. OBM <> 0 Then SelectObject. HDC,. OBM
If nobmp = False and. BMP <> 0 Then DeleteObject. Bmp
If. HDC <> 0 Then DeleteDC. HDC
End With
End Function

Private Sub Command1_Click ()
Tranblt picture1.hdc, 0, 0, image1.width, Image1.height, Image1.Picture.handle,,,,,, True
End Sub

Private Sub Form_Load ()
Me.scalemode = 3
End Sub

The common functions Newmyhdc, DELMYHDC, and associated structure and API declarations in this article can be found in the following articles
Http://blog.csdn.net/homezj/archive/2005/04/14/348001.aspx


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.