User Tools

Site Tools


drawing_20a_20transparent_20bitmap

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
drawing_20a_20transparent_20bitmap [2018/03/31 13:19] – external edit 127.0.0.1drawing_20a_20transparent_20bitmap [2024/01/05 00:22] (current) – external edit 127.0.0.1
Line 8: Line 8:
   * Create the image as a 32-bits-per-pixel ARGB bitmap, in which each pixel consists of a 24-bit **RGB** value (8 bits red, 8 bits green, 8 bits blue) plus an 8-bit **alpha** value. The alpha value defines a 'per-pixel' opacity which can be anything from 0 (fully transparent) to 255 (fully opaque). You can display such a bitmap using the **AlphaBlend** API, by using the **GDIplus** or **Direct3D** subsystems, or by means of the [[/Libraries|GFXLIB library]].   * Create the image as a 32-bits-per-pixel ARGB bitmap, in which each pixel consists of a 24-bit **RGB** value (8 bits red, 8 bits green, 8 bits blue) plus an 8-bit **alpha** value. The alpha value defines a 'per-pixel' opacity which can be anything from 0 (fully transparent) to 255 (fully opaque). You can display such a bitmap using the **AlphaBlend** API, by using the **GDIplus** or **Direct3D** subsystems, or by means of the [[/Libraries|GFXLIB library]].
   * Create the image as a **Transparent GIF** or **PNG** and display it using, for example, the code listed [[/Displaying%20animated%20GIFs|here]].   * Create the image as a **Transparent GIF** or **PNG** and display it using, for example, the code listed [[/Displaying%20animated%20GIFs|here]].
-\\ //by Michael Hutton, July 2009//\\ \\  How do you make a part of a bitmap transparent? One way is to use the "TransparentBlt" function supplied in MSIMG32.DLL.\\ \\  A complete example is [[/Drawing%20a%20Transparent%20Bitmap#example|here]].\\ \\  And an example bitmap you can use is here.\\ [[/file/view/star.bmp/80304965/star.bmp|{{star.bmp}}]][[/file/view/star.bmp/80304965/star.bmp|star.bmp]]\\ +\\ //by Michael Hutton, July 2009//\\ \\  How do you make a part of a bitmap transparent? One way is to use the "TransparentBlt" function supplied in MSIMG32.DLL.
  
-  * [[/file/detail/star.bmp|Details]] +First select a MODE and define some constants: 
-  * [[/file/view/star.bmp/80304965/star.bmp|Download]] + 
-  * 7 KB +<code bb4w>
-\\  +
-==== Explanation: ==== +
- First select a MODE and define some constants:\\ +
         MODE 8         MODE 8
  
Line 26: Line 23:
         cx% = 64 : REM width         cx% = 64 : REM width
         cy% = 64 : REM height         cy% = 64 : REM height
-Next, we have to load the MSIMG32.DLL into memory and get the address of the "TransparentBlt" function.\\ +</code> 
 + 
 +Next, we have to load the MSIMG32.DLL into memory and get the address of the "TransparentBlt" function. 
 + 
 +<code bb4w>
         SYS "LoadLibrary", "MSIMG32.DLL" TO msimg32%         SYS "LoadLibrary", "MSIMG32.DLL" TO msimg32%
         SYS "GetProcAddress", msimg32%, "TransparentBlt" TO `TransparentBlt`         SYS "GetProcAddress", msimg32%, "TransparentBlt" TO `TransparentBlt`
-Next, load the image we want (we will assume it's in the same directory and is called star.bmp).\\ +</code> 
 + 
 +Next, load the image we want (we will assume it's in the same directory and is called star.bmp). 
 + 
 +<code bb4w>
         bmpfile$ = @dir$ + "star.bmp"         bmpfile$ = @dir$ + "star.bmp"
         SYS "LoadImage", 0, bmpfile$, IMAGE_BITMAP, cx%, cy%, LR_LOADFROMFILE TO hbitmap%         SYS "LoadImage", 0, bmpfile$, IMAGE_BITMAP, cx%, cy%, LR_LOADFROMFILE TO hbitmap%
         IF hbitmap% = 0 THEN ERROR 100,"Cannot Load Image."         IF hbitmap% = 0 THEN ERROR 100,"Cannot Load Image."
-Now, because we are using GDI and we only have a handle to the bitmap we have to create a new Device Context and select our bitmap into it. (A bitmap can only be selected into one DC at a time).\\ +</code> 
 + 
 +Now, because we are using GDI and we only have a handle to the bitmap we have to create a new Device Context and select our bitmap into it. (A bitmap can only be selected into one DC at a time). 
 + 
 +<code bb4w>
         SYS "CreateCompatibleDC", @memhdc% TO hdc%         SYS "CreateCompatibleDC", @memhdc% TO hdc%
         SYS "SelectObject", hdc%, hbitmap% TO old%         SYS "SelectObject", hdc%, hbitmap% TO old%
-\\  Now, we can ask GDI to transfer that bitmap into our main window. When using "TransparentBlt" we can select an RGB (COLOURREF) value to make transparent when it is drawing. It is the last parameter in "TranparentBlt". Here I have selected 0 which is all the black bits (Red = 0, Green = 0, Blue = 0) but you can make it any colour you want. We also update a RECT{} so that we only update the region of the screen which has changed. It is much quicker to do this than update the whole window.\\ +</code> 
 + 
 +Now, we can ask GDI to transfer that bitmap into our main window. When using "TransparentBlt" we can select an RGB (COLOURREF) value to make transparent when it is drawing. It is the last parameter in "TranparentBlt". Here I have selected 0 which is all the black bits (Red = 0, Green = 0, Blue = 0) but you can make it any colour you want. We also update a RECT{} so that we only update the region of the screen which has changed. It is much quicker to do this than update the whole window. 
 + 
 +<code bb4w>
         FOR I% = 0 TO 50         FOR I% = 0 TO 50
           xpos% = RND(640)           xpos% = RND(640)
Line 48: Line 61:
           WAIT 1           WAIT 1
         NEXT         NEXT
-\\  Now we must remember to cleanup all the GDI objects we have created otherwise we will end up with a leak.\\ +</code> 
 + 
 +Now we must remember to cleanup all the GDI objects we have created otherwise we will end up with a leak. 
 + 
 +<code bb4w>
         SYS "SelectObject", hdc%, old%         SYS "SelectObject", hdc%, old%
         SYS "DeleteDC", hdc%         SYS "DeleteDC", hdc%
Line 54: Line 71:
         SYS "FreeLibrary", msimg32%         SYS "FreeLibrary", msimg32%
         END         END
-\\  The example  to copy and paste:\\ +</code> 
 + 
 +The example  to copy and paste: 
 + 
 +<code bb4w>
         MODE 8         MODE 8
  
Line 95: Line 116:
  
         END         END
-\\  And there we are! Enjoy.\\ \\  Michael Hutton+</code> 
 + 
 +And there we are! Enjoy.\\ \\  Michael Hutton
drawing_20a_20transparent_20bitmap.1522502357.txt.gz · Last modified: 2024/01/05 00:18 (external edit)