Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
GetIcon: save as file
#5
SaveBitmap saves only as bmp. Not transparent.

To save png, or bmp with alpha channel, use GDI++. It is to be used with C++, don't know is it possible with QM. Or find some graphic library.

This is SaveBitmap source. The commented part supports transparent bitmaps and png.

Code:
Copy      Help
EXPORT BOOL SaveBitmap(HBITMAP h, LPCSTR file, DWORD flags/*, LPCSTR format*/)
{
    //flags: 1 file is IStream* (from beginning), 2 preserve alpha (XP+; ignored on win2000; ignored in exe; currently disabled)
    ////format: on win2000 fails if not empty. Currently not used. By default does not work well. Eg when h is with alpha, does not preserve alpha in saved png. Let people use normal image conversion programs.

//#ifdef PROJECT_QM
//    //if(Len1(format))
//    //{
//    //    if(ver.winver<0x501) return 0;
//    //    flags|=2;
//    //}
//
//    if((flags&2) && ver.winver>=0x501) //use gdi+. Instead should use GetDIBits.
//    {
//        static ULONG_PTR g_gditoken; //don't init/uninit everytime because it takes about 10 times longer than the useful code
//        if(!g_gditoken)
//        {
//            Gdiplus::GdiplusStartupInput gi(0, 0, 0);
//            if(Gdiplus::GdiplusStartup(&g_gditoken, &gi, 0)) return 0;
//        }
//
//        //CStr ss;
//        CLSID bmpClsid; util::IIDFromStringA("{557CF400-1A04-11D3-9A73-0000F81EF32E}", &bmpClsid);
//
//        //if(Len1(format)) { ss.From("image/", format); format=ss.p; }
//        //else format="image/bmp";
//        //if(!GdiplusGetEncoderClsid(format, bmpClsid)) return 0;
//        //if(!GdiplusGetEncoderClsid("image/bmp", bmpClsid)) return 0;
//        //CStr s; s.FromGUID(bmpClsid); zz(s);
//
//        //CHbitmap bmcopy=CopyImage(h, IMAGE_BITMAP, 0, 0, 0); //may be selected in a dc. But works well without this. Should use LR_CREATEDIBSECTION?
//        //Gdiplus::Bitmap b(bmcopy, 0);
//        Gdiplus::Bitmap b(h, 0);
//
//        if(flags&1) return !b.Save((IStream*)file, &bmpClsid, 0);
//        CStr ss;
//        return !b.Save(ss.ExpandPathW(file), &bmpClsid, 0);
//    }
//    else //use IPicture
//#endif
    {
        CSmartPointer <IPicture> pict;
        PICTDESC pd; ZEROSIZ(pd);
        pd.picType=PICTYPE_BITMAP;
        pd.bmp.hbitmap=h;
        if(OleCreatePictureIndirect(&pd, IID_IPicture, 0, (void**)&pict)) return 0;

        if(flags&1)
        {
            CStream::sSetPos((IStream*)file, 0); //don't know is it necessary. Some ole picture functions have bugs and behave differently on different OS...
            return !pict->SaveAsFile((IStream*)file, 1, 0);
        }

        CStream is; CStr ss;
        return !is.CreateOnFile(file, STGM_WRITE|STGM_CREATE)
            && !pict->SaveAsFile(is, 1, 0);

        //don't use OleSavePictureFile because it does not support Unicode paths
    }
}

Code:
Copy      Help
//#ifdef PROJECT_QM //could be used in exe too, but adds 1 KB and is not necessary
//bool GdiplusGetEncoderClsid(LPCSTR format, CLSID& pClsid)
//{
//    UINT i=0, n=0;
//    Gdiplus::ImageCodecInfo* pImageCodecInfo;
//    CSTR2(s1, s2);
//
//    Gdiplus::GetImageEncodersSize(&n, &i); if(!i) return 0;
//    pImageCodecInfo = (Gdiplus::ImageCodecInfo*)s1.Buffer(i);
//    Gdiplus::GetImageEncoders(n, i, pImageCodecInfo);
//
//    for(i=0; i<n; i++)
//    {
//        s2.FromW(pImageCodecInfo[i].MimeType);
//        //zz(s2);
//        if(s2%=format) { pClsid=pImageCodecInfo[i].Clsid; return 1; }    
//    }
//
//    return 0;
//    //info: available only for bmp, jpeg, gif, png and tiff
//}
//#endif


Messages In This Thread

Forum Jump:


Users browsing this thread: 1 Guest(s)