How do I send an image stored using GdipSaveImageToStream(), in an IStream interface, through a socket connection?
The send() function needs a char array, but I don't know how to extract the data from IStream to a char array.
Would using memcpy() be a good idea? Store the data in a char array and reconstruct the stream on the other side?
[EDIT]
I tried using read but I'm missing something.
//stream to char array, to send
STATSTG myStreamStats;
ULONG bytesSaved;
myStream->Stat(&myStreamStats, 0);
char* streamData = new char[myStreamStats.cbSize.QuadPart];
if(myStream->Read(streamData, myStreamStats.cbSize.QuadPart, &bytesSaved) == S_OK)
cout<<"OK!"<<endl;
else
cout<<"Not OK!"<<endl;
//char array to stream, to save
if(myStreamR->Write(streamData, myStreamStats.cbSize.QuadPart, &bytesSaved) == S_OK)
cout<<"OK!"<<endl;
else
cout<<"Not OK!"<<endl;
myImage = Image::FromStream(myStreamR);
myImage->Save(lpszFilename, &imageCLSID, NULL);
[Note1:] The program compiles and runs but I don't get my image. I do get it if I use the original "myStream" but not with "myStreamR" which is constructed from the char array read from the original stream.
[Note2:] The output is two "OK!"s which means that all the bytes are copied into the array and all of them are pasted into the new stream. However, I checked savedBytes and I discovered that after read() it's 0(not good), while after write() it's equal to the stream size I gave. Then why on Earth is read() giving me a "S_OK" flag if nothing is read?
[Note3:] I am missing something.
Using Read and Write is indeed the right approach as Joachim Pileborg and Zane have pointed out.
However, remember that the seek cursor changes position each time the stream is used, so before the Read and before the Write, use the Seek method to get the cursor back to the beginning of the stream.
Example:
LARGE_INTEGER li;
li.QuadPart = 0;
myStream->Seek(li, STREAM_SEEK_SET, NULL);