Click here to Skip to main content
Licence 
First Posted 17 Apr 2002
Views 146,461
Bookmarked 40 times

Using GetBitmapBits and SetBitmapBits

By | 17 Apr 2002 | Article
Explains how to build a bitmap from a byte array using GetBitmapBits and SetBitmapBits

Introduction

This article explains how you can build a bitmap from an array of bytes.This array of bytes is nothing but the byte information of some bitmap which may be obtained in various ways; for example from a camera board whose buffer contains the image data in the form of an array of bytes and you want to build the actual image from this data. For this article we will be obtaining the array of bytes from a known bitmap and use this array to build an initially empty bitmap resource.

Getting Started

  1. Use MFC AppWizard to generate an SDI application.
  2. Use the resource editor to build two bitmaps, both of the same dimensions,one of them will actually have some image in it while the other will be a blank image.

Adding Code

Modify your OnDraw() to look like this :
void CBmpTryView::OnDraw(CDC* pDC)
{
    CBmpTryDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    // TODO: add draw code for native data here

    CDC memdcX,memdcY;
    memdcX.CreateCompatibleDC(pDC);
            //map these CDC objects to your window DC
    memdcY.CreateCompatibleDC(pDC);

    BITMAP bmpX,bmpY;
    
    CBitmap mybmp,bmpClone;
    bmpClone.LoadBitmap(IDB_BITMAP2);
             //initialize the clone bitmap object(empty image in 
             //this case) before using

    DWORD dwValue,dwValue2;    
    
    

      if(TRUE == mybmp.LoadBitmap(IDB_BITMAP1))
      {           
          mybmp.GetBitmap(&bmpX); //Get bitmap dimensions 
                                  //into BITMAP structure.
          BYTE* bmpBuffer=(BYTE*)GlobalAlloc(GPTR, 
             bmpX.bmWidthBytes*bmpX.bmHeight);//allocate memory for image 
                                              //byte buffer
          dwValue=mybmp.GetBitmapBits(bmpX.bmWidthBytes*bmpX.bmHeight,
                   bmpBuffer);//Get the bitmap bits  
                              //into a structure     

          
          dwValue2 = bmpClone.SetBitmapBits(bmpX.bmWidthBytes*bmpX.bmHeight,
               bmpBuffer);//generate image from 
                          //above buffer
          bmpClone.GetBitmap(&bmpY);

          memdcX.SelectObject(mybmp);//select original bitmap
          memdcY.SelectObject(bmpClone);//select clone
          
          //Draw the original bitmap
          pDC->BitBlt(10,10,bmpX.bmWidthBytes,bmpX.bmHeight ,&memdcX,
               0,0,SRCCOPY);

          //Draw the cloned bitmap image
          pDC->BitBlt(10,40,bmpX.bmWidthBytes,bmpX.bmHeight ,&memdcY,
               0,0,SRCCOPY);
          
          GlobalFree((HGLOBAL)bmpBuffer);//Free memory
          
      }
    
      

}

That's it

The code is by and large self explanatory,if you still need any explanation I will be glad to write back. Also please write if theres any better way to do this.

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

Amol Kakhandki

Web Developer

India India

Member

Amol is currently working for a software company in India.His background is an engineering degree in Industrial Electronics.
He has been implementing projects in COM,DCOM,LDAP using VC++ ,MFC,ATL.
This has to be one of my favorite card - Reward Hotel Starwood Preferred Guest Credit Card. Thanks and enjoy!

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
QuestionHow can I use Get/Set BitmapBits as ScanLine from Delphi7 ? PinmemberDomiNickDark21:49 20 Jun '09  
AnswerRe: How can I use Get/Set BitmapBits as ScanLine from Delphi7 ? PinmemberDomiNickDark14:27 31 Jan '10  
Questionhow can i send bitmap to socket??? Pinmemberjags_vc18:44 9 Mar '05  
AnswerRe: how can i send bitmap to socket??? Pinmemberjags_vc22:13 20 Apr '05  
GeneralGet pixel value under user's line Pinmemberipichet18:57 7 Oct '04  
GeneralI want to read bitmap's pixel... Pinmember2ndhandcomputer8:45 24 Sep '04  
GeneralMSDN says don't do that! PinmemberJohn R. Shaw6:00 22 Oct '03  
GeneralRe: MSDN says don't do that! PinmemberJohn R. Shaw11:31 20 Nov '03  
GeneralGreat for monochrome bitmaps PinsussColoradoKind14:34 3 May '04  
Generalconvert 32bit Bitmap intop 24bit Pinmemberschaereran@gmx.net9:11 17 Sep '03  
GeneralRe: convert 32bit Bitmap intop 24bit PinsussAnonymous0:57 25 Apr '05  
GeneralRe: Sending Bitmap through Socket PinsussAnonymous4:20 31 May '03  
GeneralRecive bitmap from socket PinsussVedran22:18 5 Sep '02  
GeneralRe: Recive bitmap from socket PinsussVinod patil0:51 20 Nov '02  
GeneralRe: Recive bitmap from socket Pinmemberjags_vc22:12 20 Apr '05  
GeneralRe: Recive bitmap from socket PinsussAnonymous0:57 20 Nov '02  
GeneralRe: Recive bitmap from socket PinsussAnonymous22:06 27 Nov '02  
GeneralBitmaps at runtime PinmemberAnonymous2:39 27 Jun '02  
QuestionDI BIts ? PinmemberJim Crafton8:57 18 Apr '02  
AnswerRe: DI BIts ? PinmemberAmol Kakhandki17:51 18 Apr '02  

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.120517.1 | Last Updated 18 Apr 2002
Article Copyright 2002 by Amol Kakhandki
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid