Create a region window from a bitmap

Source: Internet
Author: User
Tags transparent color

Requirements:Special window may be used when creating some special interfaces.
Solution:Create a bitmap and specify a transparent color in the in-place image. The transparent color is mostly used as the background, that is, the part to be removed in the window, that is to say, extracting a region window from a bitmap is a little more convenient. The principle is to read each pixel in the bitmap and compare it with the specified transparent color, if the color is the same, it will be excluded; otherwise it will be merged.

 

//////////////////////////////////////// ///////////////// /
//
// Funcution: createbitmaprgn
//
// Description:
// Create Region Window from block of Bitmap specifies
//
// Parameters:
// Nxdest X-coordinate of upper-left corner
// Nydest y-coordinate of upper-left corner
// Nwidth X-coordinate of lower-right corner
// Nheight y-coordinate of lower-right corner
// Nxsrc X-coordinate of start position
// Nysrc y-coordinate of start posttion
// Clrtrans specify the transparent color
//
// 2007-11-05 Lijun created
//
//////////////////////////////////////// ///////////////// /
Hrgn createbitmaprgn (
Hbitmap,
Int Nxdest,
Int Nydest,
Int Nwidth,
Int Nheight,
Int Nxsrc,
Int Nysrc,
Colorref clrtrans
)
{
If (Hbitmap = Null)
Return NULL;

HDC hmemdc;
Hrgn, hpix;
Hbitmap oldbitmap;

Hmemdc=Createcompatibledc (null );
Holdbitmap=SelectObject (hmemdc, hbitmap );
Hrgn=Createrectrgn (nxdest, nydest, nxdest+Nwidth, nydest+Nheight );

For ( Int I = Nxdest, X = Nxsrc; I < Nxdest + Nwidth; I ++ , X ++ )
{
For ( Int J = Nydest, y = Nysrc; j < Nydest + Nheight; j ++ , Y ++ )
{
If (Getpixel (hmemdc, x, y) = Clrtrans)
{
Hpix = Createrectrgn (I, j, I + 1 , J + 1 );
Combinergn (hrgn, hrgn, hpix, rgn_xor );
Deleteobject (hpix );
}
}
}

SelectObject (hmemdc, holdbitmap );
Deletedc (hmemdc );

ReturnHrgn;
}

 

Convert a piece of bitmap data to a DIB bitmap handle

Requirements:Sometimes a bitmap data to be displayed does not store a project resource or an independent file. It may be stored in a database or a structure storage object, therefore, you must convert the read data into a DIB bitmap handle.
Solution:Windows provides an APICreatedibitmapFunction, which can easily convert a DDB data into a Dib handle (for example, how to perform the conversion, the following example reads DDB data from a file and then converts it using the createdibitmap function)

 

Hbitmap loadbitmapex (lpctstr lpszfile)
... {
If (Lpszfile = Null)
Return NULL;
Hbitmap;
Handle Hf;
Bitmapfileheader * Pbmfh;
DWORD dwbytesread, dwfilesize, dwfilesizehigh;
Bool bsuccess;
// Open a BMP file
HF = Createfile (lpszfile, generic_read, file_pai_read,
Null, open_existing, file_flag_sequential_scan, null );
If (HF = Invalid_handle_value)
... {
Trace ("Open File filed with error % d", Getlasterror ());
ReturnNULL;
}

// Get the file size
Dwfilesize = Getfilesize (HF, & Dwfilesizehigh );
If (Dwfilesizehigh)
... {
Closehandle (HF );
ReturnNULL;
}

// Memory allocation. The size is the size of the file.
Pbmfh = (Bitmapfileheader * ) Malloc (dwfilesize );
If ( ! Pbmfh)
... {
Closehandle (HF );
ReturnNULL;
}

// Read data
Bsuccess = Readfile (HF, pbmfh, dwfilesize, & Dwbytesread, null );
Closehandle (HF );

// Verify the file size and format
If ( ! Bsuccess | Dwfilesize ! = Dwbytesread
| Pbmfh -> Bftype ! =   0x4d42   | Pbmfh -> Bfsize ! = Dwfilesize)
... {
Free ((Void*) Pbmfh );
ReturnNULL;
}

// DiB Conversion
Hbitmap = Createdibitmap (
Getwindowdc (null ),
(Bitmapinfoheader * ) (Pbmfh +   1 ),
Cbm_init,
(Byte * ) Pbmfh + Pbmfh -> Bfoffbits,
(Bitmapinfo * ) (Pbmfh +   1 ),
Dib_rgb_colors );
Free (( Void * ) Pbmfh );

Return Hbitmap;
}

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.