Jump to content
KKZiomek

How do I convert an 2d array of rgb hex colors into an png file?

Recommended Posts

KKZiomek

Hi, I recently tried to creat something that generates random pixels, and then saves it as a png file.

So, to do it, I made a 2d array, and then iterated through every element of the array and assigned it a random hex rgb color. Now I need to convert this 2d array into an image. (Doesn't actually have to be png). How would I do it? Would I use GDI+?

Share this post


Link to post
Share on other sites
UEZ

This is an example to create a bitmap from a 2D array.

 

#include <GDIPlus.au3>

_GDIPlus_Startup()
Global Const $iWidth = 100, $iHeight = 100
Global $aPixelColors[$iHeight + 1][$iWidth + 1], $iX, $iY

;create some random colors values saved in an array
For $iY = 0 To $iHeight
    For $iX = 0 To $iWidth
        $aPixelColors[$iY][$iX] = Random(0xFF000000, 0xFFFFFFFF, 1) ;some random colors where the alpha channel is opaque
    Next
Next

;copy the colors from the array to a bitmap
Global Const $hBitmap = _GDIPlus_BitmapCreateFromScan0($iWidth, $iHeight), $hGfx = _GDIPlus_ImageGetGraphicsContext($hBitmap), $hBrush = _GDIPlus_BrushCreateSolid()
For $iY = 0 To $iHeight
    For $iX = 0 To $iWidth
        _GDIPlus_BrushSetSolidColor($hBrush, $aPixelColors[$iY][$iX])
        _GDIPlus_GraphicsFillRect($hGfx, $iX, $iY, 1, 1, $hBrush) ;create the pixel according to the color in the appropriate array cell
    Next
Next

;save the bitmap in PNG format
_GDIPlus_ImageSaveToFile($hBitmap, @ScriptDir & "\SomeRandomColors.png") ;just use the extension as the output format

;cleanup resources
_GDIPlus_BrushDispose($hBrush)
_GDIPlus_ImageDispose($hGfx)
_GDIPlus_BitmapDispose($hBitmap)
_GDIPlus_Shutdown()

ShellExecute(@ScriptDir & "\SomeRandomColors.png")

 


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

Share this post


Link to post
Share on other sites
Malkey

Instead of using _GDIPlus_BrushSetSolidColor and _GDIPlus_GraphicsFillRect as in UEZ's example, this example uses _GDIPlus_BitmapSetPixel and _GDIPlus_GraphicsDrawImage.
Not better, just different.   And playing around, I changed the noisy pattern that was in the array.

#include <GDIPlus.au3>

_GDIPlus_Startup()
Global Const $iWidth = 300, $iHeight = 200
Global $aPixelColors[$iHeight + 1][$iWidth + 1], $iX, $iY

; Create a cross in colours saved in an array
; Colours in ARGB colour (Alpha,Red,Green,Blue)  hex format 0xAARRGGBB
For $iY = 0 To $iHeight
    For $iX = 0 To $iWidth
        If $iX > $iWidth / 2 - 5 And $iX < $iWidth / 2 + 5 Then
            $aPixelColors[$iY][$iX] = 0x80FF0000 ; Vertical line
        ElseIf $iY > $iHeight / 2 - 5 And $iY < $iHeight / 2 + 5 Then
            $aPixelColors[$iY][$iX] = 0xFF0000FF ; Horizontal line
        Else
            $aPixelColors[$iY][$iX] = 0x00FFFFFF ; with Alpha channel zero, background is transparent. For fully opaque change colour to 0xFFFFFFFF, where Alpha channel is 0xFF (255).
        EndIf
    Next
Next

;copy the colors from the array to a bitmap
Global Const $hBitmap = _GDIPlus_BitmapCreateFromScan0($iWidth, $iHeight), $hGfx = _GDIPlus_ImageGetGraphicsContext($hBitmap)
;Global $hBrush = _GDIPlus_BrushCreateSolid()  ; From UEZ's example
For $iY = 0 To $iHeight
    For $iX = 0 To $iWidth
        _GDIPlus_BitmapSetPixel($hBitmap, $iX, $iY, $aPixelColors[$iY][$iX])
        ;_GDIPlus_BrushSetSolidColor($hBrush, $aPixelColors[$iY][$iX]) ; From UEZ's example
        ;_GDIPlus_GraphicsFillRect($hGfx, $iX, $iY, 1, 1, $hBrush)     ; From UEZ's example
    Next
Next

_GDIPlus_GraphicsDrawImage($hGfx, $hBitmap, 0, 0)
; And/Or the next 4 commands
#cs
_GDIPlus_GraphicsDrawImageRect($hGfx, $hBitmap, 0, 0, $iWidth / 2 - 5, $iHeight / 2)
_GDIPlus_GraphicsDrawImageRect($hGfx, $hBitmap, $iWidth / 2 + 5, 0, $iWidth / 2, $iHeight / 2)
_GDIPlus_GraphicsDrawImageRect($hGfx, $hBitmap, 0, $iHeight / 2, $iWidth / 2 - 5, $iHeight / 2)
_GDIPlus_GraphicsDrawImageRect($hGfx, $hBitmap, $iWidth / 2 + 5, $iHeight / 2, $iWidth / 2, $iHeight / 2)
#ce

;save the bitmap in PNG format
_GDIPlus_ImageSaveToFile($hBitmap, @ScriptDir & "\SomeRandomColors.png") ;just use the extension as the output format

;cleanup resources
;_GDIPlus_BrushDispose($hBrush) ; From UEZ's example
_GDIPlus_ImageDispose($hGfx)
_GDIPlus_BitmapDispose($hBitmap)
_GDIPlus_Shutdown()

ShellExecute(@ScriptDir & "\SomeRandomColors.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

×