Click here to Skip to main content
15,846,571 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi i want to generate in VC++ an image (Bitmap) from a 2D array of numerical value Array[1024][1024] that each value is a RGB from 0 to 255. is there any VC++ function to do this?

Thanks in Advance.
Updated 28-May-13 2:28am
Sergey Alexandrovich Kryukov 13-May-13 10:33am    
What did you try so far?

1 solution

Have a look at SetDIBitsToDevice Win32 function:

It's a very old one, and one I used to do the same job you are doing - I had a 2d array of 8bit signal strength, and that mapped to a pallette of colours. In your case, the colours will be greyscale, but there's no reason they need to be.

One "gotcha" I can think of - the data will have to be a multiple of 4 bytes wide, but as your data is 1024x1024, that's no problem.
Another is that I seem to remember it painting the image upside down, but you can play with the coordinates to see how it works.

MSDN claims support from Win2000, but I was using this function in Windows 3.1, so it's very ancient, very tricky, and very powerful.

Good luck!

Share this answer
Sergey Alexandrovich Kryukov 13-May-13 10:57am    
Sounds good, 5ed.
Rajeshkumar Ramasamy 15-May-13 2:11am    
Are you have any example for SetDIBitsToDevice?
Iain Clarke, Warrior Programmer 15-May-13 3:01am    
There's an example linked from the website I showed above, but I agree it's not very clear.

Unfortunately, any code I can paste in here belongs to previous customers, but I can recommend just looking at the msdn pages, and for BITMAPINFO structure too.
Rajeshkumar Ramasamy 15-May-13 3:30am    
Iain Clarke,
I think you understand my question.
I have only a 2D Array (1024 x 1024), which contains the RGB colors.
My question is, I want to plot these array of pixel into picture box or bitmap, whatever at a time, Now I am using loop for drawing the same, It take more CPU utilization.
But, In SetDIBitsToDevice, We can copy the from a bitmap to destination, Am I right?
Iain Clarke, Warrior Programmer 15-May-13 3:40am    
I guess you have a CDC of some sorts, and are doing SetPixel in a loop?

If so, throw the loop away, and do:
::SetDIBitsToDevice (*pDC, ...., &m_pBlockOfBytes, &m_bmiPrePreparedBitmapStructure, DIB_RGB_COLORS);

Your block of bytes is where your numerical values are - bytes 0-1024, followed by bytes 1040-2047, and so on. It's not quite the same as a 1040x1024 array, but it's equivalent.

You can also prepare your bitmap array in advance too:
m_pbmi = (BITMAPINFO *)malloc (sizeof (BITMAPINFOHEADER) + 256 * sizeof (RGBQUAD));
ZeroMemory (m_pbmi, sizeof (BITMAPINFOHEADER) + 256 * sizeof (RGBQUAD));
m_pbmi->bmiHeader.... = ...;
for (int n = 0; n < 256; n++)
m_pbmi->bmiColors [n].rgbRed = n;
m_pbmi->bmiColors [n].rgbGreen = n;
m_pbmi->bmiColors [n].rgbBlue = n;

As I said before, this is a complex function, but powerful. Once you've done the initial setup work, you can just change one of your data bytes, invalidate your window, and in your paint / ondraw function, you can just call the one line setdibitstodevice function, and you're done!

It's MILES faster than 1 million setpixels.


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

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900