Code: Select all
Procedure.l SaveIcon(hIcon,file$)
Protected iconinfo.ICONINFO,hbmMask,hbmColor
Protected cbitmap.BITMAP,cwidth,cheight,cbitsperpixel,colorcount,colorplanes
Protected mbitmap.BITMAP,mwidth,mheight,ficon,xhotspot,yhotspot
Protected file,imagebytecount,hdc,oldbitmap,mem,bytesinrow,temp
Protected bitmapinfo.BITMAPINFO,bitmapinfoheader.BITMAPINFOHEADER
If Not(GetIconInfo_(hIcon,iconinfo)) : ProcedureReturn 0 : EndIf
ficon=2-iconinfo\fIcon : hbmMask=iconinfo\hbmMask : GetObject_(hbmMask,SizeOf(BITMAP),mbitmap)
mwidth=mbitmap\bmWidth : mheight=mbitmap\bmHeight
hbmColor=iconinfo\hbmColor
If hbmColor
GetObject_(hbmColor,SizeOf(BITMAP),cbitmap) : cwidth=cbitmap\bmWidth : cheight=cbitmap\bmHeight
cbitsperpixel=cbitmap\bmBitsPixel : If cbitsperpixel=0 : cbitsperpixel=1 : EndIf
If cbitsperpixel < 8 : colorcount=Pow(2,cbitsperpixel) : EndIf
colorplanes=cbitmap\bmplanes
Else
cwidth=mwidth : cheight=mheight/2 : cbitsperpixel=1 : colorcount=2 : colorplanes=1 : mheight=cheight
EndIf
file=CreateFile(#PB_Any,file$)
If file
WriteWord(file,0)
WriteWord(file,ficon)
WriteWord(file,1)
WriteByte(file,cwidth)
WriteByte(file,cheight)
WriteByte(file,colorcount)
WriteByte(file,0)
WriteWord(file,colorplanes)
WriteWord(file,cbitsperpixel)
WriteLong(file,0)
WriteLong(file,Loc(file)+4)
imagebytecount=SizeOf(BITMAPINFOHEADER)
WriteLong(file,imagebytecount)
WriteLong(file,cwidth)
WriteLong(file,cheight+mheight)
WriteWord(file,colorplanes)
WriteWord(file,cbitsperpixel)
WriteLong(file,0)
WriteLong(file,0)
WriteLong(file,0)
WriteLong(file,0)
WriteLong(file,0)
WriteLong(file,0)
hdc=CreateCompatibleDC_(0)
If hbmColor=0
WriteLong(file,#Black)
WriteLong(file,#White)
imagebytecount+SizeOf(rgbquad)*2
ElseIf cbitsperpixel<=8
temp=Pow(2,cbitsperpixel)
bytesinrow=SizeOf(rgbquad)*temp
mem=AllocateMemory(bytesinrow)
GetDIBColorTable_(hdc,0,temp,mem)
WriteData(file,mem,bytesinrow)
FreeMemory(mem)
imagebytecount+bytesinrow
EndIf
bytesinrow=(cwidth*cbitsperpixel+31)/32*4
bytesinrow * cheight
mem=AllocateMemory(bytesinrow)
bitmapinfo\bmiHeader\biSize=SizeOf(BITMAPINFOHEADER)
bitmapinfo\bmiHeader\biWidth=cwidth
bitmapinfo\bmiHeader\biPlanes=colorplanes
bitmapinfo\bmiHeader\biBitCount=cbitsperpixel
If hbmColor
bitmapinfo\bmiHeader\biHeight=cheight
GetDIBits_(hdc,hbmColor,0,cheight,mem,bitmapinfo,#DIB_RGB_COLORS)
Else
bitmapinfo\bmiHeader\biHeight=2*cheight
GetDIBits_(hdc,hbmMask,0,cheight,mem,bitmapinfo,#DIB_RGB_COLORS)
EndIf
WriteData(file,mem,bytesinrow)
FreeMemory(mem)
imagebytecount+bytesinrow
bytesinrow=(mwidth+31)/32*4
bytesinrow*mheight
mem=AllocateMemory(bytesinrow)
bitmapinfo\bmiHeader\biWidth=mwidth
bitmapinfo\bmiHeader\biPlanes=1
bitmapinfo\bmiHeader\biBitCount=1
If hbmColor
bitmapinfo\bmiHeader\biHeight=mheight
GetDIBits_(hdc,hbmMask,0,mheight,mem,bitmapinfo,#DIB_RGB_COLORS) ; ***** ERROR HERE *****
Else
bitmapinfo\bmiHeader\biHeight=2*mheight
GetDIBits_(hdc,hbmMask,mheight,mheight,mem,bitmapinfo,#DIB_RGB_COLORS)
EndIf
WriteData(file,mem,bytesinrow)
FreeMemory(mem)
imagebytecount+bytesinrow
DeleteDC_(hdc)
FileSeek(file,14)
WriteLong(file,imagebytecount)
CloseFile(file)
EndIf
DeleteObject_(hbmMask)
DeleteObject_(hbmColor)
ProcedureReturn file
EndProcedure