问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

VB如何编写BMP转换ICO的程序

发布网友 发布时间:2022-07-10 14:07

我来回答

1个回答

热心网友 时间:2022-07-12 22:23

按如下操作可以实现BMP、ICO互转。首先,建立如下工程picture控件:picImage picMaskbackcolor属性分别为黑色和白色其他四个picture控件从上到下,从左到右名称依次为默认值按键从左到右为Command1和Command2在form1中输入以下代码:Option ExplicitPrivate Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal X As Long, _
ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, _
ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
Private Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hdc As Long, _
ByVal nWidth As Long, ByVal nHeight As Long) As Long

Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As LongPrivate Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, _
ByVal hObject As Long) As Long

Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As LongPrivate Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As LongPrivate Declare Function CreateIconIndirect Lib "user32" (icoinfo As ICONINFO) As LongPrivate Declare Function OleCreatePictureIndirect Lib "olepro32.dll" (lppictDesc As _
pictDesc, riid As Guid, ByVal fown As Long, ipic As IPicture) As Long

Private Declare Function GetIconInfo Lib "user32" (ByVal hIcon As Long, _
icoinfo As ICONINFO) As Long

Private Declare Function SetBkColor Lib "gdi32" (ByVal hdc As Long, _
ByVal crColor As Long) As Long

Private Declare Function CreateBitmap Lib "gdi32" (ByVal nWidth As Long, ByVal nHeight _
As Long, ByVal nPlanes As Long, ByVal nBitCount As Long, lpBits As Any) As Long

Private Type ICONINFO
fIcon As Long
xHotspot As Long
yHotspot As Long
hBMMask As Long
hBMColor As Long
End TypePrivate Type Guid
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(7) As Byte
End TypePrivate Type pictDesc
cbSizeofStruct As Long
picType As Long
hImage As Long
xExt As Long
yExt As Long
End TypeConst PICTYPE_BITMAP = 1
Const PICTYPE_ICON = 3
Dim iGuid As Guid
Dim hdcMono
Dim bmpMono
Dim bmpMonoTemp
Const stdW = 32
Const stdH = 32
Dim mresult
Private Sub Form_Load()
hdcMono = CreateCompatibleDC(hdc)
bmpMono = CreateCompatibleBitmap(hdcMono, stdW, stdH)
bmpMonoTemp = SelectObject(hdcMono, bmpMono)
With iGuid
.Data1 = &H20400
.Data4(0) = &HC0
.Data4(7) = &H46
End With
End Sub
Private Sub command1_Click()
On Error Resume Next
Dim mtransp As Long
picImage.BackColor = Picture1.BackColor
mtransp = Picture1.Point(0, 0)
CreateTransparent Picture1, picImage, mtransp
CreateMask_viaMemoryDC picImage, picMask
mresult = BitBlt(Picture2.hdc, 0, 0, stdW, stdH, picMask.hdc, 0, 0, vbSrcAnd)
mresult = BitBlt(Picture2.hdc, 0, 0, stdW, stdH, picImage.hdc, 0, 0, vbSrcInvert)
BuildIcon Picture2
SavePicture Picture2.Picture, App.Path & "/Frombmp.ico"
End Sub
Private Sub command2_Click()
On Error Resume Next
Dim i, j
Dim p, q

Picture4.Picture = Picture3.Image
p = Picture4.Point(0, 0)
q = Me.BackColor
For i = 0 To stdW
For j = 0 To stdH
If Picture4.Point(i, j) = p Then
Picture4.PSet (i, j), q
End If
Next j
Next i

SavePicture Picture4.Picture, App.Path & "/Fromico.bmp"
End Sub
Private Function CreateMask_viaMemoryDC(Pic1 As PictureBox, Pic2 As PictureBox) As Boolean
On Error GoTo errHandler
CreateMask_viaMemoryDC = False
Dim dx As Long, dy As Long
Dim hdcMono2 As Long, bmpMono2 As Long, bmpMonoTemp2 As Long
dx = Pic1.ScaleWidth
dy = Pic1.ScaleHeight
hdcMono2 = CreateCompatibleDC(0)
If hdcMono2 = 0 Then
GoTo errHandler
End If
bmpMono2 = CreateCompatibleBitmap(hdcMono2, dx, dy)
bmpMonoTemp2 = SelectObject(hdcMono2, bmpMono2)
mresult = BitBlt(hdcMono2, 0, 0, dx, dy, Pic1.hdc, 0, 0, vbSrcCopy)
mresult = BitBlt(Pic2.hdc, 0, 0, dx, dy, hdcMono2, 0, 0, vbSrcCopy)
Call SelectObject(hdcMono2, bmpMonoTemp2)
Call DeleteDC(hdcMono2)
Call DeleteObject(bmpMono2)

CreateMask_viaMemoryDC = True
Exit Function
errHandler:
MsgBox "MakeMask_viaMemoryDC"
End Function
Private Sub ExtractIconComposite(inPic As PictureBox)
On Error Resume Next
Dim ipic As IPicture
Dim icoinfo As ICONINFO
Dim pDesc As pictDesc
Dim hDCWork
Dim hBMOldWork
Dim hNewBM
Dim hBMOldMono
GetIconInfo inPic.Picture, icoinfo
hDCWork = CreateCompatibleDC(0)
hNewBM = CreateCompatibleBitmap(inPic.hdc, stdW, stdH)
hBMOldWork = SelectObject(hDCWork, hNewBM)
hBMOldMono = SelectObject(hdcMono, icoinfo.hBMMask)
BitBlt hDCWork, 0, 0, stdW, stdH, hdcMono, 0, 0, vbSrcCopy
SelectObject hdcMono, hBMOldMono
SelectObject hDCWork, hBMOldWork
With pDesc
.cbSizeofStruct = Len(pDesc)
.picType = PICTYPE_BITMAP
.hImage = hNewBM
End With
OleCreatePictureIndirect pDesc, iGuid, 1, ipic
picMask = ipic
Set ipic = Nothing
pDesc.hImage = icoinfo.hBMColor
OleCreatePictureIndirect pDesc, iGuid, 1, ipic
picImage = ipic
DeleteObject icoinfo.hBMMask
DeleteDC hDCWork
Set hBMOldWork = Nothing
Set hBMOldMono = Nothing
End Sub
Private Sub BuildIcon(inPic As PictureBox)
On Error Resume Next
Dim hOldMonoBM
Dim hDCWork
Dim hBMOldWork
Dim hBMWork
Dim ipic As IPicture
Dim pDesc As pictDesc
Dim icoinfo As ICONINFO
BitBlt hdcMono, 0, 0, stdW, stdH, picMask.hdc, 0, 0, vbSrcCopy
SelectObject hdcMono, bmpMonoTemp
hDCWork = CreateCompatibleDC(0)
With inPic
hBMWork = CreateCompatibleBitmap(inPic.hdc, stdW, stdH)
End With
hBMOldWork = SelectObject(hDCWork, hBMWork)
BitBlt hDCWork, 0, 0, stdW, stdH, picImage.hdc, 0, 0, vbSrcCopy
SelectObject hDCWork, hBMOldWork
With icoinfo
.fIcon = 1
.xHotspot = 16
.yHotspot = 16
.hBMMask = bmpMono
.hBMColor = hBMWork
End With
With pDesc
.cbSizeofStruct = Len(pDesc)
.picType = PICTYPE_ICON
.hImage = CreateIconIndirect(icoinfo)
End With
OleCreatePictureIndirect pDesc, iGuid, 1, ipic
inPic.Picture = LoadPicture()
inPic = ipic
bmpMonoTemp = SelectObject(hdcMono, bmpMono)
DeleteObject icoinfo.hBMMask
DeleteDC hDCWork
Set hBMOldWork = Nothing
End Sub
Sub CreateTransparent(inpicSrc As PictureBox, inpicDest As PictureBox, _
inTrasparentColor As Long)
On Error Resume Next
Dim mMaskDC As Long
Dim mMaskBmp As Long
Dim mTempMaskBMP As Long
Dim mMonoBMP As Long
Dim mMonoDC As Long
Dim mTempMonoBMP As Long
Dim mSrcHDC As Long, mDestHDC As Long
Dim w As Long, h As Long
w = inpicSrc.ScaleWidth
h = inpicSrc.ScaleHeight
mSrcHDC = inpicSrc.hdc
mDestHDC = inpicDest.hdc
mresult = SetBkColor&(mSrcHDC, inTrasparentColor)
mresult = SetBkColor&(mDestHDC, inTrasparentColor)
mMaskDC = CreateCompatibleDC(mDestHDC)
mMaskBmp = CreateCompatibleBitmap(mDestHDC, w, h)
mTempMaskBMP = SelectObject(mMaskDC, mMaskBmp)
mMonoDC = CreateCompatibleDC(mDestHDC)
mMonoBMP = CreateBitmap(w, h, 1, 1, 0)
mTempMonoBMP = SelectObject(mMonoDC, mMonoBMP)
mresult = BitBlt(mMonoDC, 0, 0, w, h, mSrcHDC, 0, 0, vbSrcCopy)
mresult = BitBlt(mMaskDC, 0, 0, w, h, mMonoDC, 0, 0, vbSrcCopy)
mMonoBMP = SelectObject(mMonoDC, mTempMonoBMP)
mresult = DeleteObject(mMonoBMP)
mresult = DeleteDC(mMonoDC)
mresult = BitBlt(mDestHDC, 0, 0, w, h, mSrcHDC, 0, 0, vbSrcInvert)
mresult = BitBlt(mDestHDC, 0, 0, w, h, mMaskDC, 0, 0, vbSrcAnd)
BitBlt mDestHDC, 0, 0, w, h, mSrcHDC, 0, 0, vbSrcInvert
inpicDest.Picture = inpicDest.Image
mMaskBmp = SelectObject(mMaskDC, mTempMaskBMP)
mresult = DeleteObject(mMaskBmp)
mresult = DeleteDC(mMaskDC)
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
SelectObject bmpMono, bmpMonoTemp
DeleteObject bmpMono
DeleteDC hdcMono
End Sub
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
手机垃圾清理使用哪个软件最好? 适合新手用的手机清理软件 苹果手机怎么把号码黑名单里? VUE视频相机使用教程 教你简单几步拍出大片 如何使用佳能相机录像 相机录像具体操作教程分享 羊头捣蒜做法图解 服刑期间领取养老金是否构成犯罪 狗狗胰腺炎最怕的三种蔬菜是什么 怎么喂食 银行卡给专业的贷款公司做流水有风险吗? 电话号可直接贷款吗,他知道我的实名电话,还用paos机刷了我的银行卡一... 用C#写windows应用程序图片浏览器,利用pictureBox控件为何打开ico和gif格式图像为何报错?求各位高手教教 ln(int)+1/lnt的积分 如何给exe图标加载32位色的ico 金山毒霸里面一个红色叉叉叫什么unint.ico 32*32 acdsee ico图像点击后怎么出现好多麻烦事?关都来不及? 手机微信上的电话号码可以再电脑上打印出来吗 百无一用是书生出自何典故?有着什么意义? 鲁迅《杂感》赏析200字 仙佛茫茫两未成,只知独夜不平鸣。 风蓬飘尽悲歌气,泥絮沾来薄幸名。 十有九人堪白眼,百无一用是 杂感黄景仁感情赏析 黄景仁 (清)杂感 的解释? 我想请您帮我整理一下七年级上历史与社会的第二章地理部分的知识!谢谢 魔百和精准拓新优惠啥意思 拓新这两个的拼音 非励学无以笃志,非拓新无以致远什么意思 没有违反交通法开车意外撞死人怎么判 没有违反交通规则撞死人怎么处理 没有任何违规撞死人咋办 开车没有违章撞死人? 开车没有违规撞死人怎么处理 血参只有一种名字吗? c#怎么实现一个替换exe图标的功能,如qq.exe图标替换成酷狗的ico。 thinkpad x61 屏幕坏了,外接显示器是好的。重装系统后Fn+F7不能直接切换屏幕了。现在两个不显示了 “憧”字怎么读 憧怎么读了 空中瑜伽可以天天练吗 憧这个字读什么 憧的读音是什么 憧读什么 憧怎么读? 憧念什么字 考二建的作用大么,能做什么,没有经验如何备考? 丰田奕泽仪表盘如何设置油耗和里程?- 问一问 谁有 日本卡通鸭子操?(是一只*的小鸭子 ,要么可能是小鸡)那位大侠有 ,多谢了 有分值奖励哦。。。 小鸡洗了操怎么办 吃节操的小鸡叫什么 爱的生卵小鸡我的想法我的想法应该怎么写 英语,翻跟头怎么说 典范英语6 1翻译 欢呼声不断,非常热闹。英语 软件测试中,哪些项目活动能发现软件缺陷:()和()。