Click here to Skip to main content
12,828,403 members (46,717 online)
Click here to Skip to main content
Add your own
alternative version


43 bookmarked
Posted 17 Apr 2002

Using GetBitmapBits and SetBitmapBits

, 17 Apr 2002 CPOL
Rate this:
Please Sign up or sign in to vote.
Explains how to build a bitmap from a byte array using GetBitmapBits and SetBitmapBits


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();
    // TODO: add draw code for native data here

    CDC memdcX,memdcY;
            //map these CDC objects to your window DC

    BITMAP bmpX,bmpY;
    CBitmap mybmp,bmpClone;
             //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
                   bmpBuffer);//Get the bitmap bits  
                              //into a structure     

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

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

          //Draw the cloned bitmap image
          pDC->BitBlt(10,40,bmpX.bmWidthBytes,bmpX.bmHeight ,&memdcY,
          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.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Amol Kakhandki
Software Developer (Senior)
Netherlands Netherlands
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionHow can I use Get/Set BitmapBits as ScanLine from Delphi7 ? Pin
DomiNickDark20-Jun-09 22:49
memberDomiNickDark20-Jun-09 22:49 
AnswerRe: How can I use Get/Set BitmapBits as ScanLine from Delphi7 ? Pin
DomiNickDark31-Jan-10 15:27
memberDomiNickDark31-Jan-10 15:27 
Questionhow can i send bitmap to socket??? Pin
jags_vc9-Mar-05 19:44
memberjags_vc9-Mar-05 19:44 
AnswerRe: how can i send bitmap to socket??? Pin
jags_vc20-Apr-05 23:13
memberjags_vc20-Apr-05 23:13 
GeneralGet pixel value under user's line Pin
ipichet7-Oct-04 19:57
memberipichet7-Oct-04 19:57 
GeneralI want to read bitmap's pixel... Pin
2ndhandcomputer24-Sep-04 9:45
member2ndhandcomputer24-Sep-04 9:45 
GeneralMSDN says don't do that! Pin
John R. Shaw22-Oct-03 7:00
memberJohn R. Shaw22-Oct-03 7:00 
GeneralRe: MSDN says don't do that! Pin
John R. Shaw20-Nov-03 12:31
memberJohn R. Shaw20-Nov-03 12:31 
GeneralGreat for monochrome bitmaps Pin
ColoradoKind3-May-04 15:34
sussColoradoKind3-May-04 15:34 
Generalconvert 32bit Bitmap intop 24bit Pin
schaereran@gmx.net17-Sep-03 10:11
memberschaereran@gmx.net17-Sep-03 10:11 
GeneralRe: convert 32bit Bitmap intop 24bit Pin
Anonymous25-Apr-05 1:57
sussAnonymous25-Apr-05 1:57 
GeneralRe: Sending Bitmap through Socket Pin
Anonymous31-May-03 5:20
sussAnonymous31-May-03 5:20 
GeneralRecive bitmap from socket Pin
Vedran5-Sep-02 23:18
sussVedran5-Sep-02 23:18 
GeneralRe: Recive bitmap from socket Pin
Vinod patil20-Nov-02 1:51
sussVinod patil20-Nov-02 1:51 
GeneralRe: Recive bitmap from socket Pin
jags_vc20-Apr-05 23:12
memberjags_vc20-Apr-05 23:12 
GeneralRe: Recive bitmap from socket Pin
Anonymous20-Nov-02 1:57
sussAnonymous20-Nov-02 1:57 
GeneralRe: Recive bitmap from socket Pin
Anonymous27-Nov-02 23:06
sussAnonymous27-Nov-02 23:06 
GeneralBitmaps at runtime Pin
Anonymous27-Jun-02 3:39
memberAnonymous27-Jun-02 3:39 
QuestionDI BIts ? Pin
Jim Crafton18-Apr-02 9:57
memberJim Crafton18-Apr-02 9:57 
AnswerRe: DI BIts ? Pin
Amol Kakhandki18-Apr-02 18:51
memberAmol Kakhandki18-Apr-02 18:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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 | Terms of Use | Mobile
Web02 | 2.8.170326.1 | Last Updated 18 Apr 2002
Article Copyright 2002 by Amol Kakhandki
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid