I have to send bitmap and do something like this:

Client:

HBITMAP bmp = LoadBitmap(0,MAKEINTRESOURCE(1));
send(socket,(char*)&bmp,sizeof(HBITMAP),0);

Server:

HBITMAP bmp;
recv(socket,(char*)&bmp,sizeof(HBITMAP),0);

But this won't work and I know it... so I have to use e.g. GetDIBits and SetDIBits.
But how? Please don't give any example links, they will rarely help. I have tried myself by searching google.

Write the bitmap to a file, then send the file?

That's pretty much what you'd have to send anyway using a bunch of API calls, namely
- the overall dimensions and properties of the bitmap
- the colour table
- the bits.

But saving to a file is AFAIK an already available API to use.

I know how to save the bitmap to file and then send it byte by byte. But it could be nice to know how to send bitmap without saving it first, e.g. if the directory doesn't allow you to create new files.

Sure you can do that, so what's the problem?

I could tell you to read a few APIs, but you've already said you're not interested in that information.

It would be better if you posted some code, then we can see what it is you're really stuck on.

There isn't server hosting, main or another ws functions, but they aren't important now.

#include <stdio.h>
#include <windows.h>
#include <fstream>
using namespace std;

bool SaveBmp(const char *filename, HBITMAP hb)
{
     HDC hdc=NULL;
     FILE *file=NULL;
     LPVOID buf=NULL;
     BITMAPINFO bmpInfo;
     BITMAPFILEHEADER bmpFileHeader;
     hdc=GetDC(NULL);
     ZeroMemory(&bmpInfo,sizeof(BITMAPINFO));
     bmpInfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
     GetDIBits(hdc,hb,0,0,NULL,&bmpInfo,DIB_RGB_COLORS);
     if(bmpInfo.bmiHeader.biSizeImage <= 0)
            bmpInfo.bmiHeader.biSizeImage=bmpInfo.bmiHeader.biWidth*abs(bmpInfo.bmiHeader.biHeight)*(bmpInfo.bmiHeader.biBitCount+7)/8;
     if((buf = malloc(bmpInfo.bmiHeader.biSizeImage)) == NULL)
            return 0;
     bmpInfo.bmiHeader.biCompression = BI_RGB;
     GetDIBits(hdc,hb,0,bmpInfo.bmiHeader.biHeight,buf,&bmpInfo,DIB_RGB_COLORS);
     if((file = fopen(filename,"wb")) == NULL)
            return 0;
     bmpFileHeader.bfReserved1=0;
     bmpFileHeader.bfReserved2=0;
     bmpFileHeader.bfSize=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+bmpInfo.bmiHeader.biSizeImage;
     bmpFileHeader.bfType=19778;
     bmpFileHeader.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER);
     fwrite(&bmpFileHeader,sizeof(BITMAPFILEHEADER),1,file);
     fwrite(&bmpInfo.bmiHeader,sizeof(BITMAPINFOHEADER),1,file);
     fwrite(buf,bmpInfo.bmiHeader.biSizeImage,1,file);

     ReleaseDC(0,hdc);
     free(buf);
     fclose(file);
     
     return 1;
}


void Send(const char *filename, int clientSocket) //filename = source bitmap
{
     ifstream bmpfile;
     bmpfile.open(filename, ios::in|ios::binary);
     if(bmpfile == NULL)
          return;
     bmpfile.seekg(0,ios::end);
     int size = bmpfile.tellg();
     bmpfile.seekg(0,ios::beg);

     size = htonl(size);
     send(clientSocket,(char*)&size,sizeof(size),0); //send the size of file
     size = ntohl(size);
     
     
     while(!bmpfile.eof())
     {
           char Byte[1] = {0};
           Byte[0] = bmpfile.get();
           send(clientSocket,Byte,1,0); //send bitmap byte by byte
     }
     bmpfile.close();
}

I can use GetDIBits somehow.
I simply want to send the bitmap without saving it...

This article has been dead for over six months. Start a new discussion instead.