[已解决]图片二进制调用还能用的别方式么
本帖最后由 haijie1223 于 2012-3-30 22:25 编辑图片二进制调用还能用的别方式么,虽然没有文件释放,但是感觉这样调用好麻烦。
#include <APIConstants.au3>
#include <GDIPlus.au3>
#include <Memory.au3>
#include <WinAPIEx.au3>
Opt('MustDeclareVars', 1)
Global Const $STM_SETIMAGE = 0x0172
Global Const $STM_GETIMAGE = 0x0173
Global $hForm, $Pic, $hPic, $hBitmap, $hObj, $hImage, $pStream, $bData, $hData, $pData, $tData, $Width, $Height, $Lenght
; 创建位图 (MSDNLogo.png)
$bData = _Image_MSDNLogo()
$Lenght = BinaryLen($bData)
$hData = _MemGlobalAlloc($Lenght, $GMEM_MOVEABLE)
$pData = _MemGlobalLock($hData)
$tData = DllStructCreate('byte[' & $Lenght & ']', $pData)
DllStructSetData($tData, 1, $bData)
_MemGlobalUnlock($hData)
$pStream = _WinAPI_CreateStreamOnHGlobal($hData)
_GDIPlus_Startup()
$hImage = _GDIPlus_BitmapCreateFromStream($pStream)
$hBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage)
$Width = _GDIPlus_ImageGetWidth($hImage)
$Height = _GDIPlus_ImageGetHeight($hImage)
_WinAPI_ReleaseStream($pStream)
_GDIPlus_ImageDispose($hImage)
_GDIPlus_Shutdown()
; 创建 GUI
$hForm = GUICreate('MyGUI', $Width, $Height)
$Pic = GUICtrlCreatePic('', 0, 0, $Width, $Height)
$hPic = GUICtrlGetHandle($Pic)
; 设置位图到控件
_SendMessage($hPic, $STM_SETIMAGE, 0, $hBitmap)
$hObj = _SendMessage($hPic, $STM_GETIMAGE)
If $hObj <> $hBitmap Then
_WinAPI_DeleteObject($hBitmap)
EndIf
GUISetState()
Do
Until GUIGetMsg() = -3
Func _GDIPlus_BitmapCreateFromStream($pStream)
Local $Ret = DllCall($ghGDIPDll, 'uint', 'GdipCreateBitmapFromStream', 'ptr', $pStream, 'ptr*', 0)
If (@error) Or ($Ret) Then
Return SetError(@error, @extended, 0)
EndIf
Return $Ret
EndFunc ;==>_GDIPlus_BitmapCreateFromStream
Func _Image_MSDNLogo()
Local $bImage = _
'0xFFD8FFE000104A46494600010100000100010000FFDB00840005030404' & _
'04030504040405050506070C08070707070F0B0B090C110F1212110F1111' & _
'13161C1713141A1511111821181A1D1D1F1F1F13172224221E241C1E1F1E' & _
'010505050706070E08080E1E1411141E1E1E1E1E1E1E1E1E1E1E1E1E1E1E' & _
'1E1E1E1E1E1E1E1E1E1E1E1E1E1E1E1E1E1E1E1E1E1E1E1E1E1E1E1E1E1E' & _
'1E1E1E1E1EFFC00011080030003003011100021101031101FFC401A20000' & _
'010501010101010100000000000000000102030405060708090A0B100002' & _
'010303020403050504040000017D01020300041105122131410613516107' & _
'227114328191A1082342B1C11552D1F02433627282090A161718191A2526' & _
'2728292A3435363738393A434445464748494A535455565758595A636465' & _
'666768696A737475767778797A838485868788898A92939495969798999A' & _
'A2A3A4A5A6A7A8A9AAB2B3B4B5B6B7B8B9BAC2C3C4C5C6C7C8C9CAD2D3D4' & _
'D5D6D7D8D9DAE1E2E3E4E5E6E7E8E9EAF1F2F3F4F5F6F7F8F9FA01000301' & _
'01010101010101010000000000000102030405060708090A0B1100020102' & _
'040403040705040400010277000102031104052131061241510761711322' & _
'328108144291A1B1C109233352F0156272D10A162434E125F11718191A26' & _
'2728292A35363738393A434445464748494A535455565758595A63646566' & _
'6768696A737475767778797A82838485868788898A92939495969798999A' & _
'A2A3A4A5A6A7A8A9AAB2B3B4B5B6B7B8B9BAC2C3C4C5C6C7C8C9CAD2D3D4' & _
'D5D6D7D8D9DAE2E3E4E5E6E7E8E9EAF2F3F4F5F6F7F8F9FAFFDA000C0301' & _
'0002110311003F00E0FF006B0F166A97DF11EF7C331DD347A5E96230B144' & _
'D8592468D5CBB63A91BB68CF4C1C60939F5AB56937CB7D0E2A34D25CC78B' & _
'249B5B2501359C24BB1B3573674A17D7840B7B0926E71BD109E723A9ED5A' & _
'29AB91C8CF40D37E29DCF863C392E897D6125FEA36EDE5C3E64802C49B4F' & _
'CAC4649DAD8C0F424646D15DF4332950872DAFD8E5A984E795CCDF863E2B' & _
'F106B3E32B58A49C25AE1DE745803208C0C95231D0F039EE41EB5B50C7E2' & _
'F178A8C5CB4BECBFAF92B9954C3D2A14DB48B7A9F33C847763DF3DEBF438' & _
'6C8F053D44FDA52DE797E2DDFCC90737B14332907EF011AA67DB943F957E' & _
'578BA4E9CEDDCFA8C34B9A3B9C75B697656282E2F9D242307693C6323B77' & _
'EF5953A6BA9D4ED124BBBEBBD523FB15BDBF9364872A36F5380320745E84' & _
'F1EB58D5AD18E899D10A339181A8585D594A12E2174DFCA92386159C2A46' & _
'4B432AB4A54DD9A3D4FE142C5A0E9BA8C1A86952C17F232992695325A2E0' & _
'88D79E390C4FFC07AF15F4FC351A559CDC5FBCBF2FF87FD0F23368D4A3CB' & _
'CCB466E45E18D4B56F11DBE9D676135B8BEB958626941D9196E70CC0765E' & _
'48C6700F15F6553174E8D17394AF647890A339CD452DCA7F1E35BD1FFB5A' & _
'2B3D92FF00684712999F70CE7E62147CA0807703D481CE39E2BE17367155' & _
'D2BEC8F732FBB83D37388F07D9DAEB57C90DC24C8EBCC719FBA4FB64E493' & _
'5F398CAD28C5DB63E8703423297BCB53B2B4F056A771AB44B05948E43E14' & _
'0CA2A027A614124F19C9207D2BCEFACAE53D29507CC7B00F873A3A595A9D' & _
'62D62B99612B229C11B580FAFF003EB5C31AD24DD99A4E9A6B5353C3567E' & _
'13D37C7FA4E9D7B7905F5F24734F6F0798EEF6CCAD14918625B3F7519B07' & _
'1C647208AF5F269D58559F2BB292B3F33CCCDB9274A2A4B54EE7A6CB269F' & _
'14A6EAE6D826D99EE2391D46226208C8CF4F94E3E9C57D0A8CE4B953ECBD' & _
'4F0AE96ACF8A744F827F173C4FA9DC6A573E1EB8B4915CCD713EA8C2039C' & _
'1607CB6F9D8123036A919E3B1C723BD59FBF2F8BAFFC31A26A11F756C7A6' & _
'787BE18D8683ABC7A8EAE0BDFACE630A4FFCB4DD83CFA0E7A71815F3788A' & _
'F37CD49F43EA30F4E165522777A9EBDA6585FCADA43CD0470C425B8B9923' & _
'5100C8E14E483BB38F4E3BE6B8E34DD8EC93B9E61F12FE31DC205B5D28F9' & _
'97A57E790C5810F27A03FC5FCBF975E1F0326EF3D8E2C462A31568EE735F' & _
'053E205FE87E30F36EAE564B7D4E648AF1EEA4C0059B0242DDB6EE24FB67D' & _
'88F730D3F6324BA33C8C443DB45B7BF43E89F1FDDDDFF00C23F745751FDE79' & _
'788BCB6C6D6CF1D7F5AFA8C23A54EA294D24BAB6783554E71718EAFC8B93F88' & _
'7C4D269D308E59CDA823748B1F0BDB19E833FD6B68E0F08A6AE95FD4E6788ACE' & _
'2EDB1E7FE35D7F48D2FEC175AE5F986E24B92620CF8F318E771181D3E639278E47' & _
'23A1F9CE24CBA1CCAB526B9B66BAD975FB8F7B22C7C945D29AD3A3ECFB1CAFC66F1' & _
'0C3A6E8768BA7DD472B5E0386903B3C5800E41624293BBF840E9E95F2B83A4E73D7A1' & _
'F538AC6F252B456ACF02B999A42CEEC0B31EFD4D7B491F3CE77DCBFA0DBC97138632AA2' & _
'C60ECCF3F37AD394ACB635A51727E476D16A7A81B58D2EF50B8B8448C46A1E4E1147185' & _
'1DB1E9593973347641282BF73EB2B3D42D348D3A59ACECA59EF1E1652A2E36B371F741C00B' & _
'92073CE3D6BEAB114EAD67ABD16C7C8529429EC7C63F15751F1A78835B7D73C5D61A8C0F28F2A' & _
'113DBC91C5146BD238C30E1467DCE492492493E04E954527292D4F662E9C972C1A33FC2CB3788D' & _
'C787EEEFE7DC919FB20932EA08CFCBCF4EB5C55FF0070BDAC57A9DB867ED9FB29BF439DBCB1B9B4D' & _
'5A5B1B852268642922E73820E2BAE9CE3520A51D99C95294A351C1F43A7D2C48B163088320671822B' & _
'2BADCEDA68D1C177542CF267009C9181ED5375B9A1FFD9'
Return Binary($bImage)
EndFunc ;==>_Image_MSDNLogo
这么有艺术的进制,有点意思 写成函数调用,很简单的 回复 3# afan
如果a版有时间的话,麻烦a版写个例子看下呗。 回复 4# haijie1223
把界面部分 (27 - 40 行)拿出来,pic 控件句柄和图片二进制部分作为参数,其它部分封装起来就差不多了 学习一西.... 学习一下 多谢楼主 同 afan 学习一下 多谢楼主 同 afan 支持互转吗? 终于搞定我的图片了谢谢你 回复 1# haijie1223
老大 這篇文章 有沒有後續呀有沒有UDF 可以應用
因為我 最近需要從資料庫裡面直接讀取 2進制的圖片資料 需要顯示在GUI 上
而且 需要以一定的比例 放大縮小
上面的範例 似乎 沒有 放大或者縮小的功能
這該如何處理呢?? 回复 13# kk_lee69
把文件读取到二进制这个应该不难,你自己找下方法,至于放大缩小,刚刚搜到一个。你测试一下吧。
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GDIPlus.au3>
#include <winapi.au3>
_GDIPlus_Startup()
Local $msg
Global Const $STM_SETIMAGE = 0x0172
Global Const $IMAGE_BITMAP = 0
$hImage = _GDIPlus_ImageLoadFromFile(@ScriptDir & '\test.jpg')
$iX_ImageDisplay = _GDIPlus_ImageGetWidth($hImage)
$iY_ImageDisplay = _GDIPlus_ImageGetHeight($hImage)
ConsoleWrite($iX_ImageDisplay & " x "& $iY_ImageDisplay & @CRLF)
$iFactor_ImageDisplay = 1
If $iX_ImageDisplay > @DesktopWidth Or $iY_ImageDisplay > @DesktopHeight Then
$iX_ImageDisplay = $iX_ImageDisplay * (@DesktopHeight / $iY_ImageDisplay)
$iFactor_ImageDisplay = @DesktopHeight / $iY_ImageDisplay
$iY_ImageDisplay = @DesktopHeight
If $iX_ImageDisplay > @DesktopWidth Then
$iY_ImageDisplay = $iY_ImageDisplay * (@DesktopWidth / $iX_ImageDisplay)
$iFactor_ImageDisplay = @DesktopWidth / $iX_ImageDisplay
$iX_ImageDisplay = @DesktopWidth
EndIf
EndIf
$iX_ImageDisplay = Int($iX_ImageDisplay)
$iY_ImageDisplay = Int($iY_ImageDisplay)
$gui_image_display = GUICreate("My GUI", $iX_ImageDisplay, $iY_ImageDisplay, Default, Default, $WS_OVERLAPPEDWINDOW); will create a dialog box that when displayed is centered
$pic_image_display = GUICtrlCreatePic("", 0, 0, $iX_ImageDisplay, $iY_ImageDisplay)
;If $iFactor_ImageDisplay <> 1 Then
; $hGraphic = _GDIPlus_GraphicsCreateFromHWND(GUICtrlGetHandle($pic_image_display))
; _GDIPlus_GraphicsDrawImageRect($hGraphic, $hImage, 0, 0, $iX_ImageDisplay, $iY_ImageDisplay)
ConsoleWrite($iX_ImageDisplay & @TAB & $iY_ImageDisplay & @CRLF)
;EndIf
$hBMP = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage)
_WinAPI_DeleteObject(GUICtrlSendMsg($pic_image_display, $STM_SETIMAGE, $IMAGE_BITMAP, $hBMP))
;$aBmp = DllCall("user32.dll", "hwnd", "SendMessage", "hwnd", GUICtrlGetHandle($pic_image_display), "int", $STM_SETIMAGE, "int", $IMAGE_BITMAP, "int", $hBMP)
;_WinAPI_RedrawWindow($gui_image_display, "", "", BitOR($RDW_INVALIDATE, $RDW_UPDATENOW, $RDW_FRAME))
;If $aBmp <> 0 Then _WinAPI_DeleteObject($aBmp)
_GDIPlus_ImageDispose($hImage)
_WinAPI_DeleteObject($hBMP)
;If $iFactor_ImageDisplay <> 1 Then
; _GDIPlus_GraphicsDispose($hGraphic)
;EndIf
_GDIPlus_Shutdown()
GUICtrlSetPos($pic_image_display, 0, 0, $iX_ImageDisplay, $iY_ImageDisplay)
GUIRegisterMsg(522, "_ResizePic"); WM_MOUSEWHEEL
GUISetState(@SW_SHOW); will display an empty dialog box
; Run the GUI until the dialog is closed
While 1
$msg = GUIGetMsg()
If $msg = $GUI_EVENT_CLOSE Then ExitLoop
WEnd
GUIDelete()
Func _ResizePic($hWnd, $iMsg, $wParam, $lParam)
If BitShift($wParam, 16) > 0 Then
$iX_ImageDisplay *= 1.1
$iY_ImageDisplay *= 1.1
Else
$iX_ImageDisplay /= 1.1
$iY_ImageDisplay /= 1.1
EndIf
GUICtrlSetPos($pic_image_display, 0, 0, $iX_ImageDisplay, $iY_ImageDisplay)
EndFunc;==>_ResizePic 这个图片不能放大缩小,只是外框而已 好久没上来学习了。强悍! 试一下,感谢分享
页:
[1]
2