如何将HBITMAP转换为字节数组或字符串? [英] How to convert HBITMAP to byte array or string?

查看:57
本文介绍了如何将HBITMAP转换为字节数组或字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

我有两个单独的程序,一个用C#和.NET编写,另一个用C ++和WinAPI编写.第一个为第二个创建配置文件.在这些配置文件中,我插入图像和其他数据.然后,我考虑使用两个平台上都可用的HBITMAP.但是我需要将HBITMAP转换为可以保存为字节数组或字符串的文件中的某种结构.

配置文件的结构将如下所示:
[DataID] [DataLenght] [Data]
[DataID] [DataLenght] [Data]
[DataID] [DataLenght] [Data]
...

我在Google上看过,但没有满意的答案.

有人知道怎么做吗?还是知道更好的方法?

@
好的,谢谢您的答复.

但是,如何在C#中获得BYTE数组?

我搜索并找到了此代码.

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?

@
OK, thanks for the replies.

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

I searched and found this code.

<pre lang="cs">        private byte[] BmpToByte(Bitmap bmp)<br />
        {<br />
            MemoryStream ms = new MemoryStream();<br />
            // Save to memory using the Jpeg format<br />
            bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);<br />
            // read to end<br />
            byte[] bmpBytes = ms.GetBuffer();<br />
            bmp.Dispose();<br />
            ms.Close();<br />
            return bmpBytes;<br />
        }</pre><br />



如果我使用SetBitmapBits加载此代码生成的字节数组,它将起作用吗?



If I load a byte array generated by this code with SetBitmapBits it will work?

推荐答案

HBITMAP是内部Windows对象的句柄.您需要从句柄中获取实际的位图,然后将其存储在资源文件中.请参见此处 [
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.


使用GetBitmapBits可以从HBITMAP获取字节数据.
http://www.codeproject.com/KB/graphics/using_get_set_bitmapbits.aspx [ ^ ]
Using the GetBitmapBits you can get the Byte data from HBITMAP.
http://www.codeproject.com/KB/graphics/using_get_set_bitmapbits.aspx[^]


此处是转换为位图:

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;
				}


这篇关于如何将HBITMAP转换为字节数组或字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆