Jump to content

GDI+ StretchBlt


Tolf
 Share

Recommended Posts

In my script, I want to resize a picture and I do this :

#include <WindowsConstants.au3>
#include <WinAPI.au3>
#include <GDIPlus.au3>

$src_img = @ScriptDir & "\test.jpg"
$dst_path = @ScriptDir & "\conv3.jpg"
$width = 200
$height = 150

; load the original image
$h_image_tmp = _GDIPlus_ImageLoadFromFile ($src_img)
$width_tmp = _GDIPlus_ImageGetWidth ($h_image_tmp)
$height_tmp = _GDIPlus_ImageGetHeight ($h_image_tmp)

; create a new image
$h_bitmap = _WinAPI_CreateBitmap ($width, $height, 1, 32)
$h_image = _GDIPlus_BitmapCreateFromHBITMAP ($h_bitmap)
_WinAPI_DeleteObject ($h_bitmap)
$h_graphic = _GDIPlus_ImageGetGraphicsContext ($h_image)

; StretchBlt
$h_graphic_tmp = _GDIPlus_ImageGetGraphicsContext ($h_image_tmp)
$dc_tmp = _GDIPlus_GraphicsGetDC($h_graphic_tmp)
$dc_img = _GDIPlus_GraphicsGetDC($h_graphic)
DLLCall($ghGDIPDll, "int", "StretchBlt", "int", $dc_img, "int", 0, "int", 0, "int", $width, "int", $height, "int", $dc_tmp, "int", 0, "int", 0, "int", $width_tmp, "int", $height_tmp, "long", $SRCCOPY)

; realease the objects
_GDIPlus_GraphicsReleaseDC($h_graphic_tmp, $dc_tmp)
_GDIPlus_GraphicsReleaseDC($h_graphic, $dc_img)
_GDIPlus_ImageDispose ($h_image_tmp)

; save the new image
_GDIPlus_ImageSaveToFile ($h_image, $dst_path)

...but I get an error.

Does anybody know why I get an error ?

Edited by Tolf
My UDF : Array2D
Link to comment
Share on other sites

Well, try this:

DLLCall("gdi32.dll", "int", "StretchBlt", "int", $dc_img, "int", 0, "int", 0, "int", $width, "int", $height, "int", $dc_tmp, "int", 0, "int", 0, "int", $width_tmp, "int", $height_tmp, "long", $SRCCOPY)

gdi32 and gdiplus have different DLLs and StretchBlt is a GDI32 function.

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Link to comment
Share on other sites

No other ideas ?

Your script is missing _GDIPlus_Startup() before calling _GDIPlus_ commands. And _GDIPlus_Shutdown() after calling all _GDIPlus_ commands.

It would be an interesting exercise to get StretchBlt to do this task.

If your endeavour is to resize an image, (picture) you could look at this modified version of the script from here,

http://www.autoitscript.com/forum/index.ph...st&p=544484

or

use the other re-size example on that thread.

#include <GDIPlus.au3>
; http://www.autoitscript.com/forum/index.php?s=&showtopic=75022&view=findpost&p=544484
_ImageResize(@DesktopCommonDir & "\JigSawCentrePiece.png", @DesktopCommonDir & "\Bitm.png", 400, 200)

Func _ImageResize($sInImage, $sOutImage, $newW, $newH)
    Local $oldImage, $GC, $newBmp, $newGC
    _GDIPlus_Startup()
    ; Load Image
    $oldImage = _GDIPlus_ImageLoadFromFile($sInImage)
    ;Create New image
    $GC = _GDIPlus_ImageGetGraphicsContext($oldImage)
    $newBmp = _GDIPlus_BitmapCreateFromGraphics($newW, $newH, $GC)
    $newGC = _GDIPlus_ImageGetGraphicsContext($newBmp)
    ;Draw
    _GDIPlus_GraphicsDrawImageRect($newGC, $oldImage, 0, 0, $newW, $newH)
    _GDIPlus_ImageSaveToFile($newBmp, $sOutImage)
    ;Clenaup
    _GDIPlus_GraphicsDispose($GC)
    _GDIPlus_GraphicsDispose($newGC)
    _GDIPlus_BitmapDispose($newBmp)
    _GDIPlus_ImageDispose($oldImage)
    _GDIPlus_Shutdown()
EndFunc   ;==>_ImageResize

If you manage to get the StretchBlt version working, I would like to see how it was done.

Link to comment
Share on other sites

I've added _GDIPlus_Startup() and _GDIPlus_Shutdown() but now, I can only get a black image !

#include <WindowsConstants.au3>
#include <WinAPI.au3>
#include <GDIPlus.au3>

$src_img = @ScriptDir & "\test.jpg"
$dst_path = @ScriptDir & "\conv3.jpg"
$width = 200
$height = 150

_GDIPlus_Startup()

; load the original image
$h_image_tmp = _GDIPlus_ImageLoadFromFile ($src_img)
$width_tmp = _GDIPlus_ImageGetWidth ($h_image_tmp)
$height_tmp = _GDIPlus_ImageGetHeight ($h_image_tmp)

; create a new image
$h_bitmap = _WinAPI_CreateBitmap ($width, $height, 1, 32)
$h_image = _GDIPlus_BitmapCreateFromHBITMAP ($h_bitmap)
_WinAPI_DeleteObject ($h_bitmap)
$h_graphic = _GDIPlus_ImageGetGraphicsContext ($h_image)

; StretchBlt
$h_graphic_tmp = _GDIPlus_ImageGetGraphicsContext ($h_image_tmp)
$dc_tmp = _GDIPlus_GraphicsGetDC($h_graphic_tmp)
$dc_img = _GDIPlus_GraphicsGetDC($h_graphic)
DLLCall("gdi32.dll", "int", "StretchBlt", "int", $dc_img, "int", 0, "int", 0, "int", $width, "int", $height, "int", $dc_tmp, "int", 0, "int", 0, "int", $width_tmp, "int", $height_tmp, "long", $SRCCOPY)

; realease the objects
_GDIPlus_GraphicsReleaseDC($h_graphic_tmp, $dc_tmp)
_GDIPlus_GraphicsReleaseDC($h_graphic, $dc_img)
_GDIPlus_ImageDispose ($h_image_tmp)

; save the new image
_GDIPlus_ImageSaveToFile ($h_image, $dst_path)

_GDIPlus_Shutdown()
My UDF : Array2D
Link to comment
Share on other sites

  • 4 years later...

This is a late answer and the issue probably has been solved, but i couldn't find it and therefore made my own function, it's using StretchBlt, with GDI+

Forgive me if the code has its flaws, I tried making it as clean as possible.

#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <GDIPLUS.au3>

$hGui=GUICreate("", 320, 320)
_GDIPlus_Startup()
$hGraphics=_GDIPlus_GraphicsCreateFromHWND($hGui)
$hImage=_GDIPlus_ImageLoadFromFile(@ScriptDir&"1.png")

GUISetState(@SW_SHOW, $hGui)

_GDIPlus_StretchBlt($hGraphics, $hImage, 320, 320)

Do
Sleep(10)
Until GUIGetMsg()=$GUI_EVENT_CLOSE

_GDIPlus_ImageDispose($hImage)
_GDIPlus_GraphicsDispose($hGraphics)
_GDIPlus_Shutdown()
GUIDelete($hGui)
Exit

Func _GDIPlus_StretchBlt($hGraphics, $hImage, $iWidth, $iHeight, $iARGB=0xFF000000)
$iWidth2=_GDIPlus_ImageGetWidth($hImage)
$iHeight2=_GDIPlus_ImageGetHeight($hImage)
$hDC=_WinAPI_CreateCompatibleDC(0)
$hBmp=_GDIPlus_BitmapCreateFromGraphics($iWidth2, $iHeight2, $hGraphics)
$hGraphics2=_GDIPlus_ImageGetGraphicsContext($hBmp)
_GDIPlus_GraphicsDrawImage($hGraphics2, $hImage, 0, 0)
_GDIPlus_GraphicsDispose($hGraphics2)
$hGDIObj=_GDIPlus_BitmapCreateHBITMAPFromBitmap($hBmp, $iARGB)
_GDIPlus_BitmapDispose($hBmp)
$obj_select=_WinAPI_SelectObject($hDC, $hGDIObj)
$hGraphicsDc = _GDIPlus_GraphicsGetDC($hGraphics)

DLLCall("gdi32.dll", "int", "StretchBlt", "int", $hGraphicsDc, "int", 0, "int", 0, "int", $iWidth, "int", $iHeight, "int", $hDC, "int", 0, "int", 0, "int", $iWidth2, "int", $iHeight2, "long", $SRCCOPY)

_GDIPlus_GraphicsReleaseDC($hGraphics, $hGraphicsDc)
_WinAPI_SelectObject($hDC, $obj_select)
_WinAPI_DeleteObject($hGDIObj)
_WinAPI_DeleteDC($hDC)
EndFunc

Image for the exsample script above: http://green-tag.dk/AutoIt/1.png

Edited by genius257
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...