V
Vin Y
Guest
Hi,
I want to send a icon over the socket and create the bitmap on the receiver side. I have the below implementation where I'm able to save the icon locally. Could you please add your thoughts to send them over the sockets or the way through which I can receive and create a icon on the receiver side.
Note:- I don't want to save in the file. I want to store in a buffer and send the buffer over the socket and create a icon on the other side.
I tried the other way Link. It didn't work with some icons. So, I came back to this approach.
Thank you in advance.
Code Snippet:-
int nColorBits = 32;
HDC dc;
BOOL bSaved = TRUE;
dc =::GetDC(NULL); // ensure that DC is released when function ends
// Open file for writing:
HRESULT hr;
// Write header:
UCHAR icoHeader[6] = {0, 0, 1, 0, 1, 0}; // ICO file with 1 image
DWORD written =0;
CURSORINFO ci;
ci.cbSize = sizeof(ci);
GetCursorInfo(&ci);
ICONINFO iconInfo;
GetIconInfo(ci.hCursor, &iconInfo);
BITMAPINFO bmInfo = {0};
bmInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmInfo.bmiHeader.biBitCount = 0; // don't get the color table
if (!GetDIBits(dc, iconInfo.hbmColor, 0, 0, NULL, &bmInfo, DIB_RGB_COLORS))
{
bSaved = FALSE;
}
// Allocate size of bitmap info header plus space for color table:
int nBmInfoSize = sizeof(BITMAPINFOHEADER);
if (nColorBits < 24)
{
nBmInfoSize += sizeof(RGBQUAD) * (int)(1 << nColorBits);
}
CAutoVectorPtr<UCHAR> bitmapInfo;
bitmapInfo.Allocate(nBmInfoSize);
BITMAPINFO* pBmInfo = (BITMAPINFO*)(UCHAR*)bitmapInfo;
memcpy(pBmInfo, &bmInfo, sizeof(BITMAPINFOHEADER));
// Get bitmap data:
if((bmInfo.bmiHeader.biSizeImage != 0))
{
bSaved = FALSE;
}
CAutoVectorPtr<UCHAR> bits;
bits.Allocate(bmInfo.bmiHeader.biSizeImage);
pBmInfo->bmiHeader.biBitCount = nColorBits;
pBmInfo->bmiHeader.biCompression = BI_RGB;
if (!GetDIBits(dc, iconInfo.hbmColor, 0, bmInfo.bmiHeader.biHeight, (UCHAR*)bits, pBmInfo, DIB_RGB_COLORS))
{
bSaved = FALSE;//Testing found where we can know the image to be removed.
}
// Get mask data:
BITMAPINFO maskInfo = {0};
maskInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
maskInfo.bmiHeader.biBitCount = 0; // don't get the color table
if (!GetDIBits(dc, iconInfo.hbmMask, 0, 0, NULL, &maskInfo, DIB_RGB_COLORS))
{
bSaved = FALSE;
}
if(maskInfo.bmiHeader.biBitCount == 1)
{
bSaved = FALSE;
}
CAutoVectorPtr<UCHAR> maskBits;
maskBits.Allocate(maskInfo.bmiHeader.biSizeImage);
CAutoVectorPtr<UCHAR> maskInfoBytes;
maskInfoBytes.Allocate(sizeof(BITMAPINFO) + 2 * sizeof(RGBQUAD));
BITMAPINFO* pMaskInfo = (BITMAPINFO*)(UCHAR*)maskInfoBytes;
memcpy(pMaskInfo, &maskInfo, sizeof(maskInfo));
if (!GetDIBits(dc, iconInfo.hbmMask, 0, maskInfo.bmiHeader.biHeight, (UCHAR*)maskBits, pMaskInfo, DIB_RGB_COLORS))
{
bSaved = FALSE;
}
// Write directory entry:
ICONDIRENTRY dir;
dir.nWidth = (UCHAR) pBmInfo->bmiHeader.biWidth;
dir.nHeight = (UCHAR) pBmInfo->bmiHeader.biHeight;
dir.nNumColorsInPalette = (nColorBits == 4 ? 16 : 0);
dir.nReserved = 0;
dir.nNumColorPlanes = 0;
dir.nBitsPerPixel = pBmInfo->bmiHeader.biBitCount;
dir.nDataLength = pBmInfo->bmiHeader.biSizeImage + pMaskInfo->bmiHeader.biSizeImage + nBmInfoSize;
dir.nOffset = sizeof(dir) + sizeof(icoHeader);
// file.Write(&dir, sizeof(dir));
// Write DIB header (including color table):
int nBitsSize = pBmInfo->bmiHeader.biSizeImage;
pBmInfo->bmiHeader.biHeight *= 2; // because the header is for both image and mask
pBmInfo->bmiHeader.biCompression = 0;
pBmInfo->bmiHeader.biSizeImage += pMaskInfo->bmiHeader.biSizeImage; // because the header is for both image and mask
HBITMAP bmp = NULL;
BITMAPINFO info;
ZeroMemory(&info, sizeof(BITMAPINFO));
info.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
info.bmiHeader.biBitCount = 32;
info.bmiHeader.biPlanes = 1;
info.bmiHeader.biCompression = BI_RGB;
info.bmiHeader.biSizeImage = pBmInfo->bmiHeader.biSizeImage;//bmpInfoHeader.biSizeImage;//sourceImage->GetArea()*4;
info.bmiHeader.biWidth = pBmInfo->bmiHeader.biWidth;//bmpInfoHeader.biWidth;
info.bmiHeader.biHeight = pBmInfo->bmiHeader.biHeight;
info.bmiHeader.biClrUsed = 0;
HANDLE hFile = CreateFile(L"E:\\abccc11.bmp", GENERIC_WRITE, 0, 0, CREATE_ALWAYS, 0, 0);
if (!hFile) hr = HRESULT_FROM_WIN32(GetLastError());
{
bSaved = FALSE;
}
WriteFile(hFile, icoHeader, sizeof(icoHeader), &written, 0);
WriteFile(hFile, &dir, sizeof(dir), &written, 0); //vinay sends
WriteFile(hFile, &info, nBmInfoSize, &written, 0); //vinay sends
WriteFile(hFile, (UCHAR*)bits, nBitsSize, &written, 0); //vinay sends
WriteFile(hFile,(UCHAR*)maskBits, pMaskInfo->bmiHeader.biSizeImage, &written, 0); //vinay sends
CloseHandle(hFile);
Continue reading...
I want to send a icon over the socket and create the bitmap on the receiver side. I have the below implementation where I'm able to save the icon locally. Could you please add your thoughts to send them over the sockets or the way through which I can receive and create a icon on the receiver side.
Note:- I don't want to save in the file. I want to store in a buffer and send the buffer over the socket and create a icon on the other side.
I tried the other way Link. It didn't work with some icons. So, I came back to this approach.
Thank you in advance.
Code Snippet:-
int nColorBits = 32;
HDC dc;
BOOL bSaved = TRUE;
dc =::GetDC(NULL); // ensure that DC is released when function ends
// Open file for writing:
HRESULT hr;
// Write header:
UCHAR icoHeader[6] = {0, 0, 1, 0, 1, 0}; // ICO file with 1 image
DWORD written =0;
CURSORINFO ci;
ci.cbSize = sizeof(ci);
GetCursorInfo(&ci);
ICONINFO iconInfo;
GetIconInfo(ci.hCursor, &iconInfo);
BITMAPINFO bmInfo = {0};
bmInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmInfo.bmiHeader.biBitCount = 0; // don't get the color table
if (!GetDIBits(dc, iconInfo.hbmColor, 0, 0, NULL, &bmInfo, DIB_RGB_COLORS))
{
bSaved = FALSE;
}
// Allocate size of bitmap info header plus space for color table:
int nBmInfoSize = sizeof(BITMAPINFOHEADER);
if (nColorBits < 24)
{
nBmInfoSize += sizeof(RGBQUAD) * (int)(1 << nColorBits);
}
CAutoVectorPtr<UCHAR> bitmapInfo;
bitmapInfo.Allocate(nBmInfoSize);
BITMAPINFO* pBmInfo = (BITMAPINFO*)(UCHAR*)bitmapInfo;
memcpy(pBmInfo, &bmInfo, sizeof(BITMAPINFOHEADER));
// Get bitmap data:
if((bmInfo.bmiHeader.biSizeImage != 0))
{
bSaved = FALSE;
}
CAutoVectorPtr<UCHAR> bits;
bits.Allocate(bmInfo.bmiHeader.biSizeImage);
pBmInfo->bmiHeader.biBitCount = nColorBits;
pBmInfo->bmiHeader.biCompression = BI_RGB;
if (!GetDIBits(dc, iconInfo.hbmColor, 0, bmInfo.bmiHeader.biHeight, (UCHAR*)bits, pBmInfo, DIB_RGB_COLORS))
{
bSaved = FALSE;//Testing found where we can know the image to be removed.
}
// Get mask data:
BITMAPINFO maskInfo = {0};
maskInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
maskInfo.bmiHeader.biBitCount = 0; // don't get the color table
if (!GetDIBits(dc, iconInfo.hbmMask, 0, 0, NULL, &maskInfo, DIB_RGB_COLORS))
{
bSaved = FALSE;
}
if(maskInfo.bmiHeader.biBitCount == 1)
{
bSaved = FALSE;
}
CAutoVectorPtr<UCHAR> maskBits;
maskBits.Allocate(maskInfo.bmiHeader.biSizeImage);
CAutoVectorPtr<UCHAR> maskInfoBytes;
maskInfoBytes.Allocate(sizeof(BITMAPINFO) + 2 * sizeof(RGBQUAD));
BITMAPINFO* pMaskInfo = (BITMAPINFO*)(UCHAR*)maskInfoBytes;
memcpy(pMaskInfo, &maskInfo, sizeof(maskInfo));
if (!GetDIBits(dc, iconInfo.hbmMask, 0, maskInfo.bmiHeader.biHeight, (UCHAR*)maskBits, pMaskInfo, DIB_RGB_COLORS))
{
bSaved = FALSE;
}
// Write directory entry:
ICONDIRENTRY dir;
dir.nWidth = (UCHAR) pBmInfo->bmiHeader.biWidth;
dir.nHeight = (UCHAR) pBmInfo->bmiHeader.biHeight;
dir.nNumColorsInPalette = (nColorBits == 4 ? 16 : 0);
dir.nReserved = 0;
dir.nNumColorPlanes = 0;
dir.nBitsPerPixel = pBmInfo->bmiHeader.biBitCount;
dir.nDataLength = pBmInfo->bmiHeader.biSizeImage + pMaskInfo->bmiHeader.biSizeImage + nBmInfoSize;
dir.nOffset = sizeof(dir) + sizeof(icoHeader);
// file.Write(&dir, sizeof(dir));
// Write DIB header (including color table):
int nBitsSize = pBmInfo->bmiHeader.biSizeImage;
pBmInfo->bmiHeader.biHeight *= 2; // because the header is for both image and mask
pBmInfo->bmiHeader.biCompression = 0;
pBmInfo->bmiHeader.biSizeImage += pMaskInfo->bmiHeader.biSizeImage; // because the header is for both image and mask
HBITMAP bmp = NULL;
BITMAPINFO info;
ZeroMemory(&info, sizeof(BITMAPINFO));
info.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
info.bmiHeader.biBitCount = 32;
info.bmiHeader.biPlanes = 1;
info.bmiHeader.biCompression = BI_RGB;
info.bmiHeader.biSizeImage = pBmInfo->bmiHeader.biSizeImage;//bmpInfoHeader.biSizeImage;//sourceImage->GetArea()*4;
info.bmiHeader.biWidth = pBmInfo->bmiHeader.biWidth;//bmpInfoHeader.biWidth;
info.bmiHeader.biHeight = pBmInfo->bmiHeader.biHeight;
info.bmiHeader.biClrUsed = 0;
HANDLE hFile = CreateFile(L"E:\\abccc11.bmp", GENERIC_WRITE, 0, 0, CREATE_ALWAYS, 0, 0);
if (!hFile) hr = HRESULT_FROM_WIN32(GetLastError());
{
bSaved = FALSE;
}
WriteFile(hFile, icoHeader, sizeof(icoHeader), &written, 0);
WriteFile(hFile, &dir, sizeof(dir), &written, 0); //vinay sends
WriteFile(hFile, &info, nBmInfoSize, &written, 0); //vinay sends
WriteFile(hFile, (UCHAR*)bits, nBitsSize, &written, 0); //vinay sends
WriteFile(hFile,(UCHAR*)maskBits, pMaskInfo->bmiHeader.biSizeImage, &written, 0); //vinay sends
CloseHandle(hFile);
Continue reading...