Jump to content

? Convert image formats with AutoIt! _GDI+


Trong
 Share

Recommended Posts

When use FileInstall that BMP image is quite large capacity!

So how to extract jpg files to bmp when use FileInstall?

FileInstall("BG.JPG",@WorkingDir&"\BG.BMP") ;LOL

I need a function to quickly convert image formats!

Something like:

_IMGConvert("$File/Dir input",  "$Dir/Fileoutput=@ScriptDir", "OutputFormats=JPG/PNG/BMP", $Quality=100)

Regards,
 

Link to comment
Share on other sites

Try this:

#include <GDIPlus.au3>

_GDIPlus_Startup()
ConsoleWrite(_GDIPlus_ConvertImage("C:\Temp\Test.jpg", "C:\Temp\Converted.jpg", 10) & " / " & @error & @LF)
ConsoleWrite(_GDIPlus_ConvertImage("C:\Temp\Test.jpg", "C:\Temp\Converted.png") & " / " & @error & @LF)
ConsoleWrite(_GDIPlus_ConvertImage("C:\Temp\Test.jpg", "C:\Temp\Converted.bmp") & " / " & @error & @LF)
ConsoleWrite(_GDIPlus_ConvertImage("C:\Temp\Test.jpg", "C:\Temp\Converted.gif") & " / " & @error & @LF)
_GDIPlus_Shutdown()
Exit

; #FUNCTION# ====================================================================================================================
; Name ..........: _GDIPlus_ConvertImage
; Version .......:  v0.90 build 2013-02-14
; Description ...: Converts an image from one of these format to another (JPG, PNG, BMP, GIF, TIF)
; Syntax ........: _GDIPlus_ConvertImage($sIn, $sOut[, $iQuality = 90[, $iBGColor = 0x000000]])
; Parameters ....: $sIn                 - image file to load
;                  $sOut                -  output file name, extension must be given (JPG, PNG, BMP, GIF, TIF)
;                  $iQuality            - [optional] JPG quality settings. Default is 90.
;                  $iBGColor            - [optional] background color. Default is 0x000000. Needed for transparent images when background color shouldn't be black
; Return values .:  1
;                                           on error:
;                                           1       image file to load not found
;                                           2       GDI+ not initialized -> _GDIPlus_Startup()
;                                           3       unable to create bitmap from file
;                                           4       output image format not given
;                                           5       unable to save output JPG image
;                                           6       unable to save output image (BMP, PNG, GIF, TIF)
; Author ........: UEZ
; Modified ......:
; Remarks .......: GDIPlus.au3 needs to be included
; Related .......: GDI+
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _GDIPlus_ConvertImage($sIn, $sOut, $iQuality = 90, $iBGColor = 0x000000)
    If Not FileExists($sIn) Then Return SetError(1, 0, 0)
    If Not $giGDIPToken Then Return SetError(2, 0, 0)
    Local $hBitmap = _GDIPlus_BitmapCreateFromFile($sIn)
    If @error Then Return SetError(3, 0, 0)
    If Int($iQuality) < 0 Or Int($iQuality) > 100 Then $iQuality = 90
    If $iBGColor Then
        Local $iW = _GDIPlus_ImageGetWidth($hBitmap), $iH = _GDIPlus_ImageGetHeight($hBitmap)
        Local $aResult = DllCall($ghGDIPDll, "uint", "GdipCreateBitmapFromScan0", "int", $iW, "int", $iH, "int", 0, "int", $GDIP_PXF32ARGB, "ptr", 0, "int*", 0)
        Local $hCtxt = _GDIPlus_ImageGetGraphicsContext($aResult[6])
        _GDIPlus_GraphicsClear($hCtxt, 0xFF000000 + $iBGColor)
        _GDIPlus_GraphicsDrawImageRect($hCtxt, $hBitmap, 0, 0, $iW, $iH)
        _GDIPlus_GraphicsDispose($hCtxt)
        _GDIPlus_BitmapDispose($hBitmap)
        $hBitmap = $aResult[6]
    EndIf
    Local $iResult, $sSuffix = StringRight($sOut, 4), $iErr = 0
    Switch $sSuffix
        Case ".jpg"
            Local $sCLSID = _GDIPlus_EncodersGetCLSID("JPG")
            Local $tParams = _GDIPlus_ParamInit(1)
            Local $tData = DllStructCreate("int Quality")
            DllStructSetData($tData, "Quality", $iQuality)
            _GDIPlus_ParamAdd($tParams, $GDIP_EPGQUALITY, 1, $GDIP_EPTLONG, DllStructGetPtr($tData))
            _GDIPlus_ImageSaveToFileEx($hBitmap, $sOut, $sCLSID, DllStructGetPtr($tParams))
            If @error Then $iErr = 5
        Case ".bmp", ".gif", ".png", ".tif"
            _GDIPlus_ImageSaveToFile($hBitmap, $sOut)
            If @error Then $iErr = 6
        Case Else
            $iErr = 4
    EndSwitch
    _GDIPlus_BitmapDispose($hBitmap)
    $tParams = 0
    $tData = 0
    If $iErr Then Return SetError($iErr, 0, 0)
    Return 1
EndFunc

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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

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...