找回密码
 加入
搜索
查看: 4560|回复: 7

[图形处理] GDI+ Rect效果偏差 [已解决]

  [复制链接]
发表于 2010-8-16 22:33:36 | 显示全部楼层 |阅读模式
本帖最后由 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[0], $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] <> 0 Then _WinAPI_DeleteObject($oldBmp[0])
        Return 1
EndFunc
素材: 请另存为BKG-Body.jpg



效果差异:


代码在8楼.

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×

评分

参与人数 1金钱 +10 收起 理由
afan + 10 感谢主动将修改帖子分类为[已解决],请继续 ...

查看全部评分

发表于 2010-8-16 22:57:01 | 显示全部楼层
gdi的放大的时候用的算法 和 GUICtrlCreatePic创建的控件用的算法不同吧...?
 楼主| 发表于 2010-8-16 23:05:58 | 显示全部楼层
估计是的,我现在觉得GUICtrlCreatePic的算法很好。

我现在想达到createpic的效果。

因为仔细看GDI+的效果,连颜色都有偏差,不得不放弃.
发表于 2010-8-16 23:14:44 | 显示全部楼层
可恨...
http://social.msdn.microsoft.com ... pDrawImageRectRectI

居然失效了... 我看看..
发表于 2010-8-17 09:52:37 | 显示全部楼层
这个是什么呀,没看懂呢
 楼主| 发表于 2010-8-17 12:23:10 | 显示全部楼层
回复 4# rolaka

感谢帮忙!
发表于 2010-8-17 12:24:28 | 显示全部楼层
..只是简单查了下...

好像没有定义这一块的东西.
 楼主| 发表于 2010-8-17 19:27:19 | 显示全部楼层
本帖最后由 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[2]=[$hImage,$hBitmap]
                Return $Return
        EndIf
EndFunc

评分

参与人数 1金钱 +30 贡献 +2 收起 理由
afan + 30 + 2

查看全部评分

您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-5-17 17:29 , Processed in 0.088822 second(s), 30 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表