Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6163015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:45:18+00:00 2026-05-23T21:45:18+00:00

I need to create a CImage from a byte array (actually, its an array

  • 0

I need to create a CImage from a byte array (actually, its an array of unsigned char, but I can cast to whatever form is necessary). The byte array is in the form “RGBRGBRGB…”. The new image needs to contain a copy of the image bytes, rather than using the memory of the byte array itself.

I have tried many different ways of achieving this — including going through various HBITMAP creation functions, trying to use BitBlt — and nothing so far has worked.

To test whether the function works, it should pass this test:

BYTE* imgBits;
int width;
int height;
int Bpp;   // BYTES per pixel (e.g. 3)
getImage(&imgBits, &width, &height, &Bpp); // get the image bits

// This is the magic function I need!!!
CImage img = createCImage(imgBits, width, height, Bpp);

// Test the image
BYTE* data = img.GetBits();  // data should now have the same data as imgBits

All implementations of createCImage() so far have ended up with data pointing to an empty (zero filled) array.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T21:45:19+00:00Added an answer on May 23, 2026 at 9:45 pm

    Thanks everyone, I managed to solve it in the end with your help. It mainly involved @tinman and @Roel’s suggestion to use SetDIBitsToDevice(), but it involved a bit of extra bit-twiddling and memory management, so I thought I’d share my end-point here.

    In the code below, I assume that width, height and Bpp (Bytes per pixel) are set, and that data is a pointer to the array of RGB pixel values.

    // Create the header info
    bmInfohdr.biSize = sizeof(BITMAPINFOHEADER);
    bmInfohdr.biWidth = width;
    bmInfohdr.biHeight = -height;
    bmInfohdr.biPlanes = 1;
    bmInfohdr.biBitCount = Bpp*8;
    bmInfohdr.biCompression = BI_RGB;
    bmInfohdr.biSizeImage = width*height*Bpp;
    bmInfohdr.biXPelsPerMeter = 0;
    bmInfohdr.biYPelsPerMeter = 0;
    bmInfohdr.biClrUsed = 0;
    bmInfohdr.biClrImportant = 0;
    
    BITMAPINFO bmInfo;
    bmInfo.bmiHeader = bmInfohdr;
    bmInfo.bmiColors[0].rgbBlue=255;
    
    // Allocate some memory and some pointers
    unsigned char * p24Img = new unsigned char[width*height*3];
    BYTE *pTemp,*ptr;
    pTemp=(BYTE*)data;
    ptr=p24Img;
    
    // Convert image from RGB to BGR
    for (DWORD index = 0; index < width*height ; index++)
    {
        unsigned char r = *(pTemp++);
        unsigned char g = *(pTemp++);
        unsigned char b = *(pTemp++);   
    
        *(ptr++) = b;
        *(ptr++) = g;
        *(ptr++) = r;
    }
    
    // Create the CImage
    CImage im;
    im.Create(width, height, 24, NULL);
    
    HDC dc = im.GetDC();
    SetDIBitsToDevice(dc, 0,0,width,height,0,0, 0, height, p24Img, &bmInfo, DIB_RGB_COLORS);
    im.ReleaseDC();
    
    delete[] p24Img;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create an UIImage from a byte array. Here is now I
I need to create a 2D int array of size 800x800. But doing so
I need to create an extension method to array class, but this extension method
I need to create a historical timeline starting from 1600's to the present day.
I need to create a grayscale image from data in an nio ShortBuffer. I
I want add new user from another script and I need create password for
I need to create a common installtion of Wordpress which I can then use
I need to create an array of strings, easy enough... the only problem is
I need to create a form in the view (not in the edit )
I need to create a login form with 'username' 'password' fields and two buttons

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.