如何把一个256色的bmp图像转成ico格式?(100分)

  • 主题发起人 主题发起人 7030
  • 开始时间 开始时间
7

7030

Unregistered / Unconfirmed
GUEST, unregistred user!
查阅了以前的大富翁资料,也没有完整答案能解决此题
 
用delphi自带的image editor不行吗?
 
function BitmapToIcon(Bitmap: TBitmap): TIcon;<br>var<br>&nbsp; IconSizeX, IconSizeY : integer;<br>&nbsp; IconInfo: TIconInfo;<br>&nbsp; IconBitmap, MaskBitmap: TBitmap;<br>&nbsp; x, y: Integer;<br>&nbsp; TransparentColor: TColor;<br>begin<br>&nbsp; IconSizeX := GetSystemMetrics(SM_CXICON);<br>&nbsp; IconSizeY := GetSystemMetrics(SM_CYICON);<br>&nbsp; IconBitmap:= TBitmap.Create;<br>&nbsp; IconBitmap.Width:= IconSizeX;<br>&nbsp; IconBitmap.Height:= IconSizeY;<br>&nbsp; IconBitmap.Canvas.StretchDraw(Rect(0, 0, IconSizeX, IconSizeY), Bitmap);<br>&nbsp; IconBitmap.TransparentColor:= Bitmap.TransparentColor;<br>&nbsp; TransparentColor:= IconBitmap.TransparentColor and $FFFFFF;<br>&nbsp; MaskBitmap:= TBitmap.Create;<br>&nbsp; MaskBitmap.Assign(IconBitmap);<br>&nbsp; for y:= 0 to IconSizeY - 1 do<br>&nbsp; &nbsp; for x:= 0 to IconSizeX - 1 do<br>&nbsp; &nbsp; &nbsp; if IconBitmap.Canvas.Pixels[x, y] = TransparentColor then<br>&nbsp; &nbsp; &nbsp; &nbsp; IconBitmap.Canvas.Pixels[x, y]:= clBlack;<br>&nbsp; IconInfo.fIcon:= True;<br>&nbsp; IconInfo.hbmMask:= MaskBitmap.MaskHandle;<br>&nbsp; IconInfo.hbmColor:= IconBitmap.Handle;<br>&nbsp; Result:= TIcon.Create;<br>&nbsp; Result.Handle:= CreateIconIndirect(IconInfo);<br>&nbsp; MaskBitmap.Free;<br>&nbsp; IconBitmap.Free;<br>end;<br>这个也是查从前的答案,解决不了吗???
 
一年前的另一个答案<br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; IconSizeX: integer;<br>&nbsp; IconSizeY: integer;<br>&nbsp; AndMask &nbsp;: TBitmap;<br>&nbsp; XorMask &nbsp;: TBitmap;<br>&nbsp; IconInfo : TIconInfo;<br>&nbsp; Icon &nbsp; &nbsp; : TIcon;<br>begin<br>&nbsp; {Get the icon size}<br>&nbsp; IconSizeX := GetSystemMetrics(SM_CXICON);<br>&nbsp; IconSizeY := GetSystemMetrics(SM_CYICON);<br><br>&nbsp; {Create the "and" mask}<br>&nbsp; AndMask := TBitmap.Create;<br>&nbsp; AndMask.Monochrome := true;<br>&nbsp; AndMask.Width := IconSizeX;<br>&nbsp; AndMask.Height := IconSizeY;<br><br>&nbsp; {Draw on the "and" mask}<br>&nbsp; AndMask.Canvas.Brush.Color := clWhite;<br>&nbsp; AndMask.Canvas.FillRect(rect(0, 0, IconSizeX, IconSizeY));<br>&nbsp; AndMask.Canvas.Brush.Color := clblack;<br>&nbsp; AndMask.Canvas.ELLIPSE(4, 4, IconSizeX-4, IconSizeY-4);<br><br>&nbsp; {Draw as a test}<br>&nbsp; Form1.Canvas.Draw(IconSizeX*2, IconSizeY, AndMask);<br><br>&nbsp; {Create the "xor" mask}<br>&nbsp; XorMask := TBitmap.Create;<br>&nbsp; XorMask.Width := IconSizeX;<br>&nbsp; XorMask.Height := IconSizeY;<br><br>&nbsp; {Draw on the "xor" mask}<br>&nbsp; XorMask.Canvas.Brush.Color := clblack;<br>&nbsp; XorMask.Canvas.FillRect(rect(0, 0, IconSizeX, IconSizeY));<br>&nbsp; XorMask.Canvas.Pen.Color := clRed;<br>&nbsp; XorMask.Canvas.Brush.Color := clRed;<br>&nbsp; XorMask.Canvas.ELLIPSE(4, 4, IconSizeX-4, IconSizeY-4);<br><br>&nbsp; {Draw as a test}<br>&nbsp; Form1.Canvas.Draw(IconSizeX*4, IconSizeY, XOrMask);<br><br>&nbsp; {Create a icon}<br>&nbsp; Icon := TIcon.Create;<br>&nbsp; IconInfo.fIcon := true;<br>&nbsp; IconInfo.xHotspot := 0;<br>&nbsp; IconInfo.yHotspot := 0;<br>&nbsp; IconInfo.hbmMask := AndMask.Handle;<br>&nbsp; IconInfo.hbmColor := XOrMask.Handle;<br>&nbsp; Icon.Handle := CreateIconIndirect(IconInfo);<br><br>&nbsp; {Destroy the temporary bitmaps}<br>&nbsp; AndMask.Free;<br>&nbsp; XorMask.Free;<br><br>&nbsp; {Draw as a test}<br>&nbsp; Form1.Canvas.Draw(IconSizeX*6, IconSizeY, Icon);<br><br>&nbsp; {Assign the application icon}<br>&nbsp; Application.Icon := Icon;<br><br>&nbsp; {Force a repaint}<br>&nbsp; InvalidateRect(Application.Handle, nil, true);<br><br>&nbsp; {Free the icon}<br>&nbsp; Icon.Free;<br>
 
app2001:可能没说清楚,我想最终保存成文件,你的方法是不能保存成256色图标的,而且对48*48的bmp图像转换会有问题
 
谁能给出个答案?<br><br>
 
多人接受答案了。
 

Similar threads

后退
顶部