how create a IStream(from Image class) for save and read on a file?

i'm overloading the std::ostream& operator << and std::istream& operator >> for save the Image IStream on file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
friend std::ostream& operator << (std::ostream& lhs, const image& rhs)
    {

        //Create an empty IStream:
        IStream* pIStream = nullptr;
        HGLOBAL hg=NULL;
        if(CreateStreamOnHGlobal(hg, FALSE, (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:
        int buffsize=GlobalSize(&hg);

        char *pBuff = new char[buffsize];
        ULONG ulBytesRead;

        //Read the stream to pBuff
        if(pIStream->Read(pBuff,buffsize, &ulBytesRead)!=S_OK)
            DebugText("error on saving IStream to buffer");

        //write the stream size on file
        lhs << buffsize;

        //write pBuff data on file
        lhs << pBuff;

        //clean resources
        delete pBuff;
        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(streamsize));

        IStream* pIStream = nullptr;

        // Create stream with 0 size
        HGLOBAL hg= ::GlobalAlloc(GMEM_MOVEABLE,streamsize);
        GlobalLock(hg);
        lhs.read(reinterpret_cast<char*>(&hg),sizeof(streamsize));
        GlobalUnlock(hg);

        if(CreateStreamOnHGlobal(&hg, TRUE, (LPSTREAM*)&pIStream)!=S_OK)
            DebugText("error on creating an empty IStream");

        //reading IStream on Image class:
        if(rhs.img->FromStream(pIStream,TRUE)!=S_OK)
            DebugText("error reading stream to Image");

        //realease resources:
        pIStream->Release();
        GlobalFree(hg);
        return lhs;
    }

i even don't have sure if the IStream is saved on file.
can anyone tell me where i miss something?
i'm totaly new with IStream.
Last edited on
Topic archived. No new replies allowed.