Click here to Skip to main content
Licence 
First Posted 26 May 2000
Views 110,618
Downloads 2,053
Bookmarked 59 times

CreateRegionFromFile

By Yuriy Zaporozhets | 10 Dec 2001
Very primitive function that creates region from *.bmp files.

1

2

3

4
11 votes, 100.0%
5
4.51/5 - 37 votes
μ 4.51, σa 0.61 [?]

Introduction

This very simple function creates a region from a bitmap (.bmp) file. 8, 16, 24 and 32 bit color modes are supported.

The function takes two parameters:

  • hBmp - a handle to the bitmap image.
  • color - transparent color.
HRGN CreateRgnFromFile( HBITMAP hBmp, COLORREF color );

Using the function is very simple. For example, if you wish to set the window shape of your dialog, then in your OnInitDialog handler, do this:

BOOL CFileRgnDlg::OnInitDialog()
{
    CDialog::OnInitDialog();

    // load image
    HBITMAP hBmp = (HBITMAP)LoadImage( AfxGetInstanceHandle(), 
                     "Rgn.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE );
    // if fail - nothing to do
    if ( hBmp == NULL ) return TRUE;
    // create region, let the RED color be transparent

    HRGN hRgn = CreateRgnFromFile( hBmp, RGB(255,0,0) );

    // build memory dc for background
    CDC* dc = GetDC();
    m_dcBkGrnd = CreateCompatibleDC( dc->m_hDC );
    ReleaseDC( dc );
    // select background image
    SelectObject( m_dcBkGrnd, hBmp );
    // set window size the same as image size
    SetWindowPos( NULL, 0, 0, m_dwWidth, m_dwHeight, 
                           SWP_NOZORDER | SWP_NOMOVE );
    // assign region to window
    SetWindowRgn( hRgn, FALSE );

    return TRUE;
}

History

  • December 08 2001

    * cut & paste bug for 16-bit mode (white color as transparent did not work).

    * pixels in a last column has not been used.

    + Now you can use non-aligned (by width) images.

  • June 11 2001

    * ExtCreateRegion NT-problem (personal thanks to Martin for reporting about it).

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Yuriy Zaporozhets



Canada Canada

Member


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralNot working on Windows Seven PinmemberDom3022:19 6 May '11  
GeneralRe: Not working on Windows Seven PinmemberMember 112345011:01 26 May '11  
GeneralMy vote of 5 Pinmemberryan20fun21:15 9 Feb '11  
GeneralRotated Edit Box PinsussZAKIR4:57 2 Sep '04  
GeneralCreateRgnFromFile question Pinsussrioan11:23 31 Jul '04  
GeneralRe: CreateRgnFromFile question PinmemberYuriy_Zaporozhets14:59 5 Oct '04  
Damn, it was 3 years ago... I can't believe it...
 
LPBITMAPINFO bi = (LPBITMAPINFO) new BYTE[sizeof(BITMAPINFO) + 8];
DIB_RGB_COLORS in the second call of the GetDIBits means that (from MSDN) "The color table should consist of literal red, green, blue (RGB) values.", which means three RGBQUADs (4 bytes each one), in BITMAPINFO we have one, so we need to two more. (better to write 2 * sizeof(RGBQUAD) instead of 8).
 
LPBYTE pBits = new BYTE[bi->bmiHeader.biSizeImage + 4];
Actually D'Oh! | :doh: , here should be 2, not 4. It's required for 16- and 24-bit color values.
Let's say, we are working with 24-bit image and pColor points to the last 3 bytes in the buffer, here (*(LPDWORD)pColor & 0x00FFFFFF) we access one byte out of buffer, and cut it by 0x00FFFFFF, for 16-bit image we will access two bytes out of buffer.
That was made just to prevent read access violation of these bytes and make some codereview programs like boundschecker to be happy. Wink | ;)
 
I've changed my email and lost the password, so I can't modify the article, and I think in my case, administrator would not help me.Frown | :(
 
Thanks for the interest, happy programming. Cool | :cool:
GeneralCustom controls ... PinmemberAdrian Datcu1:58 10 Jun '04  
GeneralFantastic !!! PinsussHarleyDude17:37 28 Apr '04  
GeneralGreat example PinmemberUbersnack1:42 10 Jun '03  
GeneralAdapting this PinmemberJudd4:27 11 Dec '02  
Generalfinally found it PinmemberKevin Smith10:10 1 Nov '02  
GeneralLarge Fonts PinmemberMatt Philmon12:11 5 Feb '02  
GeneralRegionToBitmap class... Pinmembersaturn2:53 5 Feb '02  
GeneralArticle screwed up... no links PinmemberMatt Philmon5:44 11 Dec '01  
GeneralRe: Article screwed up... no links PinmemberChris Maunder11:44 11 Dec '01  
Generaldoesn't work in 16bits, look at this valid code ... PinmemberF.Julien1:28 20 Sep '01  
Generaldoesn't work when you want white as transparent color Pinmemberbene4:47 29 Aug '01  
GeneralI found here how to load HBITMAP from file... PinmemberPavel Kurochkin13:42 26 Aug '01  
Questionit's not working .... why? Pinmemberscwpark21:15 30 Jul '01  
QuestionWhat it taje to get ::ExtCreateRegion work in NT PinsussAndy18:01 9 Oct '00  
AnswerRe: What it taje to get ::ExtCreateRegion work in NT PinmemberAnonymous0:04 2 Mar '01  
GeneralRe: What it taje to get ::ExtCreateRegion work in NT PinmemberAnonymous19:46 2 Apr '01  
Generalbuggy, slow code PinsussFeng Yuan www,fengyuan.com20:37 8 Sep '00  
GeneralRe: buggy, slow code PinsussLance Gordon5:04 29 Sep '00  
GeneralI need some help here... (rookie!) PinsussMatuX18:27 19 Jul '00  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120210.1 | Last Updated 11 Dec 2001
Article Copyright 2000 by Yuriy Zaporozhets
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid