Jump to content

Set pixels per inch using GDI+


 Share

Recommended Posts

Hi,

I have some code which resizes an image so the longest side is a prespecified value. Resizing is done which retains the aspect ratio of the image.

Is there any function of GDI+ which allows me to specify the ppi or dpi of the image? Currently, all of my resized images are set to 96dpi, but I'd also like to be able to set them to 72dpi.

Thanks for any help, and here is some of the core code

$hWnd = _WinAPI_GetDesktopWindow()
        $hDC = _WinAPI_GetDC($hWnd)
        $hBMP = _WinAPI_CreateCompatibleBitmap($hDC, $iW, $iH)
        _WinAPI_ReleaseDC($hWnd, $hDC)
        $hImage1 = _GDIPlus_BitmapCreateFromHBITMAP($hBMP)
        $hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage1)
        _GDIPlus_GraphicsDrawImageRect($hGraphic, $hImage2, 0, 0, $iW, $iH)
        $CLSID = _GDIPlus_EncodersGetCLSID($Ext)
        _GDIPlus_ImageSaveToFileEx($hImage1, $sOutImage, $CLSID)
        _GDIPlus_ImageDispose($hImage1)
        _GDIPlus_ImageDispose($hImage2)
        _GDIPlus_GraphicsDispose($hGraphic)
        _WinAPI_DeleteObject($hBMP)
        _GDIPlus_Shutdown()
Link to comment
Share on other sites

Maybe this could help you (taken from GDIP.au3):

; #FUNCTION# ====================================================================================================================
; Name...........: _GDIPlus_BitmapSetResolution
; Description ...: Sets the resolution of this Bitmap object
; Syntax.........: _GDIPlus_BitmapSetResolution($hBitmap, $nDpiX, $nDpiY)
; Parameters ....: $hBitmap - Pointer to the Bitmap object
;   $nDpiX  - Value that specifies the horizontal resolution in dots per inch.
;   $nDpiX  - Value that specifies the vertical resolution in dots per inch.
; Return values .: Success  - True
;   Failure     - False and either:
;   |@error and @extended are set if DllCall failed
;   |$GDIP_STATUS contains a non zero value specifying the error code
; Remarks .......: None
; Related .......:
; Link ..........; @@MsdnLink@@ GdipBitmapSetResolution
; Example .......; Yes
; ===============================================================================================================================
Func _GDIPlus_BitmapSetResolution($hBitmap, $nDpiX, $nDpiY)
    Local $aResult = DllCall($ghGDIPDll, "uint", "GdipBitmapSetResolution", "hwnd", $hBitmap, "float", $nDpiX, "float", $nDpiY)

    If @error Then Return SetError(@error, @extended, False)
    $GDIP_STATUS = $aResult[0]
    Return $aResult[0] = 0
EndFunc ;==>_GDIPlus_BitmapSetResolution

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

Maybe this could help you (taken from GDIP.au3):

; #FUNCTION# ====================================================================================================================
; Name...........: _GDIPlus_BitmapSetResolution

Br,

UEZ

$hWnd = _WinAPI_GetDesktopWindow()
        $hDC = _WinAPI_GetDC($hWnd)
        $hBMP = _WinAPI_CreateCompatibleBitmap($hDC, $iW, $iH)
        _WinAPI_ReleaseDC($hWnd, $hDC)
        $hImage1 = _GDIPlus_BitmapCreateFromHBITMAP($hBMP)
        
        _GDIPlus_BitmapSetResolution($hImage1, 72, 72)
        
        $hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage1)
        _GDIPlus_GraphicsDrawImageRect($hGraphic, $hImage2, 0, 0, $iW, $iH)
        $CLSID = _GDIPlus_EncodersGetCLSID($Ext)
        _GDIPlus_ImageSaveToFileEx($hImage1, $sOutImage, $CLSID)
        _GDIPlus_ImageDispose($hImage1)
        _GDIPlus_ImageDispose($hImage2)
        _GDIPlus_GraphicsDispose($hGraphic)
        _WinAPI_DeleteObject($hBMP)
        _GDIPlus_Shutdown()

That works a charm UEZ, thanks. I simply needed to add 1 further function call. I didn't know about gdip.au3, so thanks to Authenticity too.

Mfg,

D

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