Jump to content

GDI Plus Image Save to File


Adol
 Share

Recommended Posts

Hi everyone.

I would like to know how to save to file a bitmap object created by cloning several sections of another one (bitmap object) in a GUI's picture control using GDI+.

I made a basic bitmap scrambler that works fine, the only two things I need are the save to file part and maybe to make the images stay in GUI after it's minimized or a window is drawn above it.

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GDIPlus.au3>
;
;Declare Variables
Dim Const $Logo = @ProgramFilesDir & "AutoIt3ExamplesGUIlogo4.gif"
Dim $Gfx1, $Gfx2
;
;Create GUI
$Form1 = GUICreate("Form1", 172, 225, 440, 313)
$Label1 = GUICtrlCreateLabel("Insert a 0-3  4 numbers pattern:", 0, 72, 169, 17, $SS_CENTER)
$Input1 = GUICtrlCreateInput("3210", 0, 96, 169, 21)
GUICtrlSetLimit(-1, 4, 4)
$Button1 = GUICtrlCreateButton("Rearrange", 0, 120, 171, 25)
$Pic1 = GUICtrlCreatePic("", 0, 0, 169, 68)
$Pic2 = GUICtrlCreatePic("", 0, 152, 169, 68)
GUISetState(@SW_SHOW)
;
; Register WM_PAINT for Painting
GUIRegisterMsg(0xF, "_Paint") ;WM_PAINT
;
_GDIPlus_Startup(); Initialize GDI+ library
;
$Gfx1 = _GDIPlus_GraphicsCreateFromHWND(ControlGetHandle($Form1, "", $Pic1))
$Gfx2 = _GDIPlus_GraphicsCreateFromHWND(ControlGetHandle($Form1, "", $Pic2))
Dim Const $Bmp = _GDIPlus_BitmapCreateFromFile($Logo)
Dim Const $iX = _GDIPlus_ImageGetWidth($Bmp)
Dim Const $iY = _GDIPlus_ImageGetHeight($Bmp)
Dim Const $TileWidth = $iX / 4
Dim Const $TileHeight = $iY
_GDIPlus_GraphicsDrawImageRect($Gfx1, $Bmp, 0, 0, $iX, $iY)
_GDIPlus_GraphicsDrawImageRect($Gfx2, $Bmp, 0, 0, $iX, $iY)
;
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
            ; Clean up resources
            _GDIPlus_GraphicsDispose($Gfx1)
            _GDIPlus_GraphicsDispose($Gfx2)
            _GDIPlus_BitmapDispose($Bmp)
            ;
            ; Shut down GDI+ library
            _GDIPlus_Shutdown()
            ;
        Case $Button1
            $Pattern = GUICtrlRead($Input1)
            ;
            ;Check Pattern Validity
            $asResult = StringRegExp($Pattern, '([0-3]{4,4})', 1)
            If @error Then
                MsgBox(0, "Invalid pattern", "Pattern must contain 4 numbers from 0 to 3")
            Else
                $aPattern = StringRegExp($Pattern, '([0-3])', 3)
                For $i = 0 To UBound($aPattern) - 1 Step 1
                $Tile = _GDIPlus_BitmapCloneArea($Bmp, $TileWidth * $i, 0, $TileWidth, $TileHeight)
                _GDIPlus_GraphicsDrawImageRect($Gfx2, $Tile, $TileWidth * $aPattern[$i], 0, $TileWidth, $TileHeight)
                ;
                ;Export Scrambled Bitmap (Not Working)
                $BmpExport = _GDIPlus_BitmapCreateFromGraphics($iX, $iY, $Gfx2)
                _GDIPlus_ImageSaveToFile($BmpExport, @MyDocumentsDir & "GDIPlus_Export.gif");<====this produces a black bitmap
                Next
            EndIf
    EndSwitch
WEnd
;~ Functions:
Func _Paint();<====================================================================================this is not even working
    _GDIPlus_GraphicsDrawImageRect($Gfx1, $Bmp, 0, 0, $iX, $iY)
    _GDIPlus_GraphicsDrawImageRect($Gfx2, $BmpExport, 0, 0, $iX, $iY)
EndFunc   ;==>_Paint

I'd appreciate any help you can give.

Link to comment
Share on other sites

Try this:

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GDIPlus.au3>
;
;Declare Variables
Dim Const $Logo = @ProgramFilesDir & "AutoIt3ExamplesGUIlogo4.gif"
Dim $Gfx1, $Gfx2
;
;Create GUI
$Form1 = GUICreate("Form1", 172, 225, 440, 313)
$Label1 = GUICtrlCreateLabel("Insert a 0-3  4 numbers pattern:", 0, 72, 169, 17, $SS_CENTER)
$Input1 = GUICtrlCreateInput("3210", 0, 96, 169, 21)
GUICtrlSetLimit(-1, 4, 4)
$Button1 = GUICtrlCreateButton("Rearrange", 0, 120, 171, 25)
$Pic1 = GUICtrlCreatePic("", 0, 0, 169, 68)
$Pic2 = GUICtrlCreatePic("", 0, 152, 169, 68)
GUISetState(@SW_SHOW)
;
; Register WM_PAINT for Painting
GUIRegisterMsg(0xF, "_Paint") ;WM_PAINT
;
_GDIPlus_Startup(); Initialize GDI+ library
;
$Gfx1 = _GDIPlus_GraphicsCreateFromHWND(ControlGetHandle($Form1, "", $Pic1))
$Gfx2 = _GDIPlus_GraphicsCreateFromHWND(ControlGetHandle($Form1, "", $Pic2))
$hBackbuffer = _GDIPlus_BitmapCreateFromGraphics(169, 68, $Gfx2)
$hCtxt = _GDIPlus_ImageGetGraphicsContext($hBackbuffer)

Dim Const $Bmp = _GDIPlus_BitmapCreateFromFile($Logo)
Dim Const $iX = _GDIPlus_ImageGetWidth($Bmp)
Dim Const $iY = _GDIPlus_ImageGetHeight($Bmp)
Dim Const $TileWidth = $iX / 4
Dim Const $TileHeight = $iY
_GDIPlus_GraphicsDrawImageRect($Gfx1, $Bmp, 0, 0, $iX, $iY)
_GDIPlus_GraphicsDrawImageRect($hCtxt, $Bmp, 0, 0, $iX, $iY)
_GDIPlus_GraphicsDrawImageRect($Gfx2, $Bmp, 0, 0, $iX, $iY)
;
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
            ; Clean up resources
            _GDIPlus_GraphicsDispose($Gfx1)
            _GDIPlus_GraphicsDispose($Gfx2)
            _GDIPlus_GraphicsDispose($hCtxt)
            _GDIPlus_BitmapDispose($Bmp)
            _GDIPlus_BitmapDispose($hBackbuffer)
            ;
            ; Shut down GDI+ library
            _GDIPlus_Shutdown()
            ;
        Case $Button1
            $Pattern = GUICtrlRead($Input1)
            ;
            ;Check Pattern Validity
            $asResult = StringRegExp($Pattern, '([0-3]{4,4})', 1)
            If @error Then
                MsgBox(0, "Invalid pattern", "Pattern must contain 4 numbers from 0 to 3")
            Else
                $aPattern = StringRegExp($Pattern, '([0-3])', 3)
                For $i = 0 To UBound($aPattern) - 1 Step 1
                $Tile = _GDIPlus_BitmapCloneArea($Bmp, $TileWidth * $i, 0, $TileWidth, $TileHeight)
                _GDIPlus_GraphicsDrawImageRect($hCtxt , $Tile, $TileWidth * $aPattern[$i], 0, $TileWidth, $TileHeight)
                _GDIPlus_GraphicsDrawImage($Gfx2 , $hBackbuffer, 0, 0)

                ;
                ;Export Scrambled Bitmap (Not Working)
                _GDIPlus_ImageSaveToFile($hBackbuffer, @MyDocumentsDir & "GDIPlus_Export.gif");<====this produces a black bitmap
                Next
            EndIf
    EndSwitch
WEnd
;~ Functions:
Func _Paint($hWnd, $msg, $wParam, $lParam);
    _WinAPI_RedrawWindow($hWnd, "", "", BitOR($RDW_INVALIDATE, $RDW_UPDATENOW, $RDW_FRAME)); , $RDW_ALLCHILDREN
    _GDIPlus_GraphicsDrawImageRect($Gfx1, $Bmp, 0, 0, $iX, $iY)
    _GDIPlus_GraphicsDrawImage($Gfx2 , $hBackbuffer, 0, 0)
      Return "GUI_RUNDEFMSG"
EndFunc   ;==>_Paint

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

@UEZ: Wow that was helpful (and awesome), I really appreciate this.

The only thing left that I'm concerned about, is the gif palette changed and it got dithered, is it possible to control this behavior?

Link to comment
Share on other sites

Not without using another format. The GDI+ GIF encoder can only save GIFs as 8-bpp.

Yes, the base image is a standard gif too, but it seems to get re-encoded and dithered.

By the way, the only true-color gif I've seen it's slow enough to be undesired, specially when there are png's, which makes me wonder why the format hasn't been implemented in autoit.

Link to comment
Share on other sites

PNG are implemented! Just use

_GDIPlus_ImageSaveToFile($hBackbuffer, @MyDocumentsDir & "GDIPlus_Export.png")

and the bitmap will be saved as a PNG.

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