設(shè)為首頁收藏本站Access中國

Office中國論壇/Access中國論壇

 找回密碼
 注冊

QQ登錄

只需一步,快速開始

123下一頁
返回列表 發(fā)新帖
查看: 16379|回復(fù): 22
打印 上一主題 下一主題

[模塊/函數(shù)] 導(dǎo)出ImageList控件中的圖像

[復(fù)制鏈接]

點(diǎn)擊這里給我發(fā)消息

跳轉(zhuǎn)到指定樓層
1#
發(fā)表于 2013-7-16 16:37:17 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
本帖最后由 t小寶 于 2013-7-16 17:04 編輯

ImageList非Access自帶,是Activex控件,用于保存多個(gè)圖像,為TreeView、ListView等兄弟控件提供圖像引用。
有時(shí)候我們添加到ImageList控件的原始圖片找不到了,但I(xiàn)mageList中還有,怎么把它們導(dǎo)出來另作它用呢?
王站已經(jīng)開了一個(gè)好頭,在這里:http://m.mzhfr.cn/access/20130715/8063.html
ImageList控件允許兩種圖像格式:圖標(biāo)和位圖。用常規(guī)的SavePicture方法可以正常導(dǎo)出位圖,但導(dǎo)出圖標(biāo)會(huì)失真(因?yàn)樯钭兂?位)。
于是只能自己動(dòng)手豐衣足食,寫了一個(gè)模塊,實(shí)現(xiàn)正常導(dǎo)出圖標(biāo),還能夠自動(dòng)區(qū)分圖標(biāo)和位圖,分別導(dǎo)出。
要注意的是,從ImageList控件中獲取的圖標(biāo)丟失了原始色深(位深)信息,所以在代碼中設(shè)了一個(gè)參數(shù),可以手動(dòng)指定導(dǎo)出圖標(biāo)的色深,一般指定為24位(真彩色)即可。

模塊中的代碼:

  1. Private Type icondirentry
  2.     bwidth  As Byte
  3.     bheight  As Byte
  4.     bcolorcount  As Byte
  5.     breserved  As Byte
  6.     wplanes  As Integer
  7.     wbitcount  As Integer
  8.     dwbytesinres  As Long
  9.     dwimageoffset  As Long
  10. End Type
  11. Private Type icondir
  12.     idreserved As Integer
  13.     idtype As Integer
  14.     idcount As Integer
  15.     identries() As icondirentry
  16. End Type

  17. Private Type bitmap
  18.     bmType As Long
  19.     bmWidth As Long
  20.     bmHeight As Long
  21.     bmWidthBytes As Long
  22.     bmPlanes As Integer
  23.     bmBitsPixel As Integer
  24.     bmBits As Long
  25. End Type

  26. Private Type BITMAPINFOHEADER
  27.     biSize          As Long
  28.     biWidth         As Long
  29.     biHeight        As Long
  30.     biPlanes        As Integer
  31.     biBitCount      As Integer
  32.     biCompression   As Long
  33.     biSizeImage     As Long
  34.     biXPelsPerMeter As Long
  35.     biYPelsPerMeter As Long
  36.     biClrUsed       As Long
  37.     biClrImportant  As Long
  38. End Type

  39. Private Type RGBQUAD
  40.     b As Byte
  41.     G As Byte
  42.     r As Byte
  43.     a As Byte
  44. End Type

  45. Private Type BITMAPINFO
  46.     bmiHeader As BITMAPINFOHEADER
  47.     bmiColors(255) As RGBQUAD
  48. End Type

  49. Private Type ICONINFO
  50.     fIcon As Long
  51.     xHotspot As Long
  52.     yHotspot As Long
  53.     hbmMask As Long
  54.     hBMColor As Long
  55. End Type

  56. Private Declare Function GetIconInfo Lib "user32" (ByVal hIcon As Long, piconinfo As ICONINFO) As Long
  57. Private Declare Function GetObject Lib "gdi32" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long
  58. Private Declare Function GetDIBits Lib "gdi32" (ByVal aHDC As Long, ByVal hBitmap As Long, ByVal nStartScan As Long, ByVal nNumScans As Long, lpBits As Any, lpBI As BITMAPINFO, ByVal wUsage As Long) As Long
  59. Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hDc As Long) As Long
  60. Private Declare Function DeleteDC Lib "gdi32" (ByVal hDc As Long) As Long

  61. Private Const DIB_RGB_COLORS = 0
  62. Private Const BI_RGB = 0&


  63. '━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
  64. '  函數(shù)名稱: IcoToFile
  65. '  功能描述: 從圖標(biāo)句柄創(chuàng)建圖標(biāo)文件
  66. '  輸入?yún)?shù): hIcon  .......... 必選,圖標(biāo)句柄
  67. '            sFileName  ...... 必選,輸出文件名
  68. '            iBitsPixel  ..... 可選,圖標(biāo)的色深,2、4、8、16、24、32等值。如果不指定,自動(dòng)獲取色深
  69. '  返回參數(shù): 成功返回 True
  70. '  使用示例: IcoToFile hIcon, "C:\MyIcon.ico", 24  '從圖標(biāo)句柄hIcon創(chuàng)建圖標(biāo)文件,指定色深為24位,輸出到C:\MyIcon.ico
  71. '  作    者: t小寶 (QQ:377922812)
  72. '  創(chuàng)建日期: 20013-07-16
  73. '━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
  74. Public Function IcoToFile(ByVal hIcon As Long, ByVal sFileName As String, Optional iBitsPixel As Integer) As Boolean
  75. On Error GoTo Err_Handler

  76.     Dim tIconInfo As ICONINFO
  77.     Dim hBMColor As Long
  78.     Dim hbmMask As Long
  79.     Dim hMemDC As Long
  80.     Dim bm As bitmap
  81.     Dim tBitInfoAND As BITMAPINFO
  82.     Dim tBitInfoXOR As BITMAPINFO
  83.     Dim lBmiHeaderLen As Long
  84.     Dim lColorsLen As Long

  85.     Dim bytDataAND() As Byte
  86.     Dim bytDataXOR() As Byte
  87.     Dim tIconDir As icondir     ' 圖標(biāo)目錄結(jié)構(gòu)
  88.     Dim iFileNum As Integer
  89.    
  90.     Dim bytAnd() As Byte
  91.     Dim i As Long, j As Long, k As Long
  92.     Dim bBlank As Boolean
  93.     Dim byt0 As Byte, byt1 As Byte, byt2 As Byte, byt3 As Byte ' 檢查所有像素是否相同
  94.    
  95.     If Len(sFileName) = 0 Then Exit Function
  96.    
  97.     ' 從圖標(biāo)句柄獲取圖標(biāo)XOR位圖和AND位圖
  98.     GetIconInfo hIcon, tIconInfo
  99.     hBMColor = tIconInfo.hBMColor
  100.     hbmMask = tIconInfo.hbmMask

  101.     ' 獲取XOR位圖數(shù)據(jù)
  102.     lBmiHeaderLen = Len(tBitInfoXOR.bmiHeader)   '40
  103.     GetObject hBMColor, Len(bm), bm
  104.     If iBitsPixel <> 1 And iBitsPixel <> 4 And iBitsPixel <> 8 And iBitsPixel <> 16 _
  105.         And iBitsPixel <> 24 And iBitsPixel <> 32 Then iBitsPixel = bm.bmBitsPixel
  106.     If bm.bmWidth > 255 Or bm.bmHeight > 255 Then Exit Function                         ' 圖標(biāo)尺寸不能大于256*256

  107.     With tBitInfoXOR.bmiHeader
  108.         .biWidth = bm.bmWidth
  109.         .biHeight = bm.bmHeight
  110.         .biBitCount = iBitsPixel
  111.         .biCompression = BI_RGB
  112.         .biPlanes = 1
  113.         .biSize = lBmiHeaderLen
  114.         If .biBitCount = 8 Then
  115.             lColorsLen = 256 * 4
  116.         ElseIf .biBitCount = 4 Then
  117.             lColorsLen = 16 * 4
  118.         ElseIf .biBitCount = 1 Then
  119.             lColorsLen = 2 * 4
  120.         End If
  121.         ReDim bytDataXOR(((.biWidth * .biBitCount / 8 + 3) \ 4) * 4 * .biHeight - 1) As Byte        ' 設(shè)置數(shù)組大小與位圖數(shù)據(jù)一致
  122.     End With
  123.    
  124.     hMemDC = CreateCompatibleDC(0)                                                           ' 創(chuàng)建內(nèi)存設(shè)備場景
  125.     GetDIBits hMemDC, hBMColor, 0, bm.bmHeight, bytDataXOR(0), tBitInfoXOR, DIB_RGB_COLORS    ' 獲得位圖數(shù)據(jù)
  126.    
  127.     ' 獲取AND位圖數(shù)據(jù)
  128.     If hbmMask = 0 Then
  129.         tBitInfoAND.bmiHeader.biSizeImage = ((bm.bmWidth * 1 / 8 + 3) \ 4) * 4 * bm.bmHeight
  130.         ReDim bytDataAND(tBitInfoAND.bmiHeader.biSizeImage - 1) As Byte                         ' 設(shè)置數(shù)組大小與位圖數(shù)據(jù)一致
  131.     Else
  132.         GetObject hbmMask, Len(bm), bm
  133.         With tBitInfoAND.bmiHeader
  134.             .biWidth = bm.bmWidth
  135.             .biHeight = bm.bmHeight
  136.             .biBitCount = 1
  137.             .biCompression = BI_RGB
  138.             .biPlanes = 1
  139.             .biSize = lBmiHeaderLen
  140.         End With
  141.         ReDim bytDataAND(((bm.bmWidth * 1 / 8 + 3) \ 4) * 4 * bm.bmHeight - 1) As Byte        ' 設(shè)置數(shù)組大小與位圖數(shù)據(jù)一致
  142.         GetDIBits hMemDC, hbmMask, 0, bm.bmHeight, bytDataAND(0), tBitInfoAND, DIB_RGB_COLORS    ' 獲得位圖數(shù)據(jù)
  143.     End If

  144.     DeleteDC hMemDC

  145.     '處理圖標(biāo)目錄
  146.     ReDim tIconDir.identries(0)
  147.     tIconDir.idreserved = 0                             ' 保留字,必須為0
  148.     tIconDir.idtype = 1                             ' 1為圖標(biāo),0為光標(biāo)
  149.     tIconDir.idcount = 1                             ' 圖像個(gè)數(shù)
  150.     With tIconDir.identries(0)
  151.         .bwidth = tBitInfoXOR.bmiHeader.biWidth
  152.         .bheight = tBitInfoXOR.bmiHeader.biHeight
  153.         .bcolorcount = 0
  154.         .breserved = 0
  155.         .wplanes = 1                                                              ' 不設(shè)也沒有影響
  156.         .wbitcount = tBitInfoXOR.bmiHeader.biBitCount                             ' 每個(gè)像素的位數(shù),不設(shè)也沒有影響
  157.         .dwbytesinres = lBmiHeaderLen + lColorsLen + _
  158.             tBitInfoXOR.bmiHeader.biSizeImage + tBitInfoAND.bmiHeader.biSizeImage '
  159.         .dwimageoffset = 22                                                       ' 圖像數(shù)據(jù)偏移起點(diǎn),第1個(gè)圖像是22
  160.     End With
  161.    
  162.     'XOR位圖信息頭兩個(gè)成員須要調(diào)整
  163.     With tBitInfoXOR.bmiHeader
  164.         .biSizeImage = .biSizeImage + tBitInfoAND.bmiHeader.biSizeImage
  165.         .biHeight = .biHeight * 2
  166.     End With

  167.     '創(chuàng)建文件 寫入圖標(biāo)數(shù)據(jù)
  168.     iFileNum = FreeFile
  169.     Open sFileName For Output As #iFileNum
  170.     Close #iFileNum
  171.     Open sFileName For Binary As #iFileNum
  172.     Put #iFileNum, , tIconDir.idreserved
  173.     Put #iFileNum, , tIconDir.idtype
  174.     Put #iFileNum, , tIconDir.idcount
  175.     Put #iFileNum, , tIconDir.identries(0)                             ' icondirentry 圖標(biāo)目錄
  176.     Put #iFileNum, , tBitInfoXOR.bmiHeader                             ' XOR位圖頭
  177.     If lColorsLen > 0 Then Put #iFileNum, , tBitInfoXOR.bmiColors      ' XOR位圖顏色表
  178.     Put #iFileNum, , bytDataXOR                                        ' XOR位圖數(shù)據(jù)
  179.     Put #iFileNum, , bytDataAND                                        ' AND位圖數(shù)據(jù)
  180.     Close #iFileNum
  181.     IcoToFile = True
  182.    
  183. Err_Handler:
  184.     Exit Function
  185. End Function
復(fù)制代碼
在窗體上添加一個(gè)ImageList控件,插入一些圖標(biāo)和位圖,添加一個(gè)命令按鈕,窗體模塊中添加以下代碼:
  1. Private Sub Command1_Click()

  2.     Dim pic As IPictureDisp
  3.     Dim i As Integer
  4.    
  5.     For i = 1 To Me.ImageList0.ListImages.Count
  6.         '.ListImages(i).ExtractIcon:返回的總是圖標(biāo) '.Overlay(i,i):返回的總是位圖
  7.         Set pic = Me.ImageList0.ListImages(i).Picture
  8.         
  9.         ' 3是圖標(biāo),1是位圖
  10.         If pic.Type = 3 Then
  11.             '這里圖標(biāo)的色深是當(dāng)前屏幕色深,不是原始圖標(biāo)的色深,所以在最后一個(gè)參數(shù)指定色深。用LoadPicture加載的圖片可以取得原始色深。
  12.             IcoToFile pic.handle, "C:\ImageList" & i & ".ico", 24
  13.         Else
  14.             SavePicture pic, "C:\ImageList" & i & ".bmp"
  15.         End If
  16.         Set pic = Nothing
  17.     Next
  18.     MsgBox "已導(dǎo)出到c盤根目錄。"
  19.    
  20. End Sub
復(fù)制代碼

示例如下:

游客,如果您要查看本帖隱藏內(nèi)容請回復(fù)


本帖子中包含更多資源

您需要 登錄 才可以下載或查看,沒有帳號?注冊

x

評分

參與人數(shù) 2經(jīng)驗(yàn) +20 收起 理由
roych + 10 很給力!
魚兒游游 + 10 很給力!

查看全部評分

本帖被以下淘專輯推薦:

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享分享 分享淘帖1 訂閱訂閱

點(diǎn)擊這里給我發(fā)消息

2#
發(fā)表于 2013-7-16 17:13:14 | 只看該作者
好貼, 用小寶的工具解決了我的圖片的導(dǎo)出問題. 且圖片不失真.
小寶對圖片的處理 的確很到位
3#
發(fā)表于 2013-7-16 17:32:33 | 只看該作者
seesee
回復(fù)

使用道具 舉報(bào)

點(diǎn)擊這里給我發(fā)消息

4#
發(fā)表于 2013-7-16 18:01:51 | 只看該作者
{:soso_e163:}感謝分享!!

點(diǎn)擊這里給我發(fā)消息

5#
發(fā)表于 2013-7-16 18:41:28 | 只看該作者
頂一個(gè)!
回復(fù)

使用道具 舉報(bào)

6#
發(fā)表于 2013-7-16 20:59:26 | 只看該作者
謝謝分享
回復(fù)

使用道具 舉報(bào)

點(diǎn)擊這里給我發(fā)消息

7#
 樓主| 發(fā)表于 2013-7-16 21:17:08 | 只看該作者
模塊代碼中有些變量聲明忘記刪了{(lán):soso_e101:}
8#
發(fā)表于 2013-7-18 06:43:25 | 只看該作者
感謝分享!!
回復(fù)

使用道具 舉報(bào)

9#
發(fā)表于 2013-7-18 10:03:22 | 只看該作者
感謝分享!
回復(fù)

使用道具 舉報(bào)

10#
發(fā)表于 2013-7-18 11:15:52 | 只看該作者
學(xué)習(xí)一下
回復(fù)

使用道具 舉報(bào)

您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則

QQ|站長郵箱|小黑屋|手機(jī)版|Office中國/Access中國 ( 粵ICP備10043721號-1 )  

GMT+8, 2025-7-13 05:25 , Processed in 0.103614 second(s), 38 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回復(fù) 返回頂部 返回列表