cramaboule Posted January 2 Posted January 2 (edited) Here is an exemple of the PNG scaled down based on this script : https://www.autoitscript.com/forum/topic/200086-display-png-images/#findComment-1498721 Here is my script: but the png is cropped 1 or 2 px The others looks bad any hint how to fix it ? expandcollapse popup#include <GDIPlus.au3> #include <GUIConstantsEx.au3> #include <MsgBoxConstants.au3> #include <WindowsConstants.au3> #include <StaticConstants.au3> Local $idPic, $idPic1 GUICreate('Test', 200, 500, 1050, 50) ;Create a GUI GUISetBkColor(0xFFFFFF) $idPic = _GUICtrlCreatePngScale(@ScriptDir & "\big-green.png", 5, 5, .035) $idPic1 = _GUICtrlCreatePngScale(@ScriptDir & "\big-green.png", 5, 200, 1) $idPic2 = GUICtrlCreatePic(@ScriptDir & "\green.bmp", 5, 300, 100, 100) GUICtrlSetPos($idPic1, 5, 150, 100, 100) GUISetState(@SW_SHOW) While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE Exit Case $idPic MsgBox($MB_TOPMOST, "Information", "PNG 1 image was clicked") Case $idPic1 MsgBox($MB_TOPMOST, "Information", "PNG 2 image was clicked") Case $idPic2 MsgBox($MB_TOPMOST, "Information", "PNG 3 image was clicked") EndSwitch WEnd Func _GUICtrlCreatePngScale($_sName, $_left, $_top, $_iScale = 1) _GDIPlus_Startup() Local $_hImage = _GDIPlus_ImageLoadFromFile($_sName) ;The PNG image that you want to display Local $B_Scaled = _GDIPlus_ImageScale($_hImage, $_iScale, $_iScale) Local $_idPic = GUICtrlCreatePic("", $_left, $_top, 20, 20) GUICtrlSendMsg($_idPic, 0x0172, 0, _GDIPlus_BitmapCreateHBITMAPFromBitmap($B_Scaled)) _WinAPI_DeleteObject($_hImage) ;some clean up _GDIPlus_ImageDispose($B_Scaled) _GDIPlus_Shutdown() Return $_idPic EndFunc ;==>_GUICtrlCreatePngScale green.BMP Edited January 3 by cramaboule My Autoit programs: MAC Address - - Delete Temp Files - - Ping Test - - Play Video with VLC full screen dual monitors - - Set IP - - Pics Converter - - AutoUpdater - - CPU Usage - - Ending Script Nicely - - GDI+ GUI crossfades (slide transitions) - - Beamer - - Search and Search in Files - - Silent Ninite Others: Export Icons into Dll - - My website
MattyD Posted January 3 Posted January 3 Not sure if this helps - I was looking for an out by 1 error re the cropping, but the scaling function looks to be correct as far as I can tell. 🤷♂️ Below is the same scaling routine, but I've added a 1px box around the image to help find the edge. Then I've saved the bitmap using _GDIPlus_ImageSaveToFile to inspect it and verify the new dimensions etc. So with this you can play with adding +1s with the widths and heights etc (just in case it helps) Func _GDIPlus_ImageScale2($hImage, $iScaleW, $iScaleH, $iInterpolationMode = $GDIP_INTERPOLATIONMODE_HIGHQUALITYBICUBIC) Local $iWidth = _GDIPlus_ImageGetWidth($hImage) If @error Then Return SetError(1, 0, 0) Local $iHeight = _GDIPlus_ImageGetHeight($hImage) If @error Then Return SetError(2, 0, 0) Local $iNewWidth = $iWidth * $iScaleW Local $iNewHeight = $iHeight * $iScaleH Local $hBitmap = _GDIPlus_BitmapCreateFromScan0($iNewWidth, $iNewHeight) If @error Then Return SetError(3, 0, 0) Local $hBmpCtxt = _GDIPlus_ImageGetGraphicsContext($hBitmap) Local $hPen = _GDIPlus_PenCreate(0xFFAA00AA) _GDIPlus_GraphicsDrawRect($hBmpCtxt, 0, 0, $iNewWidth-1, $iNewHeight-1, $hPen) _GDIPlus_GraphicsSetInterpolationMode($hBmpCtxt, $iInterpolationMode) _GDIPlus_GraphicsSetPixelOffsetMode($hBmpCtxt, $GDIP_PIXELOFFSETMODE_HIGHQUALITY) Local $hIA = _GDIPlus_ImageAttributesCreate() __GDIPlus_ImageAttributesSetImageWrapMode($hIA) If @error Then _GDIPlus_ImageAttributesDispose($hIA) _GDIPlus_GraphicsDispose($hBmpCtxt) _GDIPlus_BitmapDispose($hBitmap) Return SetError(4, 0, 0) EndIf _GDIPlus_GraphicsDrawImageRectRect($hBmpCtxt, $hImage, 0, 0, $iWidth, $iHeight, 0, 0, $iNewWidth, $iNewHeight, $hIA) _GDIPlus_ImageAttributesDispose($hIA) _GDIPlus_GraphicsDispose($hBmpCtxt) Return $hBitmap EndFunc ioa747 1
Solution cramaboule Posted January 3 Author Solution Posted January 3 After digging into my script I found a small bug or improvement in the code of @UEZ in the UDF of GDI+ _GDIPlus_ImageScale() : I added Floor($iNewWidth), Floor($iNewHeight) Here is my script working nicely 😄 expandcollapse popup#include <GDIPlus.au3> #include <GUIConstantsEx.au3> #include <MsgBoxConstants.au3> #include <WindowsConstants.au3> #include <StaticConstants.au3> Local $idPic, $idPic1 GUICreate('Test', 200, 500, 1050, 50) ;Create a GUI GUISetBkColor(0xFFFFFF) $idPic = _GUICtrlCreatePngScale(@ScriptDir & "\big-green.png", 5, 5, .035) $idPic1 = _GUICtrlCreatePngScale(@ScriptDir & "\big-green.png", 5, 200, 1) $idPic2 = GUICtrlCreatePic(@ScriptDir & "\green.bmp", 5, 300, 100, 100) GUICtrlSetPos($idPic1, 5, 150, 100, 100) GUISetState(@SW_SHOW) While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE Exit Case $idPic MsgBox($MB_TOPMOST, "Information", "PNG 1 image was clicked") Case $idPic1 MsgBox($MB_TOPMOST, "Information", "PNG 2 image was clicked") Case $idPic2 MsgBox($MB_TOPMOST, "Information", "PNG 3 image was clicked") EndSwitch WEnd Func _GUICtrlCreatePngScale($_sName, $_left, $_top, $_iScale = 1) _GDIPlus_Startup() Local $_hImage = _GDIPlus_ImageLoadFromFile($_sName) ;The PNG image that you want to display Local $B_Scaled = _GDIPlus_ImageScaleNew($_hImage, $_iScale, $_iScale) Local $_idPic = GUICtrlCreatePic("", $_left, $_top, 20, 20) GUICtrlSendMsg($_idPic, 0x0172, 0, _GDIPlus_BitmapCreateHBITMAPFromBitmap($B_Scaled)) _WinAPI_DeleteObject($_hImage) ;some clean up _GDIPlus_ImageDispose($B_Scaled) _GDIPlus_Shutdown() Return $_idPic EndFunc ;==>_GUICtrlCreatePngScale Func _GDIPlus_ImageScaleNew($hImage, $iScaleW, $iScaleH, $iInterpolationMode = $GDIP_INTERPOLATIONMODE_HIGHQUALITYBICUBIC) Local $iWidth = _GDIPlus_ImageGetWidth($hImage) If @error Then Return SetError(1, 0, 0) Local $iHeight = _GDIPlus_ImageGetHeight($hImage) If @error Then Return SetError(2, 0, 0) Local $iNewWidth = $iWidth * $iScaleW Local $iNewHeight = $iHeight * $iScaleH Local $hBitmap = _GDIPlus_BitmapCreateFromScan0($iNewWidth, $iNewHeight) If @error Then Return SetError(3, 0, 0) Local $hBmpCtxt = _GDIPlus_ImageGetGraphicsContext($hBitmap) _GDIPlus_GraphicsSetInterpolationMode($hBmpCtxt, $iInterpolationMode) _GDIPlus_GraphicsSetPixelOffsetMode($hBmpCtxt, $GDIP_PIXELOFFSETMODE_HIGHQUALITY) Local $hIA = _GDIPlus_ImageAttributesCreate() __GDIPlus_ImageAttributesSetImageWrapMode($hIA) If @error Then _GDIPlus_ImageAttributesDispose($hIA) _GDIPlus_GraphicsDispose($hBmpCtxt) _GDIPlus_BitmapDispose($hBitmap) Return SetError(4, 0, 0) EndIf _GDIPlus_GraphicsDrawImageRectRect($hBmpCtxt, $hImage, 0, 0, $iWidth, $iHeight, 0, 0, Floor($iNewWidth), Floor($iNewHeight), $hIA) _GDIPlus_ImageAttributesDispose($hIA) _GDIPlus_GraphicsDispose($hBmpCtxt) Return $hBitmap EndFunc ;==>_GDIPlus_ImageScale MattyD and ioa747 2 My Autoit programs: MAC Address - - Delete Temp Files - - Ping Test - - Play Video with VLC full screen dual monitors - - Set IP - - Pics Converter - - AutoUpdater - - CPU Usage - - Ending Script Nicely - - GDI+ GUI crossfades (slide transitions) - - Beamer - - Search and Search in Files - - Silent Ninite Others: Export Icons into Dll - - My website
cramaboule Posted January 3 Author Posted January 3 3 minutes ago, MattyD said: Not sure if this helps - I was looking for an out by 1 error re the cropping, but the scaling function looks to be correct as far as I can tell. 🤷♂️ Below is the same scaling routine, but I've added a 1px box around the image to help find the edge. Then I've saved the bitmap using _GDIPlus_ImageSaveToFile to inspect it and verify the new dimensions etc. So with this you can play with adding +1s with the widths and heights etc (just in case it helps) Func _GDIPlus_ImageScale2($hImage, $iScaleW, $iScaleH, $iInterpolationMode = $GDIP_INTERPOLATIONMODE_HIGHQUALITYBICUBIC) Local $iWidth = _GDIPlus_ImageGetWidth($hImage) If @error Then Return SetError(1, 0, 0) Local $iHeight = _GDIPlus_ImageGetHeight($hImage) If @error Then Return SetError(2, 0, 0) Local $iNewWidth = $iWidth * $iScaleW Local $iNewHeight = $iHeight * $iScaleH Local $hBitmap = _GDIPlus_BitmapCreateFromScan0($iNewWidth, $iNewHeight) If @error Then Return SetError(3, 0, 0) Local $hBmpCtxt = _GDIPlus_ImageGetGraphicsContext($hBitmap) Local $hPen = _GDIPlus_PenCreate(0xFFAA00AA) _GDIPlus_GraphicsDrawRect($hBmpCtxt, 0, 0, $iNewWidth-1, $iNewHeight-1, $hPen) _GDIPlus_GraphicsSetInterpolationMode($hBmpCtxt, $iInterpolationMode) _GDIPlus_GraphicsSetPixelOffsetMode($hBmpCtxt, $GDIP_PIXELOFFSETMODE_HIGHQUALITY) Local $hIA = _GDIPlus_ImageAttributesCreate() __GDIPlus_ImageAttributesSetImageWrapMode($hIA) If @error Then _GDIPlus_ImageAttributesDispose($hIA) _GDIPlus_GraphicsDispose($hBmpCtxt) _GDIPlus_BitmapDispose($hBitmap) Return SetError(4, 0, 0) EndIf _GDIPlus_GraphicsDrawImageRectRect($hBmpCtxt, $hImage, 0, 0, $iWidth, $iHeight, 0, 0, $iNewWidth, $iNewHeight, $hIA) _GDIPlus_ImageAttributesDispose($hIA) _GDIPlus_GraphicsDispose($hBmpCtxt) Return $hBitmap EndFunc Thanks, yes it is playing around... but I think my solution works with upscale and downscale My Autoit programs: MAC Address - - Delete Temp Files - - Ping Test - - Play Video with VLC full screen dual monitors - - Set IP - - Pics Converter - - AutoUpdater - - CPU Usage - - Ending Script Nicely - - GDI+ GUI crossfades (slide transitions) - - Beamer - - Search and Search in Files - - Silent Ninite Others: Export Icons into Dll - - My website
MattyD Posted January 3 Posted January 3 I guess some of those GDI funcs deal with floats hey? I suppose that's where the inconsistency stems from.
cramaboule Posted January 3 Author Posted January 3 Quote I guess some of those GDI funcs deal with floats hey? I suppose that's where the inconsistency stems from. the _GDIPlus_ImageScale() can have some floats... soooo yeeeee this is why! My Autoit programs: MAC Address - - Delete Temp Files - - Ping Test - - Play Video with VLC full screen dual monitors - - Set IP - - Pics Converter - - AutoUpdater - - CPU Usage - - Ending Script Nicely - - GDI+ GUI crossfades (slide transitions) - - Beamer - - Search and Search in Files - - Silent Ninite Others: Export Icons into Dll - - My website
ioa747 Posted January 3 Posted January 3 (edited) it is much better Edited January 3 by ioa747 I know that I know nothing
ioa747 Posted January 3 Posted January 3 (edited) <snip> Edited January 3 by ioa747 I know that I know nothing
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now