Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I need to get bytes from HICON handle 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:

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);
Posted
Updated 5-Apr-17 12:30pm

1 solution

Use GetIconInfo function (Windows)[^] or GetIconInfoEx function (Windows)[^] and send these data. This requires using the HBITMAP handles to get access to the bitmap data.

To get the pixel data of an HBITMAP use this for a DIB (Device Independant Bitmap):
DIBSECTION ds;
int nSizeDS = ::GetObject(hBitmap, sizeof(ds), &ds);
// hBitmap is a DIB
if (sizeof(ds) == nSizeDS)
{
    // This should not be necessary.
    // But many MSDN examples perform these calculations.
    if (0 == ds.dsBmih.biSizeImage)
    {
        if (0 == ds.dsBm.bmWidthBytes)
            ds.dsBm.bmWidthBytes = ((ds.dsBm.bmWidth * ds.dsBm.bmBitsPixel + 31) & ~31) / 8;
        ds.dsBmih.biSizeImage = ds.dsBm.bmHeight * ds.dsBm.bmWidthBytes;
    }
    // Image data at ds.dsBm.bmBits
    // Data size in ds.dsBmih.biSizeImage
}
 
Share this answer
 
Comments
Member 12891988 5-Apr-17 18:32pm    
Thanks a lot, I think to have solved about send in server side:


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


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:


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.
Jochen Arndt 6-Apr-17 2:57am    
Because that is C# related I suggest to open a new question.

To create a bitmap you need also the dimension and colour depth. I suggest to send the BITMAPINFOHEADER structure too (ds.dsBmih from my example). Then pass the relevant data of that structure to the C# Bitmap constructor that accepts, width, height, stride, colour depth, and byte array.
Member 12891988 6-Apr-17 5:15am    
Hi, thanks and sorry but I have not to understood well you example, more exactly I do not understood what I should send to client via socket.
I tryed to do this:

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

But I do not know how to send the other that you said.
Can You give a simple example combining with my little solution?
PS. I need to do multiple sending?
Before I send this:
send(newConnection,(CHAR*)&oIconInfo,sizeof(oIconInfo),NULL);
and then I send:
BITMAPINFOHEADER bitmapinfoheader;
send(newConnection,(CHAR*)&bitmapinfoheader,sizeof(bitmapinfoheader),NULL);

I do not know how can do in practically.
Member 12891988 6-Apr-17 5:45am    
I've tryed this, but I do not know How to continue:

ICONINFO oIconInfo;
DIBSECTION ds;
if(::GetIconInfo(icon, &oIconInfo)==true){
HBITMAP hBitmap = oIconInfo.hbmColor;
int nSizeDS = ::GetObject(hBitmap, sizeof(ds), &ds);
// hBitmap is a DIB
if (0 == ds.dsBmih.biSizeImage)
{
if (0 == ds.dsBm.bmWidthBytes)
ds.dsBm.bmWidthBytes = ((ds.dsBm.bmWidth * ds.dsBm.bmBitsPixel + 31) & ~31) / 8;
ds.dsBmih.biSizeImage = ds.dsBm.bmHeight * ds.dsBm.bmWidthBytes;
}
send(newConnection,(CHAR*)&oIconInfo,sizeof(oIconInfo),NULL);
}
Jochen Arndt 6-Apr-17 5:47am    
See my answer to your new question

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