Sign in to follow this  
Followers 0
devilyn

Encode an image from a file with specific parameters

6 posts in this topic

All,

thank you for reading this post.

i would like to read an image file and save it with my own DPI value. bellow is what i have up to now.

#include <GDIPlus.au3>

Opt('MustDeclareVars', 1)

;Global $iMemo

_Main()

Func _Main()
    Local $hBitmap, $hImage ,$ImageHandle,$Status


; Initialize GDI+ library
    _GDIPlus_Startup()

; Capture  bitmap

    $hImage = _GDIPlus_BitmapCreateFromFile (@ScriptDir&"\new.jpg")

; Show horizontal resolution in DPI (dots per inch) of image
    ConsoleWrite( _GDIPlus_ImageGetHorizontalResolution($hImage)&@CRLF); only to console
    
    Local $Ext = _GDIPlus_EncodersGetCLSID("JPG")
    
    $Status = _GDIPlus_ImageSaveToFileEx($hImage,@ScriptDir&"\converted.JPG", $Ext, 0)
    MsgBox(0,$hImage,$Status)
    
; Clean up resources
    _GDIPlus_ImageDispose($hImage)
    _WinAPI_DeleteObject($hBitmap)
    
; Shut down GDI+ library
    _GDIPlus_Shutdown()


EndFunc  ;==>_Main

Thanks in advance

Share this post


Link to post
Share on other sites



while searching in google i found out that i need to call "GdipBitmapSetResolution" from "GDIPlus.dll"

now i wrote a dll call for this function but I'm not sure it works and if it works i cannot figure out how to save it as a new file.

$aResult = DllCall("GDIPlus.dll", "int", "GdipBitmapSetResolution", "hwnd", $hImage, "int*", 72 ,"int*",72)

Share this post


Link to post
Share on other sites

Hi Lior, try this

#include <GDIPlus.au3>

Opt('MustDeclareVars', 1)

;Global $iMemo

_Main()

Func _Main()
    Local $hBitmap, $hImage ,$ImageHandle,$Status


; Initialize GDI+ library
    _GDIPlus_Startup()

; Capture  bitmap

    $hImage = _GDIPlus_BitmapCreateFromFile (@ScriptDir&"\new.jpg")

; Show horizontal resolution in DPI (dots per inch) of image
    ConsoleWrite("+ " &  _GDIPlus_ImageGetHorizontalResolution($hImage) & " PPI" &@CRLF); only to console
    
    Local $aResult = _GDIPlus_BitmapSetResolution($hImage, 72, 72)
    ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $aResult = ' & $aResult & @crlf & '>Error code: ' & @error & @crlf) 
    
    
    Local $Ext = _GDIPlus_EncodersGetCLSID("JPG")
    $Status = _GDIPlus_ImageSaveToFileEx($hImage,@ScriptDir&"\converted.JPG", $Ext, 0)
    ;MsgBox(0,$hImage,$Status)
    
    
    $hImage = _GDIPlus_BitmapCreateFromFile (@ScriptDir&"\converted.JPG")

; Show horizontal resolution in DPI (dots per inch) of image
    ConsoleWrite("+ " &  _GDIPlus_ImageGetHorizontalResolution($hImage) & " PPI" &@CRLF); only to console
    
    
; Clean up resources
    _GDIPlus_ImageDispose($hImage)
    _WinAPI_DeleteObject($hBitmap)
    
; Shut down GDI+ library
    _GDIPlus_Shutdown()


EndFunc  ;==>_Main


Func _GDIPlus_BitmapSetResolution($hImage, $iPPIHoriz, $iPPIVert)
    ;Author(s): rover, Lior Berezinski, et al.
    If ($hImage = -1) Or (Not $hImage) Then Return SetError(4, 0, 0)
    Local $aResult, $iError = 0
    $aResult = DllCall($ghGDIPDll, "int", "GdipBitmapSetResolution", _
            "hwnd", $hImage, "float", $iPPIHoriz ,"float",$iPPIVert)
    $iError = @error
    If @error Or IsArray($aResult) = 0 Then Return SetError($iError, 0, 0)
    Return SetError($iError, 0, $aResult[0])
EndFunc   ;==>_GDIPlus_BitmapSetResolution

I see fascists...

Share this post


Link to post
Share on other sites

Hi Lior, try this

#include <GDIPlus.au3>

Opt('MustDeclareVars', 1)

;Global $iMemo

_Main()

Func _Main()
    Local $hBitmap, $hImage ,$ImageHandle,$Status


; Initialize GDI+ library
    _GDIPlus_Startup()

; Capture  bitmap

    $hImage = _GDIPlus_BitmapCreateFromFile (@ScriptDir&"\new.jpg")

; Show horizontal resolution in DPI (dots per inch) of image
    ConsoleWrite("+ " &  _GDIPlus_ImageGetHorizontalResolution($hImage) & " PPI" &@CRLF); only to console
    
    Local $aResult = _GDIPlus_BitmapSetResolution($hImage, 72, 72)
    ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $aResult = ' & $aResult & @crlf & '>Error code: ' & @error & @crlf) 
    
    
    Local $Ext = _GDIPlus_EncodersGetCLSID("JPG")
    $Status = _GDIPlus_ImageSaveToFileEx($hImage,@ScriptDir&"\converted.JPG", $Ext, 0)
    ;MsgBox(0,$hImage,$Status)
    
    
    $hImage = _GDIPlus_BitmapCreateFromFile (@ScriptDir&"\converted.JPG")

; Show horizontal resolution in DPI (dots per inch) of image
    ConsoleWrite("+ " &  _GDIPlus_ImageGetHorizontalResolution($hImage) & " PPI" &@CRLF); only to console
    
    
; Clean up resources
    _GDIPlus_ImageDispose($hImage)
    _WinAPI_DeleteObject($hBitmap)
    
; Shut down GDI+ library
    _GDIPlus_Shutdown()


EndFunc  ;==>_Main


Func _GDIPlus_BitmapSetResolution($hImage, $iPPIHoriz, $iPPIVert)
    ;Author(s): rover, Lior Berezinski, et al.
    If ($hImage = -1) Or (Not $hImage) Then Return SetError(4, 0, 0)
    Local $aResult, $iError = 0
    $aResult = DllCall($ghGDIPDll, "int", "GdipBitmapSetResolution", _
            "hwnd", $hImage, "float", $iPPIHoriz ,"float",$iPPIVert)
    $iError = @error
    If @error Or IsArray($aResult) = 0 Then Return SetError($iError, 0, 0)
    Return SetError($iError, 0, $aResult[0])
EndFunc   ;==>_GDIPlus_BitmapSetResolution

it runs but it does not change the DPI.

console log:

+ 72 DPI

@@ Debug(28) : $aResult = 0

>Error code: 0

+ 72 DPI

thanks for you help

Share this post


Link to post
Share on other sites

it runs but it does not change the DPI.

console log:

+ 72 DPI

@@ Debug(28) : $aResult = 0

>Error code: 0

+ 72 DPI

thanks for you help

Lior

the function works, I'm not so sure its done anything to the image, other than change that value :D

I'm not familiar with the usage of this function, isn't resizing involved for this to do anything?

_GDIPlus_BitmapSetResolution returns 0 on success

I used a screen cap jpg from a help file example: GDIPlus_ImageSaveToFile ($hImage, @MyDocumentsDir & "\GDIPlus_Image.jpg")

the saved files resolution was changed from 96 to 72

but when tested with other non-GDI+ saved jpegs, the bitmap was not saved with a new resolution,

a consolewrite of ImageGetHorizontalResolution after BitmapSetResolution shows the

image resolution was changed but wasn't saved in the jpg.

saving as bmp or png works but not with jpg.

not much of anything on MSDN or other sites that I could find on this.

Good luck


I see fascists...

Share this post


Link to post
Share on other sites

Lior

the function works, I'm not so sure its done anything to the image, other than change that value :D

I'm not familiar with the usage of this function, isn't resizing involved for this to do anything?

_GDIPlus_BitmapSetResolution returns 0 on success

I used a screen cap jpg from a help file example: GDIPlus_ImageSaveToFile ($hImage, @MyDocumentsDir & "\GDIPlus_Image.jpg")

the saved files resolution was changed from 96 to 72

but when tested with other non-GDI+ saved jpegs, the bitmap was not saved with a new resolution,

a consolewrite of ImageGetHorizontalResolution after BitmapSetResolution shows the

image resolution was changed but wasn't saved in the jpg.

saving as bmp or png works but not with jpg.

not much of anything on MSDN or other sites that I could find on this.

Good luck

SOLVED

Thanks it works with PNG.

Share this post


Link to post
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
Sign in to follow this  
Followers 0