Click here to Skip to main content
15,906,329 members
Please Sign up or sign in to vote.
3.33/5 (3 votes)
See more:
Hello

I have two separate programs, one written in C# and .NET and other in C++ and WinAPI. The first creates configuration files for the second. In these configuration files I insert images and other data. Then I thought about using the HBITMAP that is available on both platforms. But I need to convert the HBITMAP to some structure that I can save in the file, as a byte array or a string.

The struct of the configuration file will be like this:
[DataID][DataLenght][Data]
[DataID][DataLenght][Data]
[DataID][DataLenght][Data]
...

I have looked on google but got no satisfactory answer.

Anyone know how to do this? Or know any better method?

@EDIT:
OK, thanks for the replies.

But, how can I get a BYTE array in C#?

I searched and found this code.
C#
private byte[] BmpToByte(Bitmap bmp)
{
    MemoryStream ms = new MemoryStream();
    // Save to memory using the Jpeg format
    bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
    // read to end
    byte[] bmpBytes = ms.GetBuffer();
    bmp.Dispose();
    ms.Close();
    return bmpBytes;
}


If I load a byte array generated by this code with SetBitmapBits it will work?
Posted
Updated 21-Oct-10 4:40am
v2

HBITMAP is a handle to an internal Windows object. You would need to get the actual bitmap from the handle and then store that in your resource file. See here[^] for some of the functions that can help you.
 
Share this answer
 
Using the GetBitmapBits you can get the Byte data from HBITMAP.
http://www.codeproject.com/KB/graphics/using_get_set_bitmapbits.aspx[^]
 
Share this answer
 
Here is conversion to Bitmap:

DWORD *local_bit_map_buffer;
                local_bit_map_buffer = new DWORD[cxImage*cyImage];

                for(int local_counter_width=0;local_counter_width<cxImage;local_counter_width++)
                {
                    for(int local_counter_height=0;local_counter_height<cyImage;local_counter_height++)
                    {
                        int local_couter = local_counter_width+local_counter_height*cxImage;
                        int local_bit_map_couter = local_counter_width+(cyImage-(local_counter_height+1))*cxImage;
                        local_bit_map_buffer[local_bit_map_couter] = RGB_BYTE_ORDER(prgb[local_couter].rgbtRed,prgb[local_couter].rgbtGreen,prgb[local_couter].rgbtBlue);
                    }
                }

                HBITMAP handle_bit_map = NULL;

                handle_bit_map = CreateBitmap(
                    cxImage,
                    cyImage,
                    1,
                    32,
                    local_bit_map_buffer);

                delete []local_bit_map_buffer;
HDC hdc; 
				local_handle_result = gImageSrc->GetDC(&hdc);
				if (local_handle_result!=D3D_OK)
				{
					return E_FAIL;
				}
				
				HDC hdc_compatible = CreateCompatibleDC(hdc);
				
				if (hdc_compatible==NULL)
				{
					return E_FAIL;
				}
				
				if(SelectObject(hdc_compatible,handle_bit_map)==NULL)
				{
					return E_FAIL;
				}
				if(!BitBlt(hdc, 0  ,0 ,cxImage  , cyImage  , hdc_compatible, 0, 0, SRCCOPY))
				{
					return E_FAIL;
				}
				if(gImageSrc->ReleaseDC(hdc))
				{
					return E_FAIL;
				}
				DeleteDC(hdc_compatible);
				
				BOOL local_result = DeleteObject(handle_bit_map);
				if(!local_result)
				{
					return E_FAIL;
				}
 
Share this answer
 

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