来自HICON的Grt字节数组,用于发送到C#客户端并在listview上打印 [英] Grt bytes array from HICON for sending to C# client and printing on listview

查看:118
本文介绍了来自HICON的Grt字节数组,用于发送到C#客户端并在listview上打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从HICON句柄获取字节以将其从服务器(C ++)发送到客户端(C#),但我不知道如何。



i需要用

发送字节发送(socket,(CHAR *)& Byte_to_send,sizeof(Byte_to_send);



Byte_to_send应为与每个Windows应用程序的HICON相关联的字节,以及



我用这种方式得到它:

HICON icon =(HICON)GetClassLong (hWnd,GCL_HICON);



我的尝试:



SIDE C ++服务器:

Hi, I need to get bytes from HICON handle to send it from server(C++) to client(C#) but I do not know how.

i need to send the byte with
send(socket,(CHAR*)&Byte_to_send,sizeof(Byte_to_send);

Byte_to_send should be the bytes associated to HICON of each windows application, and

I've got it in this way:
HICON icon = (HICON)GetClassLong(hWnd, GCL_HICON);

What I have tried:

SIDE C++ SERVER:

HICON icon = (HICON)GetClassLong(hWnd, GCL_HICON);

ICONINFO oIconInfo;
if(::GetIconInfo(icon, &oIconInfo)==true){
cout <<"Success!"<<endl;
send(newConnection,(CHAR*)&oIconInfo,sizeof(oIconInfo),NULL);
}else{
cout <<"Failure!"<<endl;
}





现在我必须转换从我的客户端读取的字节流(这是用c#制作的)我收到的从我的c ++服务器,并将字节数组转换为图像。

所以我试图这样做:



SIDE C#客户端:



Now I have to convert the stream of byte read from my client(this is made in c#) that I have received from my c++ server, and convert the array of bytes to an Image.
So I have tried to do this:

SIDE C# CLIENT:

byte[] buffer = new byte[1500];
sck.Receive(buffer);
TypeConverter tc = TypeDescriptor.GetConverter(typeof(Bitmap));
Bitmap bitmap1 = (Bitmap)tc.ConvertFrom(buffer);
imageList1.Images.Add(bitmap1);





当我尝试这个时,我收到一行错误:



When I try this, I receive an error at this line:

Bitmap bitmap1 = (Bitmap)tc.ConvertFrom(buffer);



这是我收到的消息错误:


This is the message error that I have received:

Unhandled exception of the 'System.ArgumentException' type System.Drawing.dll
Additional information: Invalid parameter.



我也尝试过:


I have also tried:

MemoryStream ms = new MemoryStream(buffer);
Bitmap bmp;
bmp = new Bitmap(ms);



和另外这个:


and this other:

ImageConverter ic = new ImageConverter();
System.Drawing.Image img = (System.Drawing.Image)ic.ConvertFrom(buffer);
Bitmap bitmap1 = new Bitmap(img);



但我收到了同样的错误信息。


but I have received the same error message.

推荐答案

C ++部分未实现我在从HICON获取字节到通过套接字从服务器发送到客户端 [ ^ ]。 ICONINFO 结构不包含图像数据,只包含位图句柄。您必须发送图像信息和数据。



从头开始未经测试:

The C++ portion is not implementing what I have suggested at Get bytes from HICON to sending via socket from server to client[^]. The ICONINFO structure does not contain the image data but only a bitmap handle. You have to send the image information and data.

Untested from scratch:
ICONINFO oIconInfo;
if (::GetIconInfo(icon, &oIconInfo))
{
    // EDIT: Modified code to create a DIB
    HBITMAP hDib = oIconInfo.hbmColor;

    // Get bitmap info for colour mask
    DIBSECTION ds;
    int nSizeDS = ::GetObject(hDib, sizeof(ds), &ds);
    // EDIT: is not a DIB: Create one
    if (sizeof(ds) != nSizeDS)
    {
        hDib = (HBITMAP)::CopyImage(oIconInfo.hbmColor, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION);
        nSizeDS = ::GetObject(hDib, sizeof(ds), &ds);
    }
    // Send the DIBSECTION
    send(newConnection, &ds, sizeof(ds), NULL);
    // Send raw bitmap data
    send(newConnection, ds.dsBm.bmBits, ds.dsBmih.biSizeImage, NULL);
    // EDIT: Release bitmap when a DIB copy has been created
    if (hDib && hDib != oIconInfo.hbmColor)
        ::DeleteObject(hDib);
}



在C#接收站点上首先收到 DIBSECTION 结构。然后将原始数据接收到大小为 ds.dsBmih.biSizeImage 的缓冲区中。最后使用位图构造函数(Int32,Int32,Int32,PixelFormat, IntPtr)(System.Drawing) [ ^ ]。

参数为:

width = ds.dsBm.bmWidth ds.dsBmih.biWidth

身高 = ds.dsBm.bmHeight ds.dsBmih.biHeight

stride = ds.dsBm.bmWidthBytes ;

format =取决于 ds.dsBm.bmBitsPixel ;通常 Format24bppRgb

scan0 =原始数据缓冲区


On the C# receiving site receive the DIBSECTION structure first. Then receive the raw data into an buffer of size ds.dsBmih.biSizeImage. Finally create the bitmap using the Bitmap Constructor (Int32, Int32, Int32, PixelFormat, IntPtr) (System.Drawing)[^].
The parameters are:
width = ds.dsBm.bmWidth or ds.dsBmih.biWidth
height = ds.dsBm.bmHeight or ds.dsBmih.biHeight
stride = ds.dsBm.bmWidthBytes;
format = depends on ds.dsBm.bmBitsPixel; usually Format24bppRgb
scan0 = the raw data buffer


这篇关于来自HICON的Grt字节数组,用于发送到C#客户端并在listview上打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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