Relationship between IPicture, BITMAP, HBITMAP, and CBitmap

Source: Internet
Author: User

1. Problems about direct Render to memory DC after IPicture loads images (HBITMAP converts IPicture)
The Picture method get_Handle can directly obtain the image handle.

IPicture * pIPicture;
HBITMAP hBitmap;
...
PIPicture-> get_Handle (OLE_HANDLE *) & hBitmap );

2. CBitmap converts HBITMAP
CBitmap m_bitMap;
HBITMAP m_hBitMap;
M_bitMap.LoadBitmap (IDB_BITMAP );
M_hBitMap = (HBITMAP) m_bitMap.GetSafeHandle ();
3. Convert HBITMAP to CBitmap
CBitmap cbMMyBitmap; // The animation bitmap.
HBITMAP hMMyHBitmap; // The handle used to display the animation bitmap.
BITMAP bMMyBitmapInfo; // BITMAP Information
CbMMyBitmap. Attach (hMMyHBitmap );
CbMMyBitmap. GetBitmap (& bMMyBitmapInfo );
4. DrawDibDraw and AlphaBlend
1. Both of these functions draw DIB.
During use, I found that these two functions do not display the 32-Bit Bitmap correctly using the Alpha channel. That is to say, the transparency is not transparent.
The bitmap is correct.

I would like to ask, is it my reason or are these two functions not originally supporting 32-Bit Bitmap?

Ps. 32-bit bitmap display can be implemented using AlphaBlend, but this function is used to draw DDB in the scenario. Because I want to display DIB and don't want to switch in, this function is ignored first.
2. StretchDIBits or DrawDibDraw cannot display images with transparent attributes.
To display images with transparent attributes, you can only use other methods, such as GDI +.
5,
Answer (1)
CBitmap: GetBitmap
Respondent: Soft community netizens
Answer (2)
Each member variable is assigned a value.
Respondent: Soft community netizens
Answer (3)
CBitmap: GetBitmap is to assign the CBitmap information to the BITMAP structure.
What I asked is that BITMAP is assigned to CBitmap.
Respondent: Soft community netizens
Answer (4)
When creating a CBitmap object, use BITMAP to specify information
Respondent: Soft community netizens
Answer (5)
Is it a constructor?
CBitmap: CBitmap (BITMAP bitmap) does not seem to have this Constructor
Respondent: Soft community netizens
Answer (6)
CBitmap: FromHandle (HBITMAP hBitmap)

Answer (7)
HBITMAP CreateBitmapIndirect (
Const bitmap * lpbm // bitmap data
);
6. Use GDI to display bitmap transparently

Below are several key points in implementation:
[Cpp]
//////////////////////////////////////// //////////////////////////////////
//// Read the data of all pixels on rtWant in a specific rectangle of the bitmap BMP file to the buffer pBuf.
Int CChildView: GetPixFromBmp (CString fileName, CRect rtWant, BYTE * pBuf)
{
BITMAPINFOHEADER headInfo;
ZeroMemory (& headInfo, sizeof (headInfo ));
Int imageHeight;
Int imageWidth;
 
CFile file;
File. Open (fileName, CFile: modeRead );
File. Seek (14, CFile: begin );
File. Read (& headInfo, 40); // -- Read BITMAPINFOHEADER information from the object
ImageWidth = headInfo. biWidth;
ImageHeight = headInfo. biHeight;
If (headInfo. biBitCount! = 32)
{
MessageBox (fileName + "not 32-Bit Bitmap ");
}
 
For (int I = 0; I <rtWant. Height (); I ++)
{
File. Seek (54 + (imageHeight-rtWant. bottom + I) * imageWidth * headInfo. biBitCount/8 + rtWant. left * headInfo. biBitCount/8, CFile: begin );
File. Read (pBuf + I * rtWant. Width () * headInfo. biBitCount/8, rtWant. Width () * headInfo. biBitCount/8 );
}
File. Close ();
 
Return 0;
}
 
//////////////////////////////////////// //////////////////////////////////
/// PDesBuf: Target Surface buffer, W, H -- width and height of the target surface
/// PSrcBuf: Source surface Buffer
//// PRetBuf: The result is saved to the buffer, which is compatible with pDesBuf.
//// RtBlend: the rectangular area in which the source and target surface buffers are mixed with alpha.
/// Alpha: the percentage to be transparent, 0 --- completely transparent, 1 -- completely opaque
Void CChildView: MyBlend (BYTE * pDesBuf, BYTE * pSrcBuf, BYTE * pRetBuf, CRect rtBlend, int W, int H, float alpha)
{
Memcpy (pRetBuf, pDesBuf, W * H * 4 );
For (int I = 0; I <rtBlend. Height (); I ++)
{
For (int j = 0; j <rtBlend. Width (); j ++)
{
Long posDesPix = (H-rtBlend.bottom + I) * W + rtBlend. left + j) * 4;
Long posSrcPix = (I * rtBlend. Width () + j) * 4;
PRetBuf [posDesPix + 0] = BYTE (pDesBuf [posDesPix + 0] * (1-alpha) + pSrcBuf [posSrcPix + 0] * (alpha ));
PRetBuf [posDesPix + 1] = BYTE (pDesBuf [posDesPix + 1] * (1-alpha) + pSrcBuf [posSrcPix + 1] * (alpha ));
PRetBuf [posDesPix + 2] = BYTE (pDesBuf [posDesPix + 2] * (1-alpha) + pSrcBuf [posSrcPix + 2] * (alpha ));
PRetBuf [posDesPix + 3] = BYTE (pDesBuf [posDesPix + 3] * (1-alpha) + pSrcBuf [posSrcPix + 3] * (alpha ));
}
}
}
 
{
 
/// -- Sets BITMAPINFOHEADER information of a bitmap.
ZeroMemory (& m_headInfo, sizeof (m_headInfo ));
M_headInfo.biSize = sizeof (m_headInfo );
M_headInfo.biPlanes = 1;
M_headInfo.biBitCount = 32; // 24
M_headInfo.biCompression = BI_RGB;
M_headInfo.biWidth = m_rtShow.Width ();
M_headInfo.biHeight = m_rtShow.Height ();
M_headInfo.biSizeImage = m_nLen;
 
}
 
Void CChildView: OnPaint ()
{
CPaintDC dc (this); // device context for painting
 
StretchDIBits (m_MemDC.GetSafeHdc (), 0, m_rtShow.Width (), m_rtShow.Height (),
0, 0, m_rtShow.Width (), m_rtShow.Height (), (void *) m_pCurrBuf, (BITMAPINFO *) & m_headInfo, NULL, SRCCOPY );
 
Dc. BitBlt (m_rtShow.left, m_rtShow.top, m_rtShow.Width (), m_rtShow.Height (),
& M_MemDC, 0, 0, SRCCOPY );
 
}

 

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.