i'm trying overloading the operators >> and << without sucess because i'm using IStream with image:
Code:
friend std::ostream& operator << (std::ostream& lhs, image& rhs)
{
//Create an empty IStream:
IStream* pIStream = nullptr;
if(CreateStreamOnHGlobal(NULL, TRUE, (LPSTREAM*)&pIStream)!=S_OK)
DebugText("error on creating an empty IStream");
//choose image format for save it on IStream:
// Get encoder class id for jpg compression
// for other compressions use
// image/bmp
// image/jpeg
// image/gif
// image/tiff
// image/png
CLSID pngClsid;
GetEncoderClsid(L"image/gif", &pngClsid);
// Setup encoder parameters
EncoderParameters encoderParameters;
encoderParameters.Count = 1;
encoderParameters.Parameter[0].Guid = EncoderQuality;
encoderParameters.Parameter[0].Type = EncoderParameterValueTypeLong;
encoderParameters.Parameter[0].NumberOfValues = 1;
// setup compression level
ULONG quality = 50;
encoderParameters.Parameter[0].Value = &quality;
// Save the image to the stream
if(rhs.img->Save(pIStream, &pngClsid, &encoderParameters) != Ok)
{
pIStream->Release();
DebugText("error on saving to IStream");
}
//getting the stream size:
STATSTG sts;
pIStream->Stat(&sts, STATFLAG_DEFAULT);
ULARGE_INTEGER uli = sts.cbSize;
LARGE_INTEGER zero;
zero.QuadPart = 0;
int size = (int)uli.QuadPart;
char* bits = new char[size];
DebugText("write: " + to_string(size));
ULONG written;
pIStream->Seek(zero, STREAM_SEEK_SET, NULL);
pIStream->Read(bits, size, &written);
//write the stream size on file
lhs.write(reinterpret_cast<char*>(&size),sizeof(int));
//write pBuff data on file
lhs.write(reinterpret_cast<char*>(bits),size);
//clean resources
delete[] bits;
pIStream->Release();
return lhs;
}
friend std::istream& operator >> (std::istream& lhs, image& rhs)
{
//getting IStream size:
int streamsize;
lhs.read(reinterpret_cast<char*>(&streamsize), sizeof(int));
// getting IStream data:
IStream* pIStream = nullptr;
char* p =new char[streamsize];
lhs.read(reinterpret_cast<char*>(p),streamsize);
if(CreateStreamOnHGlobal(NULL, TRUE, (LPSTREAM*)&pIStream)!=S_OK)
DebugText("error on creating an empty IStream");
LARGE_INTEGER zero;
zero.QuadPart = 0;
pIStream->Seek(zero, STREAM_SEEK_SET, NULL);
ULONG written;
pIStream->Write(p,streamsize, &written);
//reading IStream to Image class:
rhs.img=Image::FromStream(pIStream);
Status stat = rhs.img->GetLastStatus();
if(stat!=S_OK)
DebugText("error reading stream to Image: " + to_string(stat));
else
{
DebugText("reading stream to Image sucessfull");
}
//realease resources:
pIStream->Release();
//prepare the image to be drawed:
rhs.imageweight=rhs.img->GetWidth();
rhs.imageheight=rhs.img->GetHeight();
rhs.intSelectFrame=0;
rhs.HBitmap.resize(rhs.imageweight,rhs.imageheight);
Gdiplus::Graphics graphics(rhs.HBitmap);
Matrix rotatepoint;
PointF rotatezeropoint={(REAL)rhs.imageweight/2,(REAL)rhs.imageheight/2};
rotatepoint.RotateAt(rhs.intRotate,rotatezeropoint);
graphics.SetTransform(&rotatepoint);
graphics.DrawImage(rhs.img, 0,0,rhs.imageweight,rhs.imageheight);
BitBlt(GetWindowDC(ActivatedForm),100,100,rhs.imageweight,rhs.imageheight,rhs.HBitmap,0,0,SRCCOPY);
UINT count = 0;
count = rhs.img->GetFrameDimensionsCount();
vector<GUID> pDimensionIDs;
pDimensionIDs.resize(count);
rhs.img->GetFrameDimensionsList(pDimensionIDs.data(), pDimensionIDs.size());
rhs.framecount=rhs.img->GetFrameCount(pDimensionIDs.data());
rhs.framedelay =rhs.img->GetPropertyItemSize(PropertyTagFrameDelay);
if(rhs.framecount>1)
{
rhs.tmrAnimation.timerprocedure=[&]()
{
static int intFrame=0;
intFrame=intFrame+1;
if(intFrame==rhs.framecount)
intFrame=0;
rhs.SelectFrame=intFrame;
};
rhs.tmrAnimation.Interval=rhs.framedelay;
rhs.tmrAnimation.Start();
}
DeleteBitmap(rhs.hbmMask);
rhs.hbmMask = HBITMAPfromHDC(rhs.HBitmap);
rhs.UpdateReturnBitmap();
//these is for do the reset option:
rhs.OriginalBitmap=rhs.HBitmap;
rhs.DrawImage(rhs.intSelectFrame);
rhs.AfterDrawImage(rhs.intSelectFrame);
return lhs;
}
(updating)
finally i can get the image.
but i see 1 problems:
- i only get 1 image using the IStream.
Code:
//Save the image to IStream:
rhs.img->Save(pIStream, &pngClsid, &encoderParameters);
//Open the image from IStream:
rhs.img=Image::FromStream(pIStream);
why i only get just 1 image?