How to create a transparent image with VB6

Source: Internet
Author: User
Tags bitwise integer transparent image
Create | Transparent through the front image to see the background image, called the front of the image as a transparent image, we have seen many programs and television programs have used a transparent image, and we will certainly for the transparency of the image and marvel. How are transparent images made? Here we will explore the method of making this transparent image.
Five necessary steps to create a transparent image:

Prepare two bitmap files, one for the background, and one for the source bitmap that will be the transparent image.

1, get the source bitmap of the long, wide data, according to this data to save a piece and the source bitmap size of the background bitmap, the source bitmap will be drawn on this background bitmap. By displaying the transparent area of the bitmap in white pixels, the black pixel displays the opaque area of the bitmap, creating a monochrome mask that determines the transparency of the bitmap.

2, monochrome mask pixel and the background bitmap used for binary "and" (and) bitwise operation, opaque area, background display black.

3, with the first step of the monochrome mask to create a reverse copy, and then use the reverse copy and the source bitmap used for binary "and" (and) bitwise operation, the source bitmap transparent area will display black

4, with the second step of the modified background and the third step to modify the source bitmap binary "XOR" (exclusive) bit operation, you can see the background through transparent bitmap.

5. Copy the result bitmap to the background

application Example:

Create a form Form1 that contains one CommandButton control and two PictureBox controls. Create a module (click Add Module on the Project menu).

Add the following controls to the form to set the associated property values:

Control Name Property Settings
-----------------------------------------------------------------
PictureBox Pictsource picture = "C:\Flower_Vine.bmp"
PictureBox pictdest picture = "C:\Stones_Blue.bmp"
Command button Command1 Caption = "Transparent image"

----Paste the following code into the Declarations section of the form,

----Option Explicit ' This code calls the procedure transparent () copies the source bitmap to the target (background) PictureBox control, ' and makes it transparent so that people can see the background image behind.

Sub Command1_Click ()
Call Transparent (PictSource.Picture.Handle, Pictdest,
QBColor (15))
End Sub

----Paste the following code into the declaration section of the module,

Option Explicit

----' because you want to read the basic information of a bitmap, you first define a variable for the bitmap structure, and then

----' Use this variable to accept the basic information of a bitmap.

'
Type Bitmap
Type as Long ' bitmap types
Width as Long ' breadth
Height as Long ' altitude
Widthbytes as Long ' how many bits constitute a storage unit
Planes as Integer ' palette number
Bitspixel as Integer ' the number of bits occupied by each pixel
Bits as Long ' bits the starting position of the data
End Type

' API function description
Declare Function GetObject Lib "GDI32"
Alias "GetObjectA" (ByVal Hobject as _
Long, ByVal ncount as Long, lpobject as any) as long
' API function to obtain object data structure via handle of object

Declare Function CreateCompatibleDC Lib "GDI32"
(ByVal hdc as Long) As Long '
This function paints the image into memory to avoid straight
' Connect the image to the screen and cause the image to flicker
Declare Function createbitmap Lib "GDI32"
(ByVal nwidth as Long, _
ByVal nheight as Long, ByVal nplanes
As Long, _
ByVal Nbitcount as Long, lpbits as any)
As Long ' creates a bitmap object
Declare Function CreateCompatibleBitmap Lib
"GDI32" (ByVal hdc as Long, _
ByVal nwidth as Long, ByVal nheight as Long)
As Long ' to create a compatible bitmap
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 Hsourcedc as Long,
ByVal Xsrc as Long, _
ByVal ysrc as Long, ByVal Dwrop as Long) as long ' image transfer
Declare Function setbkcolor Lib "GDI32" (ByVal hdc as Long,
ByVal Crcolor as Long) as long
Declare Function deletedc Lib "GDI32" (ByVal hdc as Long)
As Long ' Remove memory DC
Declare Function selectobject Lib "GDI32" (ByVal hdc as Long,
ByVal Hobject as Long) as long ' for DC Selection object
Declare Function deleteobject Lib "gdi32" (ByVal hobject as
Long) as Long ' deletes a bitmap object

----process Transparent () copies the source bitmap to any x,y position in the background, making the area transparent. Transparent () accepts five parameters: a source bitmap that will become transparent, a target PictureBox control (Pictdest), an RGB color value, and another two are the destination coordinates (DESTX and desty, in pixels) where you want to place the map.

Sub transparent (Byval sourcebmp as long, dest as control, byval  _
Destx as integer, byval desty as integer, byval transcolor  as long)
const pixel = 3
dim sourcedc as long  ' source bitmap
Dim  destscale as long
dim maskdc as long  ' mask bitmap   (monochrome)
Dim  saveDC As Long  ' Backup of source bitmap
dim resultdc as long  ' merging of source bitmap and background
Dim  invDC As Long  ' reverse image of mask bitmap
dim origcolor as long  ' background color
dim  success as long  ' calls the result of  windows api

Data structure description for Dim bmp as Bitmap ' in-situ graphs
Dim hresultbmp as Long ' source and background bitmap merge
Dim hsavebmp as Long ' copy of In-situ graphs
Dim Hsrcprevbmp as Long
Dim Hdestprevbmp as Long
Dim hinvbmp as Long ' reverse mask bitmap (monochrome)
Dim Hprevbmp as Long
Dim Hinvprevbmp as Long
Dim Hsaveprevbmp as Long
Dim Hmaskbmp as Long
Dim Hmaskprevbmp as Long


Destscale = dest. ScaleMode ' Save ScaleMode for later recovery
Dest. ScaleMode = PIXEL ' Set ScaleMode


Sourcedc = CreateCompatibleDC (DEST.HDC) ' establishes the memory DC
SaveDC = CreateCompatibleDC (DEST.HDC) ' establishes the memory DC

Invdc = createcompatibledc (DEST.HDC)   ' Build storage DC
Maskdc = createcompatibledc ( DEST.HDC)   ' Build memory DC
Resultdc = createcompatibledc (DEST.HDC)   ' Build memory DC
' accept source bitmap to get its width and length   (BMP. Width , bmp. Height)
Success = getobject (Sourcebmp, len (BMP),  bmp)
' Create monochrome mask bitmap
hmaskbmp =  createbitmap (BMP. Width, bmp. height, 1, 1, byval 0&)
Hinvbmp = createbitmap (BMP. Width, bmp. height, 1, 1, byval 0&)

Hresultbmp = CreateCompatibleBitmap (DEST.HDC, BMP. Width, _
Bmp. Height)
Hsavebmp = CreateCompatibleBitmap (DEST.HDC, BMP. Width, _
Bmp. Height)
Hsrcprevbmp = SelectObject (Sourcedc, Sourcebmp)
Hsaveprevbmp = SelectObject (SaveDC, Hsavebmp)
Hmaskprevbmp = SelectObject (MASKDC, Hmaskbmp)
Hinvprevbmp = SelectObject (INVDC, Hinvbmp)
Hdestprevbmp = SelectObject (RESULTDC, hresultbmp) ' Select bitmap
Success = BitBlt (savedc, 0, 0, BMP. Width, BMP. Height, Sourcedc, _
0, 0, vbsrccopy) ' Make a copy of the source bitmap for later recovery

Origcolor = SetBkColor (Sourcedc, Transcolor)
Success = BitBlt (maskdc, 0, 0, BMP. Width, BMP. Height, Sourcedc, _
0, 0, vbsrccopy)
Transcolor = SetBkColor (Sourcedc, Origcolor)

Success = BitBlt (invdc, 0, 0, BMP. Width, BMP. Height, MASKDC, _
0, 0, vbnotsrccopy)
' Copy the background image and create the final transparent bitmap
Success = BitBlt (resultdc, 0, 0, BMP. Width, BMP. Height, _
DEST.HDC, Destx, Desty, vbsrccopy)

Success = BitBlt (resultdc, 0, 0, BMP. Width, BMP. Height, _
MASKDC, 0, 0, Vbsrcand)
Success = BitBlt (sourcedc, 0, 0, BMP. Width, BMP. Height, INVDC, _
0, 0, Vbsrcand)

Success = BitBlt (resultdc, 0, 0, BMP. Width, BMP. Height, _
Sourcedc, 0, 0, Vbsrcinvert)

Success = BitBlt (Dest.hdc, DESTX, Desty, BMP. Width, BMP. Height, _
RESULTDC, 0, 0, vbsrccopy) ' show transparent bitmap on background

Success = BitBlt (sourcedc, 0, 0, BMP. Width, BMP. Height, SaveDC, _
0, 0, vbsrccopy) ' Restore bitmap
' Select object to release
Hprevbmp = SelectObject (RESULTDC, Hdestprevbmp)
Hprevbmp = SelectObject (Sourcedc, Hsrcprevbmp)
Hprevbmp = SelectObject (SaveDC, Hsaveprevbmp)
Hprevbmp = SelectObject (INVDC, Hinvprevbmp)
Hprevbmp = SelectObject (MASKDC, Hmaskprevbmp)
' Releasing resources
Success = DeleteDC (SAVEDC)
Success = DeleteDC (INVDC)
Success = DeleteDC (RESULTDC)
Success = DeleteObject (hsavebmp)
Success = DeleteObject (hmaskbmp)
Success = DeleteObject (hinvbmp)
Success = DeleteDC (SOURCEDC)
Success = DeleteDC (MASKDC)

Success = DeleteObject (hresultbmp)
Dest. ScaleMode = Destscale ' Restore ScaleMode
End Sub



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.