Articles   Members Online: 3
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to draw a transparent bitmap Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
Drawing Transparent Bitmaps 13-Oct-04
Category
Graphic
Language
Delphi All Versions
Views
380
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
Perevoznyk, Serhiy
Reference URL:
			A portion of a graphic image that does not change the contents of the screen is 
termed "transparent." 
The DrawIcon function can create an image that contains transparent portions. 
It is also possible to obtain this functionality using the BitBlt function; 
however, there are some additional steps involved. 
The first step is to obtain the contents of the area where the bitmap will be drawn 
and to store this background image in a memory display context (DC). Mask out the 
area of the background that corresponds to the nontransparent portion of the image 
bitmap and mask out all transparent pixels from the image bitmap. Use the XOR 
raster operation to merge the image bitmap into the background bitmap. Finally, use 
the BitBlt function to move the merged image to the destination DC. 


1   procedure DrawTransparentBitmap(DC: HDC; hBmp : HBITMAP ;
2             xStart: integer; yStart : integer; cTransparentColor : COLORREF);
3   var
4         bm:                                                  BITMAP;
5         cColor:                                              COLORREF;
6         bmAndBack, bmAndObject, bmAndMem, bmSave:            HBITMAP;
7         bmBackOld, bmObjectOld, bmMemOld, bmSaveOld:         HBITMAP;
8         hdcMem, hdcBack, hdcObject, hdcTemp, hdcSave:        HDC;
9         ptSize:                                              TPOINT;
10  
11  begin
12     hdcTemp := CreateCompatibleDC(dc);
13     SelectObject(hdcTemp, hBmp);   // Select the bitmap
14  
15     GetObject(hBmp, sizeof(BITMAP), @bm);
16     ptSize.x := bm.bmWidth;            // Get width of bitmap
17     ptSize.y := bm.bmHeight;           // Get height of bitmap
18     DPtoLP(hdcTemp, ptSize, 1);        // Convert from device
19                                        // to logical points
20  
21     // Create some DCs to hold temporary data.
22     hdcBack   := CreateCompatibleDC(dc);
23     hdcObject := CreateCompatibleDC(dc);
24     hdcMem    := CreateCompatibleDC(dc);
25     hdcSave   := CreateCompatibleDC(dc);
26  
27     // Create a bitmap for each DC. DCs are required for a number of
28     // GDI functions.
29  
30     // Monochrome DC
31     bmAndBack   := CreateBitmap(ptSize.x, ptSize.y, 1, 1, nil);
32  
33     // Monochrome DC
34     bmAndObject := CreateBitmap(ptSize.x, ptSize.y, 1, 1, nil);
35  
36     bmAndMem    := CreateCompatibleBitmap(dc, ptSize.x, ptSize.y);
37     bmSave      := CreateCompatibleBitmap(dc, ptSize.x, ptSize.y);
38  
39     // Each DC must select a bitmap object to store pixel data.
40     bmBackOld   := SelectObject(hdcBack, bmAndBack);
41     bmObjectOld := SelectObject(hdcObject, bmAndObject);
42     bmMemOld    := SelectObject(hdcMem, bmAndMem);
43     bmSaveOld   := SelectObject(hdcSave, bmSave);
44  
45     // Set proper mapping mode.
46     SetMapMode(hdcTemp, GetMapMode(dc));
47  
48     // Save the bitmap sent here, because it will be overwritten.
49     BitBlt(hdcSave, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0, SRCCOPY);
50  
51     // Set the background color of the source DC to the color.
52     // contained in the parts of the bitmap that should be transparent
53     cColor := SetBkColor(hdcTemp, cTransparentColor);
54  
55     // Create the object mask for the bitmap by performing a BitBlt
56     // from the source bitmap to a monochrome bitmap.
57     BitBlt(hdcObject, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0,
58            SRCCOPY);
59  
60     // Set the background color of the source DC back to the original
61     // color.
62     SetBkColor(hdcTemp, cColor);
63  
64     // Create the inverse of the object mask.
65     BitBlt(hdcBack, 0, 0, ptSize.x, ptSize.y, hdcObject, 0, 0,
66            NOTSRCCOPY);
67  
68     // Copy the background of the main DC to the destination.
69     BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, dc, xStart, yStart,
70            SRCCOPY);
71  
72     // Mask out the places where the bitmap will be placed.
73     BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdcObject, 0, 0, SRCAND);
74  
75     // Mask out the transparent colored pixels on the bitmap.
76     BitBlt(hdcTemp, 0, 0, ptSize.x, ptSize.y, hdcBack, 0, 0, SRCAND);
77  
78     // XOR the bitmap with the background on the destination DC.
79     BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0, SRCPAINT);
80  
81     // Copy the destination to the screen.
82     BitBlt(dc, xStart, yStart, ptSize.x, ptSize.y, hdcMem, 0, 0,
83            SRCCOPY);
84  
85     // Place the original bitmap back into the bitmap sent here.
86     BitBlt(hdcTemp, 0, 0, ptSize.x, ptSize.y, hdcSave, 0, 0, SRCCOPY);
87  
88     // Delete the memory bitmaps.
89     DeleteObject(SelectObject(hdcBack, bmBackOld));
90     DeleteObject(SelectObject(hdcObject, bmObjectOld));
91     DeleteObject(SelectObject(hdcMem, bmMemOld));
92     DeleteObject(SelectObject(hdcSave, bmSaveOld));
93  
94     // Delete the memory DCs.
95     DeleteDC(hdcMem);
96     DeleteDC(hdcBack);
97     DeleteDC(hdcObject);
98     DeleteDC(hdcSave);
99     DeleteDC(hdcTemp);
100 end;


The following is an example of how the DrawTransparentBitmap function might be 
called: 

101 procedure TForm1.Button1Click(Sender: TObject);
102 begin
103   DrawTransparentBitmap(Form1.Canvas.Handle, Image1.Picture.Bitmap.Handle, 10, 10, 
104 clWhite);
105 end;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC