dakishman Posted August 25, 2010 Posted August 25, 2010 (edited) What the program does:It is a little program to just help memorize the times table. If you answer the question within 2 seconds of it displaying and you get the answer right it will display a random image from the image folder "@ScriptDir\Pics". The user can put any picture in there from puppy dogs, to cars, to anything... The program will automatically propagate the list and select one at random. If you click the wrong answer nothing happens as of yet. If you click the right answer after 2 seconds it states "Too Slow" and comes up with a new formula. If you answer correctly and in under 2 seconds it will remember that formula so that it is not called again so that you can narrow in on what you are lacking. Hope this description helps.The Problem:I can not seem to resize the image to a format that fits the screen and keeps its aspect ratio. I was thinking about keeping a height of 200 and using W\H= Ratio to recalc the new width to keep the aspect ratio. The problem is for some reason when I try to use "GUICtrlCreatePic" the image does not appear. The window is resized and to the right dimensions but it does not display the image. As of now I have the image displayed at its original size. Even now sometimes the window is not the correct size for the imageWhat I have attempted:Before you flame I have searched the forums and I know there are a few posts about image resizing using GDI+. This is where I got my current code, however, I do not fully understand how the GDI+ works. Here is the proof of my searches:http://www.autoitscript.com/forum/index.php?showtopic=96238However whenever I try to use "GUICtrlCreatePic" in mine the image doesn't display, but if I remove "_GDIPlus_ImageLoadFromFile" it does but I can't get the dimensions. So as of now I use "_GDIPlus_GraphicsCreateFromHWND" to create the image but I don't see how I can size the image using this.Goal:Once the answer is answered correctly and under 2 seconds the image will be displayed at 200pixels by X in the same aspect ratio.Here is my current code all commented up:expandcollapse popup#include <ButtonConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> #include <File.au3> #include <Array.au3> #include <GDIPlus.au3> ;Sets Global Vars Global $sini = @ScriptDir & "\KnownTables.ini" ;sets INI location Global $Button[5], $X, $Y ;Declares buttons and X and Y numbers for formula Global $PicsList = _FileListToArray(@ScriptDir & "\Pics\") ;Location of the pics dir Global $timer = TimerInit() ;Starts timer. I don't know why but Site was barking at me to start it globally. So instead of arguing with Site... Global $GoodAnswers[13][13] ;This sets the array to record the correct answers ;Load from the inifile the answers that have been correctly answered from previous runs For $i = 1 To 12 For $k = 1 To 12 $GoodAnswers[$i][$k] = IniRead($sini, "KNOWN", $i & $k, 0) Next Next ;asks user if they would like to use the pics in the first place. $Answer = MsgBox(4, "UsePics?", "Would you like to use pics?") ;GUI Window to display the buttons and the formula #Region ### START Koda GUI section ### Form=c:\users\kish\documents\sync folder (netbook)\times table program\main window.kxf $DisplayWindow = GUICreate("Multi Trainer", 317, 132, 312, 133) $Button[1] = GUICtrlCreateButton("", 0, 72, 75, 25, $WS_GROUP) $Button[2] = GUICtrlCreateButton("", 80, 72, 75, 25, $WS_GROUP) $Button[3] = GUICtrlCreateButton("", 160, 72, 75, 25, $WS_GROUP) $Button[4] = GUICtrlCreateButton("", 240, 72, 75, 25, $WS_GROUP) $ExitButton = GUICtrlCreateButton("EXIT", 0, 104, 315, 25, $WS_GROUP) $DisplayArea = GUICtrlCreateLabel("DisplayArea", 0, 0, 316, 68) GUICtrlSetFont(-1, 40, 800, 0, "Vrinda") GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### ;Start my reset func to create a formula and to create button numbers for the formula _Reset() ;Gui Loop to read the button pressed While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE _SaveGoodAnswers() ;func used to save the correctly answered questions Exit Case $Button[1] $ButtonNum = GUICtrlRead($Button[1]) If Int($ButtonNum) = $X * $Y Then _Correct() Case $Button[2] $ButtonNum = GUICtrlRead($Button[2]) If Int($ButtonNum) = $X * $Y Then _Correct() Case $Button[3] $ButtonNum = GUICtrlRead($Button[3]) If Int($ButtonNum) = $X * $Y Then _Correct() Case $Button[4] $ButtonNum = GUICtrlRead($Button[4]) If Int($ButtonNum) = $X * $Y Then _Correct() Case $ExitButton Exit EndSwitch WEnd Func _Reset() ;Selects random numbers from 1-12 to propagate a formula and then randomizes the buttons. Do $X = Int(Random(1, 12)) $Y = Int(Random(1, 12)) Until $GoodAnswers[$X][$Y] <> 1; This checks to make sure that the equation is not already known. For $i = 1 To 4; This propagates all 4 buttons with random answers (Seemed to be the easiest way instead of trying to identify the correct answer button then generating after that...) $RndNum = Int(Random(1, 12)) $RndButtonNum = $RndNum * $X GUICtrlSetData($Button[$i], $RndButtonNum); Sets current button ($i) to the random number Next GUICtrlSetData($DisplayArea, " " & $X & " X " & $Y & " = ?"); Display the formula $CorrectAnswer = Int(Random(1, 4)); Randomly choose a button for the correct answer GUICtrlSetData($Button[$CorrectAnswer], $X * $Y); Sets that button to the answer $timer = TimerInit(); Starts the timer to count for the 2 seconds EndFunc ;==>_Reset Func _Correct() $beat2sec = Int(TimerDiff($timer)); Record the time diff of the button from when the formula was made till the button was pressed If IsArray($PicsList) And $Answer = 6 And $beat2sec < 2000 Then; Make sure that the user choose yes to using pictures (msg box above) That there are pictures in the folder, and that the answer was within 2 seconds (2000 because the timer is in miliseconds)) $GoodAnswers[$X][$Y] = 1; sets the current X & Y Vals used in formula to the array to 1 (1 = answered correctly) _GDIPlus_Startup() $PicNum = Int(Random(0, $PicsList[0])); randomly pic a number for the picture to display ($piclist[0] is the total values in the $picslist array) $hBitmap = _GDIPlus_BitmapCreateFromFile(@ScriptDir & "\pics\" & $PicsList[$PicNum]); create a bitmap object from file $LoadedPicH = _GDIPlus_ImageGetHeight($hBitmap); Read pics Hight $LoadedPicW = _GDIPlus_ImageGetWidth($hBitmap); read pics width $WidthRatio = $LoadedPicW / $LoadedPicH; create an aspect ratio of Hight to width $NewWidth = 200 * $WidthRatio; Multiplies the Aspect ratio to what the new Hight should be $PictureWindow = GUICreate("My GUI picture", $LoadedPicW, $LoadedPicH, 0, 0, $WS_SIZEBOX + $WS_SYSMENU); Creates the picture window in the GUI GUISetState(@SW_SHOW) $hGraphic = _GDIPlus_GraphicsCreateFromHWND($PictureWindow); create a graphic object in $Picture Window GUICtrlSetPos($hGraphic, 0, 0, $NewWidth, 200); Not sure, was an attempt from some of the posts I read to hopefully resize the _GDIPlus_GraphicsCreateFromHWND. _GDIPlus_GraphicsDrawImage($hGraphic, $hBitmap, 0, 0); draws the bitmap image in the graphic Sleep(3000) GUIDelete($PictureWindow); Deletes window EndIf If $beat2sec > 2000 Then ;If was correct answer but after 2 seconds GUICtrlSetData($DisplayArea, "TOO SLOW") ; Display "Too Slow" Sleep(1000) EndIf _Reset(); Goes to reset to regenerate a new equation EndFunc ;==>_Correct Func _SaveGoodAnswers(); Attempts to save the correctly answered questions to a ini file For $i = 1 To 12 For $k = 1 To 12 IniWrite($sini, "KNOWN", $i & $k, $GoodAnswers[$i][$k]) Next Next EndFunc ;==>_SaveGoodAnswersHere is my code Not commented up:expandcollapse popup#include <ButtonConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> #include <File.au3> #include <Array.au3> #include <GDIPlus.au3> Global $sini = @ScriptDir & "\KnownTables.ini" Global $Button[5], $X, $Y Global $PicsList = _FileListToArray(@ScriptDir & "\Pics\") Global $timer = TimerInit() Global $GoodAnswers[13][13] For $i = 1 To 12 For $k = 1 To 12 $GoodAnswers[$i][$k] = IniRead($sini, "KNOWN", $i & $k, 0) Next Next $Answer = MsgBox(4, "UsePics?", "Would you like to use pics?") #Region ### START Koda GUI section ### Form=c:\users\kish\documents\sync folder (netbook)\times table program\main window.kxf $DisplayWindow = GUICreate("Multi Trainer", 317, 132, 312, 133) $Button[1] = GUICtrlCreateButton("", 0, 72, 75, 25, $WS_GROUP) $Button[2] = GUICtrlCreateButton("", 80, 72, 75, 25, $WS_GROUP) $Button[3] = GUICtrlCreateButton("", 160, 72, 75, 25, $WS_GROUP) $Button[4] = GUICtrlCreateButton("", 240, 72, 75, 25, $WS_GROUP) $ExitButton = GUICtrlCreateButton("EXIT", 0, 104, 315, 25, $WS_GROUP) $DisplayArea = GUICtrlCreateLabel("DisplayArea", 0, 0, 316, 68) GUICtrlSetFont(-1, 40, 800, 0, "Vrinda") GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### _Reset() While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE _SaveGoodAnswers() Exit Case $Button[1] $ButtonNum = GUICtrlRead($Button[1]) If Int($ButtonNum) = $X * $Y Then _Correct() Case $Button[2] $ButtonNum = GUICtrlRead($Button[2]) If Int($ButtonNum) = $X * $Y Then _Correct() Case $Button[3] $ButtonNum = GUICtrlRead($Button[3]) If Int($ButtonNum) = $X * $Y Then _Correct() Case $Button[4] $ButtonNum = GUICtrlRead($Button[4]) If Int($ButtonNum) = $X * $Y Then _Correct() Case $ExitButton Exit EndSwitch WEnd Func _Reset() Do $X = Int(Random(1, 12)) $Y = Int(Random(1, 12)) Until $GoodAnswers[$X][$Y] <> 1 For $i = 1 To 4 $RndNum = Int(Random(1, 12)) $RndButtonNum = $RndNum * $X GUICtrlSetData($Button[$i], $RndButtonNum) Next GUICtrlSetData($DisplayArea, " " & $X & " X " & $Y & " = ?") $CorrectAnswer = Int(Random(1, 4)) GUICtrlSetData($Button[$CorrectAnswer], $X * $Y) $timer = TimerInit() EndFunc ;==>_Reset Func _Correct() $beat2sec = Int(TimerDiff($timer)) If IsArray($PicsList) And $Answer = 6 And $beat2sec < 2000 Then $GoodAnswers[$X][$Y] = 1 _ArrayDisplay($GoodAnswers) _GDIPlus_Startup() $PicNum = Int(Random(0, $PicsList[0])) $hBitmap = _GDIPlus_BitmapCreateFromFile(@ScriptDir & "\pics\" & $PicsList[$PicNum]); create a bitmap object from file $LoadedPicH = _GDIPlus_ImageGetHeight($hBitmap) $LoadedPicW = _GDIPlus_ImageGetWidth($hBitmap) $WidthRatio = $LoadedPicW / $LoadedPicH $NewWidth = 200 * $WidthRatio $PictureWindow = GUICreate("My GUI picture", $LoadedPicW, $LoadedPicH, 0, 0, $WS_SIZEBOX + $WS_SYSMENU) GUISetState(@SW_SHOW) $hGraphic = _GDIPlus_GraphicsCreateFromHWND($PictureWindow); create a graphic object in form1 GUICtrlSetPos($hGraphic, 0, 0, $NewWidth, 200) _GDIPlus_GraphicsDrawImage($hGraphic, $hBitmap, 0, 0); draws the bitmap image in the graphic Sleep(3000) GUIDelete($PictureWindow) EndIf If $beat2sec > 2000 Then GUICtrlSetData($DisplayArea, "TOO SLOW") Sleep(1000) EndIf _Reset() EndFunc ;==>_Correct Func _SaveGoodAnswers() For $i = 1 To 12 For $k = 1 To 12 IniWrite($sini, "KNOWN", $i & $k, $GoodAnswers[$i][$k]) Next Next EndFunc ;==>_SaveGoodAnswersThanks in advanced for the help.P.S. I know the read and write to the ini file is broken. I can fix this later. That isn't a big deal, it's prolly that I just used the wrong values or syntax. Edited August 25, 2010 by dakishman I love AutoIT! It doesn't talk back, it doesn't complain that ur on the computer, it loves to be ran over and over and over... but IT STILL DOESN'T DO THE DISHES... Gatta keep da pimp hand strong...
inzam Posted January 15, 2011 Posted January 15, 2011 if you want to resize image than i will you suggest you to visit the below link http://www.raiseitsolutions.com/forum/viewtopic.php?f=4&t=3 please visit the link, here you can know about how to resize image. thank you very much.
UEZ Posted January 15, 2011 Posted January 15, 2011 This is a simple calculation to display an image to a defined area in GUI expandcollapse popup;code by UEZ 2010 #include <GUIConstantsEx.au3> $iFile = FileOpenDialog("Please select an image", "", "Bild (*.jpg;*.bmp;*.gif)") ; for PNG images use GDI+ $iD = GetImageDim($iFile) If @error Then Exit $iW = $iD[0] $iH = $iD[1] $base_w = 150 $base_h = 150 $hGUI_w = 320 $hGUI_h = 150 $button_w = 100 $hGUI = GUICreate("Image Display by UEZ 2010", $hGUI_w, $hGUI_h) $button = GUICtrlCreateButton("Exit", $base_w + ($hGUI_w - $base_w - $button_w ) / 2, $hGUI_h / 2 - 12, $button_w) ;calculate image dimension which fits into defined area ($base_w x $base_h) If $iW < $base_w And $iH < $base_h Then $w = $base_w / 2 - $iW / 2 $h = $base_h / 2 - $iH / 2 GUICtrlCreatePic($iFile, $base_w / 2 - $iW / 2, $base_h / 2 - $iH / 2, $iW, $iH) Else If $iW > $iH Then $f = $iW / $base_w $w = $iW / $f $h = $ih / $f Else $f = $iH / $base_h $w = $iW / $f $h = $ih / $f EndIf GUICtrlCreatePic($iFile, $base_w / 2 - $w / 2, $base_h / 2 - $h / 2, $w, $h) EndIf $g = GUICtrlCreateGraphic(0, 0, $base_w, $base_h) GUICtrlSetGraphic(-1, $GUI_GR_COLOR, 0xc0c0ff, 0xc0c0ff) GUICtrlSetGraphic(-1, $GUI_GR_RECT, 0, 0, $base_w, $base_h) GUISetState() Do Switch GUIGetMsg() Case -3, $button GUIDelete($hGUI) Exit EndSwitch Until False Func GetImageDim($file) ;code by Melba23 - modified by UEZ Local $sFile = StringRegExp($file, "(?i).*\\(.*)", 3) If Not IsArray($sFile) Or @error Then Return SetError(1, 0, 0) Local $sPath = StringRegExp($file, "(?i)(.*)\\.+", 3) If Not IsArray($sFile) Or @error Then Return SetError(1, 0, 0) Local $sDimensions = "" Local $oShellApp = ObjCreate("shell.application") If IsObj($oShellApp) Then Local $oDir = $oShellApp.NameSpace($sPath[0]) If IsObj($oDir) Then Local $oFile = $oDir.Parsename($sFile[0]) If IsObj($oFile) Then If @OSBuild > 6000 Then $sDimensions = $oDir.GetDetailsOf($oFile, 31) ElseIf @OSVersion = "WIN_XP" Then $sDimensions = $oDir.GetDetailsOf($oFile, 26) EndIf EndIf EndIf EndIf If $sDimensions = "" Then Return SetError(1, 0, 0) ;"Object creation failed" Local $aDimensions = StringRegExp($sDimensions, "(?i)[\d]*x*[\d]", 3) If Not IsArray($aDimensions) Then Return SetError(1, 0, 0) ;"Cannot get image resolution!" Return SetError(0, 0, $aDimensions) EndFunc I will check you code and implement it with the GDI+ stuff Meanwhile you can check out the calculation. It is one possibility! Br, UEZ Please don't send me any personal message and ask for support! I will not reply! Selection of finest graphical examples at Codepen.io The own fart smells best! ✌Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ
UEZ Posted January 15, 2011 Posted January 15, 2011 Here your version mixed together with the calculation: expandcollapse popup#include <ButtonConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> #include <File.au3> #include <Array.au3> #include <GDIPlus.au3> _GDIPlus_Startup() ;Sets Global Vars Global $sini = @ScriptDir & "\KnownTables.ini" ;sets INI location Global $Button[5], $X, $Y ;Declares buttons and X and Y numbers for formula Global $PicsList = _FileListToArray(@ScriptDir & "\Pics\") ;Location of the pics dir Global $timer = TimerInit() ;Starts timer. I don't know why but Site was barking at me to start it globally. So instead of arguing with Site... Global $GoodAnswers[13][13] ;This sets the array to record the correct answers ;Load from the inifile the answers that have been correctly answered from previous runs For $i = 1 To 12 For $k = 1 To 12 $GoodAnswers[$i][$k] = IniRead($sini, "KNOWN", $i & $k, 0) Next Next ;asks user if they would like to use the pics in the first place. $Answer = MsgBox(4, "UsePics?", "Would you like to use pics?") ;GUI Window to display the buttons and the formula #Region ### START Koda GUI section ### Form=c:\users\kish\documents\sync folder (netbook)\times table program\main window.kxf $DisplayWindow = GUICreate("Multi Trainer", 317, 132, 312, 133) $Button[1] = GUICtrlCreateButton("", 0, 72, 75, 25, $WS_GROUP) $Button[2] = GUICtrlCreateButton("", 80, 72, 75, 25, $WS_GROUP) $Button[3] = GUICtrlCreateButton("", 160, 72, 75, 25, $WS_GROUP) $Button[4] = GUICtrlCreateButton("", 240, 72, 75, 25, $WS_GROUP) $ExitButton = GUICtrlCreateButton("EXIT", 0, 104, 315, 25, $WS_GROUP) $DisplayArea = GUICtrlCreateLabel("DisplayArea", 0, 0, 316, 68) GUICtrlSetFont(-1, 40, 800, 0, "Vrinda") GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### ;Start my reset func to create a formula and to create button numbers for the formula _Reset() ;Gui Loop to read the button pressed While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE _SaveGoodAnswers() ;func used to save the correctly answered questions Exit Case $Button[1] $ButtonNum = GUICtrlRead($Button[1]) If Int($ButtonNum) = $X * $Y Then _Correct() Case $Button[2] $ButtonNum = GUICtrlRead($Button[2]) If Int($ButtonNum) = $X * $Y Then _Correct() Case $Button[3] $ButtonNum = GUICtrlRead($Button[3]) If Int($ButtonNum) = $X * $Y Then _Correct() Case $Button[4] $ButtonNum = GUICtrlRead($Button[4]) If Int($ButtonNum) = $X * $Y Then _Correct() Case $ExitButton Exit EndSwitch WEnd Func _Reset() ;Selects random numbers from 1-12 to propagate a formula and then randomizes the buttons. Do $X = Int(Random(1, 12)) $Y = Int(Random(1, 12)) Until $GoodAnswers[$X][$Y] <> 1; This checks to make sure that the equation is not already known. For $i = 1 To 4; This propagates all 4 buttons with random answers (Seemed to be the easiest way instead of trying to identify the correct answer button then generating after that...) $RndNum = Int(Random(1, 12)) $RndButtonNum = $RndNum * $X GUICtrlSetData($Button[$i], $RndButtonNum); Sets current button ($i) to the random number Next GUICtrlSetData($DisplayArea, " " & $X & " X " & $Y & " = ?"); Display the formula $CorrectAnswer = Int(Random(1, 4)); Randomly choose a button for the correct answer GUICtrlSetData($Button[$CorrectAnswer], $X * $Y); Sets that button to the answer $timer = TimerInit(); Starts the timer to count for the 2 seconds EndFunc ;==>_Reset Func _Correct() $beat2sec = Int(TimerDiff($timer)); Record the time diff of the button from when the formula was made till the button was pressed If IsArray($PicsList) And $Answer = 6 And $beat2sec < 2000 Then; Make sure that the user choose yes to using pictures (msg box above) That there are pictures in the folder, and that the answer was within 2 seconds (2000 because the timer is in miliseconds)) $GoodAnswers[$X][$Y] = 1; sets the current X & Y Vals used in formula to the array to 1 (1 = answered correctly) $PicNum = Int(Random(1, $PicsList[0])); randomly pic a number for the picture to display ($piclist[0] is the total values in the $picslist array) $hBitmap = _GDIPlus_BitmapCreateFromFile(@ScriptDir & "\pics\" & $PicsList[$PicNum]); create a bitmap object from file $iH = _GDIPlus_ImageGetHeight($hBitmap); Read pics Hight $iW = _GDIPlus_ImageGetWidth($hBitmap); read pics width Local $base_w = 200 Local $base_h = 200 $PictureWindow = GUICreate("My GUI picture", $base_w, $base_h, 0, 0);, $WS_SIZEBOX + $WS_SYSMENU); Creates the picture window in the GUI GUISetState(@SW_SHOW) $hGraphic = _GDIPlus_GraphicsCreateFromHWND($PictureWindow); create a graphic object in $Picture Window _GDIPlus_GraphicsClear($hGraphic) If $iW < $base_w And $iH < $base_h Then $w = $base_w / 2 - $iW / 2 $h = $base_h / 2 - $iH / 2 _GDIPlus_GraphicsDrawImageRect($hGraphic, $hBitmap, $base_w / 2 - $iW / 2, $base_h / 2 - $iH / 2, $iW, $iH); draws the bitmap image in the graphic Else If $iW > $iH Then $f = $iW / $base_w Else $f = $iH / $base_h EndIf $w = $iW / $f $h = $iH / $f _GDIPlus_GraphicsDrawImageRect($hGraphic, $hBitmap, $base_w / 2 - $w / 2, $base_h / 2 - $h / 2, $w, $h) EndIf Sleep(3000) _GDIPlus_GraphicsDispose($hGraphic) _GDIPlus_BitmapDispose($hBitmap) GUIDelete($PictureWindow); Deletes window EndIf If $beat2sec > 2000 Then ;If was correct answer but after 2 seconds GUICtrlSetData($DisplayArea, "TOO SLOW") ; Display "Too Slow" Sleep(1000) EndIf _Reset(); Goes to reset to regenerate a new equation EndFunc ;==>_Correct Func _SaveGoodAnswers(); Attempts to save the correctly answered questions to a ini file For $i = 1 To 12 For $k = 1 To 12 IniWrite($sini, "KNOWN", $i & $k, $GoodAnswers[$i][$k]) Next Next EndFunc ;==>_SaveGoodAnswers Br, UEZ Please don't send me any personal message and ask for support! I will not reply! Selection of finest graphical examples at Codepen.io The own fart smells best! ✌Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ
Qwerty212 Posted February 5, 2012 Posted February 5, 2012 (edited) Is there a good way to maximize the area of the screen that the image uses?I tried to modify your example doing:Local $A, $d = $c, $iX, $iY $hImage = _GDIPlus_ImageLoadFromFile($image) $iX = _GDIPlus_ImageGetWidth($hImage) $iY = _GDIPlus_ImageGetHeight($hImage) $FDesktop = $height / $width $Fact = 1 If $iX > $width And $FDesktop >= ($iY / $iX) Then $Fact = $width / $iX ElseIf $iY >= $height Then $Fact = $height / $iY ElseIf $iY < $height And ($iY / $iX) >= 1 Then $Fact = $height / $iY ElseIf $iX < $width Then $Fact = $width / $iX EndIf $H1 = Round(($Fact * $iY), 0) $W1 = Round(($Fact * $iX), 0) ;$H0 = Round(($height-$H1)/2,0) ;$W0 = Round(($width-$W1)/2,0) _GDIPlus_GraphicsDrawImageRect($hGraphic[$d], $hImage, ($width - $W1) / 2, ($height - $H1) / 2, $W1, $H1)But sometimes some images are just cutted I wanted that large or little images just fit as big as they can in the screen keeping the aspect ratio (like when you do an slideshow at full screen with xnview): Thanks in advance Edited February 5, 2012 by adolfito121
UEZ Posted February 6, 2012 Posted February 6, 2012 (edited) Try this: expandcollapse popup#include <gdiplus.au3> #include <windowsconstants.au3> _GDIPlus_Startup() Global $sFile = StringReplace(@AutoItExe, "autoit3.exe", "ExamplesGUIMerlin.gif") Global $hBmp = ScaleImage($sFile, @DesktopWidth * 0.95, @DesktopHeight * 0.95) Global $iW = _GDIPlus_ImageGetWidth($hBmp) Global $iH = _GDIPlus_ImageGetHeight($hBmp) Global $hGUI = GUICreate("GDI+ Image Scaler", $iW, $iH, -1, -1, $WS_POPUP) GUISetState() Global $hGraphics = _GDIPlus_GraphicsCreateFromHWND($hGUI) _GDIPlus_GraphicsDrawImage($hGraphics, $hBmp, 0, 0) _GDIPlus_BitmapDispose($hBmp) Do Until GUIGetMsg() = - 3 _GDIPlus_Shutdown() Exit Func ScaleImage($sFile, $iNewWidth, $iNewHeight, $iInterpolationMode = 7) ;coded by UEZ 2012 If Not FileExists($sFile) Then Return SetError(1, 0, 0) Local $hImage = _GDIPlus_ImageLoadFromFile($sFile) If @error Then Return SetError(2, 0, 0) Local $iWidth = _GDIPlus_ImageGetWidth($hImage) Local $iHeight = _GDIPlus_ImageGetHeight($hImage) Local $iW, $iH, $f, $fRatio If $iWidth > $iHeight Then $f = $iWidth / $iNewWidth Else $f = $iHeight / $iNewHeight EndIf $iW = Int($iWidth / $f) $iH = Int($iHeight / $f) If $iW > $iNewWidth Then $fRatio = $iNewWidth / $iW $iW = Int($iW * $fRatio) $iH = Int($iH * $fRatio) ElseIf $iH > $iNewHeight Then $fRatio = $iNewHeight / $iH $iW = Int($iW * $fRatio) $iH = Int($iH * $fRatio) EndIf Local $hBitmap = DllCall($ghGDIPDll, "uint", "GdipCreateBitmapFromScan0", "int", $iW, "int", $iH, "int", 0, "int", 0x0026200A, "ptr", 0, "int*", 0) If @error Then Return SetError(3, 0, 0) $hBitmap = $hBitmap[6] Local $hBmpCtxt = _GDIPlus_ImageGetGraphicsContext($hBitmap) DllCall($ghGDIPDll, "uint", "GdipSetInterpolationMode", "handle", $hBmpCtxt, "int", $iInterpolationMode) _GDIPlus_GraphicsDrawImageRect($hBmpCtxt, $hImage, 0, 0, $iW, $iH) _GDIPlus_ImageDispose($hImage) _GDIPlus_GraphicsDispose($hBmpCtxt) Return $hBitmap EndFunc Br, UEZ Edited February 8, 2012 by UEZ Please don't send me any personal message and ask for support! I will not reply! Selection of finest graphical examples at Codepen.io The own fart smells best! ✌Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ
Qwerty212 Posted February 7, 2012 Posted February 7, 2012 Thanks a lot UEZ, it worked like a charm. I used your code mixed with another examples in the forum: Global Const $width = @DesktopWidth, $height = @DesktopHeight Local $A, $d = $c, $iX, $iY, $iW, $iH $hImage = _GDIPlus_ImageLoadFromFile($image) $iX = _GDIPlus_ImageGetWidth($hImage) $iY = _GDIPlus_ImageGetHeight($hImage) $iW = _GDIPlus_ImageGetWidth($hImage) $iH = _GDIPlus_ImageGetHeight($hImage) Local $iWidth = _GDIPlus_ImageGetWidth($hImage) Local $iHeight = _GDIPlus_ImageGetHeight($hImage) Local $iW, $iH, $f, $fRatio If $iWidth > $iHeight Then $f = $iWidth / $width Else $f = $iHeight / $height EndIf $iW = Int($iWidth / $f) $iH = Int($iHeight / $f) If $iW > $width Then $fRatio = $width / $iW $iW = Int($iW * $fRatio) $iH = Int($iH * $fRatio) ElseIf $iH > $height Then $fRatio = $height / $iH $iW = Int($iW * $fRatio) $iH = Int($iH * $fRatio) EndIf _GDIPlus_GraphicsDrawImageRect($hGraphic[$d], $hImage, ($width - $iW) / 2, ($height - $iH) / 2, $iW, $iH) _GDIPlus_ImageDispose($hImage) What are the advantages of using the DLL call and the interpolation instead of directly drawing with GDI?? Thanks again for your help.
UEZ Posted February 8, 2012 Posted February 8, 2012 What are the advantages of using the DLL call and the interpolation instead of directly drawing with GDI?? Drawing with GDI will also call a dll but there is a difference between GDI and GDI+. Several people say GDI to GDI+ stuff which is not correct. Regarding GdipSetInterpolationMode: ; $iInterpolationMode - Interpolation mode: ; |0 - Default interpolation mode ; |1 - Low-quality mode ; |2 - High-quality mode ; |3 - Bilinear interpolation. No prefiltering is done ; |4 - Bicubic interpolation. No prefiltering is done ; |5 - Nearest-neighbor interpolation ; |6 - High-quality, bilinear interpolation. Prefiltering is performed to ensure high-quality shrinking ; |7 - High-quality, bicubic interpolation. Prefiltering is performed to ensure high-quality shrinking The interpolation modes are able increase the quality of resized images. Br, UEZ Please don't send me any personal message and ask for support! I will not reply! Selection of finest graphical examples at Codepen.io The own fart smells best! ✌Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ
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