Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
GetIcon: save as file
#1
how do i save the icon as file ?

Code:
Copy      Help
int hi=GetIcon("shell32.dll,16" 1)
pi
#2
what i want to do is to extract the icon from an exe
via contextmenu and save it as 24 bit png.
pi
#3
Using GflAx:

Code:
Copy      Help
int hi=GetIcon("shell32.dll,16" 1)

typelib GflAx {059321F1-207A-47A7-93A1-29CDF876FDD3} 1.0
GflAx.GflAx g._create
;g.EnableLZW=TRUE ;;enable for gif
g.NewBitmap(32 32 0xffffff)
stdole.IPicture p=g.GetPicture

int dc=CreateCompatibleDC(0)
int oldbm=SelectObject(dc p.Handle)
DrawIconEx dc 0 0 hi 32 32 0 0 3
SelectObject dc oldbm
DeleteDC dc

g.SetPicture(p)
g.SaveFormatName="png"
g.SaveBitmap(_s.expandpath("$desktop$\test.png"))

Using SaveBitmap:
Code:
Copy      Help
int hi=GetIcon("shell32.dll,16" 1)

int dc=CreateCompatibleDC(0)
int dc0=GetDC(0)
int bm=CreateCompatibleBitmap(dc0 32 32)
ReleaseDC(0 dc0)
int oldbm=SelectObject(dc bm)
RECT r; r.right=32; r.bottom=32
FillRect dc &r GetStockObject(0) ;;WHITE_BRUSH
DrawIconEx dc 0 0 hi 32 32 0 0 3
SelectObject dc oldbm
DeleteDC dc

SaveBitmap bm "$desktop$\test.png"
#4
how do i save it as transparent png or ico with using SaveBitmap?

i tried
FillRect dc &ir GetStockObject(NULL_BRUSH)

but then the png background is black.
pi
#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


Forum Jump:


Users browsing this thread: 2 Guest(s)