GDI+ Rect效果偏差 [已解决]
本帖最后由 republican 于 2010-8-18 08:02 编辑我反反复复在调试,还是不太清楚究竟是不是GDI+的问题,遂发帖询问,以下代码及附件,放在同一个目录,就能查看差别了。#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GDIPlus.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 623, 446, 192, 124)
$Pic1 = GUICtrlCreatePic("", 48, 40, 380, 158)
_ImageResize("BKG-Body.jpg",380,158)
;~ _ImageResize("BKG-Body.jpg",380,158,1,4) ;修正偏差
GUICtrlCreatePic("BKG-Body.jpg", 48, 230, 380, 158)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
EndSwitch
WEnd
Func _ImageResize($sInImage, $iW, $iH,$WFixed=0,$HFixed=0)
Local $hWnd, $hDC, $hBMP, $hImage1, $hImage2, $hGraphic, $CLSID, $i = 0,$hBitmap,$w_img,$h_img
$hWnd = _WinAPI_GetDesktopWindow()
$hDC = _WinAPI_GetDC($hWnd)
$hBMP = _WinAPI_CreateCompatibleBitmap($hDC, $iW, $iH)
_WinAPI_ReleaseDC($hWnd, $hDC)
;Start GDIPlus
_GDIPlus_Startup()
;Get the handle of blank bitmap you created above as an image
$hImage1 = _GDIPlus_BitmapCreateFromHBITMAP ($hBMP)
;Load the image you want to resize.
$hImage2 = _GDIPlus_ImageLoadFromFile($sInImage)
$w_img = _GDIPlus_ImageGetWidth($hImage2)
$h_img = _GDIPlus_ImageGetHeight($hImage2)
ConsoleWrite($w_img &" "&$h_img)
;~ MsgBox(0,"",$hImage2)
;~ $hBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage2)
;Get the graphic context of the blank bitmap
$hGraphic = _GDIPlus_ImageGetGraphicsContext ($hImage1)
;Draw the loaded image onto the blank bitmap at the size you want
;~ _GDIPLus_GraphicsDrawImageRect($hGraphic, $hImage2, 0, 0, $iW, $ih)
_GDIPlus_GraphicsDrawImageRectRect($hGraphic, $hImage2,0,0,$w_img,$h_img,0,0,$iW+$WFixed, $iH+$HFixed)
_SetBitmapToCtrl($Pic1, _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage1))
ConsoleWrite(_GDIPlus_ImageGetWidth($hImage1) &" "&_GDIPlus_ImageGetHeight($hImage1))
_GDIPlus_ImageDispose($hImage2)
_GDIPlus_GraphicsDispose ($hGraphic)
_WinAPI_DeleteObject($hBMP)
_GDIPlus_Shutdown()
Return $hImage1
EndFunc
Func _SetBitmapToCtrl($CtrlId, $hBitmap)
Local Const $STM_SETIMAGE = 0x0172
Local Const $IMAGE_BITMAP = 0
Local Const $SS_BITMAP = 0xE
Local Const $GWL_STYLE = -16
Local $hWnd = GUICtrlGetHandle($CtrlId)
If $hWnd = 0 Then Return SetError(1, 0, 0)
; set SS_BITMAP style to control
Local $oldStyle = DllCall("user32.dll", "long", "GetWindowLong", "hwnd", $hWnd, "int", $GWL_STYLE)
If @error Then Return SetError(2, 0, 0)
DllCall("user32.dll", "long", "SetWindowLong", "hwnd", $hWnd, "int", $GWL_STYLE, "long", BitOR($oldStyle, $SS_BITMAP))
If @error Then Return SetError(3, 0, 0)
Local $oldBmp = DllCall("user32.dll", "hwnd", "SendMessage", "hwnd", $hWnd, "int", $STM_SETIMAGE, "int", $IMAGE_BITMAP, "int", $hBitmap)
If @error Then Return SetError(4, 0, 0)
If $oldBmp <> 0 Then _WinAPI_DeleteObject($oldBmp)
Return 1
EndFunc
素材: 请另存为BKG-Body.jpg
效果差异:
代码在8楼. gdi的放大的时候用的算法 和 GUICtrlCreatePic创建的控件用的算法不同吧...? 估计是的,我现在觉得GUICtrlCreatePic的算法很好。
我现在想达到createpic的效果。
因为仔细看GDI+的效果,连颜色都有偏差,不得不放弃. 可恨...
http://social.msdn.microsoft.com/Search/zh-CN/?Query=GdipDrawImageRectRectI
居然失效了... 我看看.. 这个是什么呀,没看懂呢 回复 4# rolaka
感谢帮忙! ..只是简单查了下...
好像没有定义这一块的东西. 本帖最后由 republican 于 2010-8-18 08:02 编辑
回复 7# rolaka
用了CopyImage的API,感觉不错,就这样吧~~呵呵。
我把关键代码写成UDF了...该段代码适用于文件及资源,不再需要resource.au3了。
;文件及资源的对应关系
#AutoIt3Wrapper_Res_File_Add=BKG-Body.bmp, rt_bitmap, BKG-Body
Func _SetBitMapToCtrlEx($CtrlID,$sInImage, $iW, $iH)
Local $hBitmap, $hImage
Local $sMode=0
$hImage=_LoadBitMapToHImage($sInImage,$iW,$iH)
If @error Then Return SetError(1,@extended,$hImage)
$sMode=_SetBitmapToCtrl($CtrlID,$hImage)
If $sMode <> 1 Then Return SetError(2,0,$sMode)
Return 1
EndFunc
Func _LoadBitMapToHImage($sInImage,$iW,$iH,$sMode=0)
Local $szDrive, $szDir, $szFName, $szExt,$IMAGE_BITMAP=0,$LR_COPYDELETEORG=0x0008
_PathSplit($sInImage, $szDrive, $szDir, $szFName, $szExt)
$hBitmap = _WinAPI_LoadBitmap(_WinAPI_GetModuleHandle(0), $szFName)
If $hBitmap = 0 Then
$lMode = 1
$hBitmap = _WinAPI_LoadImage(0,$sInImage,$IMAGE_BITMAP,0,0,0x0010)
If $hBitmap = 0 Then Return SetError(1, $szFName,1)
EndIf
$hImage=_WinAPI_CopyImage($hBitmap,$IMAGE_BITMAP,$iW,$iH,$LR_COPYDELETEORG)
If $sMode = 0 Then
_WinAPI_DeleteObject($hBitmap)
Return $hImage
ElseIf $sMode =1 Then
Local $Return=[$hImage,$hBitmap]
Return $Return
EndIf
EndFunc
页:
[1]