Jump to content

GDIPlus How to save a Graphic Line to file


ReFran
 Share

Recommended Posts

Hi, this is my first try with GdiPlus.   

I write a polyline on a transparent GUI (that works) and want to save it as bmp. 

But I don't get it saved. Only blank bmp. A little bit help would be fine, Reinhard

 

#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <windowsconstants.au3>


;track data Bernepark (distance, elevation in meter) (x,y)
$s = ''
    $s &= '0.0001,72;276,70;375,70;473,71;663,73;827,75;956,74;1556,74;1736,75;'
    $s &= '1954,76;2146,77;2449,77;2556,77;3000,75;3737,66;4124,56;4454,51;4686,47;4877,46;'
    $s &= '5243,39;5390,36;5478,34;5996,31;6886,35;7776,31;8565,34;8687,33;9298,35;9336,34;'
    $s &= '10379,34;10789,34;11095,33;11375,33;11491,35;11545,35;12129,37;12489,35;13034,35;13242,35;'
    $s &= '13421,36;13503,36;13606,34;13647,34;13718,35;14467,39;14625,46;15011,49;15154,58;15244,56;'
    $s &= '15358,56;15622,59;15704,63;15796,66;15838,69;15876,67;15921,68;15953,69;15986,71;16058,73;'
    $s &= '16083,73;16104,72;16124,72;16149,72;16171,72;16193,71;16375,70;16418,69;16439,70;16455,70;'
    $s &= '16499,72;16596,69;16717,65;16756,64;16805,64;16846,65;16886,68;16977,70;17046,70;17198,74;'
    $s &= '17245,74;17267,73;17295,72;17376,68;17427,66;17490,66;17566,68;17592,69;17763,70;17785,70;'
    $s &= '17846,70;18042,72;18076,73;18236,76;18306,76;18385,76;18544,76;18571,75;18709,75;18790,75'

$s = stringstripWS($s,8)
$ga = stringsplit($s,";")


Dim $maxEle = 80 ;ytop
Dim $minEle = 25 ;y0
Dim $maxMtr = 18790
Dim $minMtr = 0
Dim $gui_x = 205
Dim $gui_y = 60

Example()

Func Example()
    Local $hGUI, $trkGraphic, $hPen

    ; Create transparent GUI
    $hGUI = GUICreate("GDI+", $gui_x, $gui_y, 41, 325, $WS_POPUP, $WS_EX_LAYERED  + $WS_EX_TOPMOST)
    GUISetBkColor(0x01, $hgui)
    _WinAPI_SetLayeredWindowAttributes($hgui, 0x01, 255) ;works with$WS_EX_LAYERED
    GUISetState(@SW_SHOW)

    ; Draw line
    _GDIPlus_Startup()
    $trkGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUI)
    $hPen = _GDIPlus_PenCreate(0x55000099,1)  ;pen for frame
    $hPen2 = _GDIPlus_PenCreate(0xFFFF8060,3) ;pen for polyline

    ;draw a line in the middle
    ;;_GDIPlus_GraphicsDrawLine ( $trkGraphics, $nX1, $nY1, $nX2, $nY2 [, $hPen = 0] )
    _GDIPlus_GraphicsDrawLine($trkGraphic, 0, $gui_y/2, $gui_x, $gui_y/2, $hPen)
    ;draw a frame
    _GDIPlus_GraphicsDrawLine($trkGraphic, 0, 0, $gui_x, 0, $hPen)
    _GDIPlus_GraphicsDrawLine($trkGraphic, 0, $gui_y-1, $gui_x, $gui_y-1, $hPen)
    _GDIPlus_GraphicsDrawLine($trkGraphic, $gui_x-1, $gui_y-1, $gui_x-1, 0, $hPen)
    _GDIPlus_GraphicsDrawLine($trkGraphic, 0, $gui_x-1, 1, 0, $hPen)

    ;draw the polyline for the track
    for $i = 1 to $ga[0]-2
        $af = stringsplit($ga[$i], ",")
        $at = stringsplit($ga[$i+1], ",")

        $yf = (1 - (($af[2]-$minele) / ($maxele - $minele))) * $gui_y
        $xf = $af[1] / $maxMtr * ($gui_x)

        $yt = (1 - (($at[2]-$minele) / ($maxele-$minele))) * $gui_y
        $xt = $at[1] / $maxMtr * ($gui_x)
        ;msgbox(0,"",  $xf &", " &$yf &", " &$yt &", "&$xt)
        _GDIPlus_GraphicsDrawLine($trkGraphic, $xf, $yf, $xt, $yt, $hPen2)
    next

     $trkImage = _GDIPlus_BitmapCreateFromGraphics ( $gui_x, $gui_y, $trkGraphic )
     _GDIPlus_ImageSaveToFile($trkImage,@ScriptDir&"\test.bmp")
    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE

    ; Clean up resources
    _GDIPlus_PenDispose($hPen)
    _GDIPlus_GraphicsDispose($trkGraphic)
    _GDIPlus_BitmapDispose($trkImage)
    _GDIPlus_Shutdown()
EndFunc   ;==>Example

 

Link to comment
Share on other sites

You cannot save a graphic context directly - only bitmaps / images.

Here one approach:

#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <windowsconstants.au3>


;track data Bernepark (distance, elevation in meter) (x,y)
$s = ''
    $s &= '0.0001,72;276,70;375,70;473,71;663,73;827,75;956,74;1556,74;1736,75;'
    $s &= '1954,76;2146,77;2449,77;2556,77;3000,75;3737,66;4124,56;4454,51;4686,47;4877,46;'
    $s &= '5243,39;5390,36;5478,34;5996,31;6886,35;7776,31;8565,34;8687,33;9298,35;9336,34;'
    $s &= '10379,34;10789,34;11095,33;11375,33;11491,35;11545,35;12129,37;12489,35;13034,35;13242,35;'
    $s &= '13421,36;13503,36;13606,34;13647,34;13718,35;14467,39;14625,46;15011,49;15154,58;15244,56;'
    $s &= '15358,56;15622,59;15704,63;15796,66;15838,69;15876,67;15921,68;15953,69;15986,71;16058,73;'
    $s &= '16083,73;16104,72;16124,72;16149,72;16171,72;16193,71;16375,70;16418,69;16439,70;16455,70;'
    $s &= '16499,72;16596,69;16717,65;16756,64;16805,64;16846,65;16886,68;16977,70;17046,70;17198,74;'
    $s &= '17245,74;17267,73;17295,72;17376,68;17427,66;17490,66;17566,68;17592,69;17763,70;17785,70;'
    $s &= '17846,70;18042,72;18076,73;18236,76;18306,76;18385,76;18544,76;18571,75;18709,75;18790,75'

$s = stringstripWS($s,8)
$ga = stringsplit($s,";")


Dim $maxEle = 80 ;ytop
Dim $minEle = 25 ;y0
Dim $maxMtr = 18790
Dim $minMtr = 0
Dim $gui_x = 205
Dim $gui_y = 60

Example()

Func Example()
    Local $hGUI, $trkGraphic, $hPen

    ; Create transparent GUI
    $hGUI = GUICreate("GDI+", $gui_x, $gui_y, 41, 325, $WS_POPUP, $WS_EX_LAYERED  + $WS_EX_TOPMOST)
    GUISetBkColor(0x812345, $hgui)
    _WinAPI_SetLayeredWindowAttributes($hgui, 0xFF812345, 255) ;works with$WS_EX_LAYERED
    GUISetState(@SW_SHOW)

    ; Draw line
    _GDIPlus_Startup()
    $hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUI)
    $hBitmap = _GDIPlus_BitmapCreateFromScan0($gui_x, $gui_y)
    $trkGraphic = _GDIPlus_ImageGetGraphicsContext($hBitmap)
    _GDIPlus_GraphicsSetSmoothingMode($trkGraphic, 4)
    _GDIPlus_GraphicsSetPixelOffsetMode($trkGraphic, 4)

    $hPen = _GDIPlus_PenCreate(0x55000099,1)  ;pen for frame
    $hPen2 = _GDIPlus_PenCreate(0xFFFF8060,3) ;pen for polyline

    ;draw a line in the middle
    ;;_GDIPlus_GraphicsDrawLine ( $trkGraphics, $nX1, $nY1, $nX2, $nY2 [, $hPen = 0] )
    _GDIPlus_GraphicsDrawLine($trkGraphic, 0, $gui_y/2, $gui_x, $gui_y/2, $hPen)
    ;draw a frame
    _GDIPlus_GraphicsDrawLine($trkGraphic, 0, 0, $gui_x, 0, $hPen)
    _GDIPlus_GraphicsDrawLine($trkGraphic, 0, $gui_y-1, $gui_x, $gui_y-1, $hPen)
    _GDIPlus_GraphicsDrawLine($trkGraphic, $gui_x-1, $gui_y-1, $gui_x-1, 0, $hPen)
    _GDIPlus_GraphicsDrawLine($trkGraphic, 0, $gui_x-1, 1, 0, $hPen)

    ;draw the polyline for the track
    for $i = 1 to $ga[0]-2
        $af = stringsplit($ga[$i], ",")
        $at = stringsplit($ga[$i+1], ",")

        $yf = (1 - (($af[2]-$minele) / ($maxele - $minele))) * $gui_y
        $xf = $af[1] / $maxMtr * ($gui_x)

        $yt = (1 - (($at[2]-$minele) / ($maxele-$minele))) * $gui_y
        $xt = $at[1] / $maxMtr * ($gui_x)
        ;msgbox(0,"",  $xf &", " &$yf &", " &$yt &", "&$xt)
        _GDIPlus_GraphicsDrawLine($trkGraphic, $xf, $yf, $xt, $yt, $hPen2)
    next
    _GDIPlus_ImageSaveToFile($hBitmap,@ScriptDir&"\test.png")
    _GDIPlus_GraphicsDrawImageRect($hGraphic, $hBitmap, 0, 0, $gui_x, $gui_y)
    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE

    ; Clean up resources
    _GDIPlus_PenDispose($hPen)
    _GDIPlus_PenDispose($hPen2)
    _GDIPlus_GraphicsDispose($trkGraphic)
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_BitmapDispose($hBitmap)
    _GDIPlus_Shutdown()
EndFunc   ;==>Example

 

Edited by 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

"You cannot save a graphic context directly - only bitmaps / images."

Hi, thought could be done more direct.

Thanks for the working example.

Maybe I work with "_GDIPlus_BitmapCreateFromFile" to load a background image and then draw on it.

Also I don't need the transparent GUI  really . So maybe I change that in order to get a white background.

Have to play around with your example.

 

Thanks for your help and have a nice (rest-) evening. Gute Nacht, Reinhard

Edited by ReFran
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...