Sign in to follow this  
Followers 0
furrycow

GDI/Screencapture save image as GIF problem

3 posts in this topic

Hi,

I have a blank white bitmap image (bmp1.bmp) 2000 x 4000, filesize 123KB, now, when i do the following code:

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GuiConstantsEx.au3>
#include <GDIPlus.au3>
#Include <ScreenCapture.au3>


$frmTableGenerator = GUICreate("Table Generator", 249, 54, 192, 124)
$labelTableColour = GUICtrlCreateLabel("Table Colour #", 16, 16, 74, 17)
$txtboxTableColour = GUICtrlCreateInput("FF0000", 90, 13, 49, 21)
GUICtrlSetLimit(-1, 6)
$btnGenerate = GUICtrlCreateButton("Generate", 152, 11, 75, 25, $WS_GROUP)
GUISetState(@SW_SHOW)

Global $tblColour,  $hPenColour, $hPenGrey99, $hPenGreyCE, $hPenGreyE0, $hPenGreyE6, $hPenGreyCC, $hPenGreyBD, $hPenGreyAB, $hPenGreyBD, $hPenGreyAB, $hPenGreyD7
Global $hPenGreyB9, $hPenGreyEF, $hPenGreyFF, $hPenGreyF5, $hGraphic

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $btnGenerate
            _DrawTables()
    EndSwitch
WEnd

Func _DrawTables()

    $tblColour = GUICtrlRead($txtboxTableColour)
    If StringLen($tblColour) <> 6 Then
    Msgbox(0,"Error","Invalid HEX value")
    Return
    EndIf

    $tblColour = "0xFF" & $tblColour

    _GDIPlus_Startup ()

    _LTR_Head_r()

    _GDIPlus_PenDispose($hPenColour)
    _GDIPlus_PenDispose($hPenGrey99)
    _GDIPlus_PenDispose($hPenGreyCE)
    _GDIPlus_PenDispose($hPenGreyE0)
    _GDIPlus_PenDispose($hPenGreyE6)
    _GDIPlus_PenDispose($hPenGreyCC)
    _GDIPlus_PenDispose($hPenGreyBD)
    _GDIPlus_PenDispose($hPenGreyAB)
    _GDIPlus_PenDispose($hPenGreyBD)
    _GDIPlus_PenDispose($hPenGreyAB)
    _GDIPlus_PenDispose($hPenGreyD7)
    _GDIPlus_PenDispose($hPenGreyB9)
    _GDIPlus_PenDispose($hPenGreyEF)
    _GDIPlus_PenDispose($hPenGreyFF)
    _GDIPlus_PenDispose($hPenGreyF5)
    _GDIPlus_GraphicsDispose ($hGraphic)
    _GDIPlus_Shutdown ()

EndFunc


Func _LTR_Head_r()

    $hLtrHeadR = _GDIPlus_BitmapCreateFromFile("bmp0.bmp")
    $hGraphic = _GDIPlus_ImageGetGraphicsContext ($hLtrHeadR)
    $hBrushColour = _GDIPlus_BrushCreateSolid($tblColour)
    $hPenColour = _GDIPlus_PenCreate($tblColour);Table Colour
    $hPenGrey99 = _GDIPlus_PenCreate(0xFF999999)
    $hPenGreyCE = _GDIPlus_PenCreate(0xFFC3C3C3)
    $hPenGreyE0 = _GDIPlus_PenCreate(0xFFE0E0E0)
    $hPenGreyE6 = _GDIPlus_PenCreate(0xFFE6E6E6)
    $hPenGreyCC = _GDIPlus_PenCreate(0xFFCCCCCC)
    $hPenGreyBD = _GDIPlus_PenCreate(0xFFBDBDBD)
    $hPenGreyAB = _GDIPlus_PenCreate(0xFFABABAB)
    $hPenGreyBD = _GDIPlus_PenCreate(0xFFBDBDBD)
    $hPenGreyAB = _GDIPlus_PenCreate(0xFFABABAB)
    $hPenGreyD7 = _GDIPlus_PenCreate(0xFFD7D7D7)
    $hPenGreyB9 = _GDIPlus_PenCreate(0xFFB9B9B9)
    $hPenGreyEF = _GDIPlus_PenCreate(0xFFEFEFEF)
    $hPenGreyFF = _GDIPlus_PenCreate(0xFFFFFFFF)
    $hPenGreyF5 = _GDIPlus_PenCreate(0xFFF5F5F5)

    _GDIPlus_GraphicsDrawLine($hGraphic, 1996, 0, 1996, 1, $hPenGreyD7);Right Grey Line 1
    _GDIPlus_GraphicsDrawLine($hGraphic, 1996, 1, 1996, 2, $hPenGreyB9);Right Grey Line 1
    _GDIPlus_GraphicsDrawLine($hGraphic, 1997, 0, 1997, 2, $hPenGreyE6);Right Grey Line 1
    _GDIPlus_GraphicsDrawLine($hGraphic, 1997, 1, 1997, 3, $hPenGreyCC);Right Grey Line 1
    _GDIPlus_GraphicsDrawLine($hGraphic, 1997, 2, 1997, 4, $hPenGreyBD);Right Grey Line 1
    _GDIPlus_GraphicsDrawLine($hGraphic, 1997, 3, 1997, 5, $hPenGreyAB);Right Grey Line 1
    _GDIPlus_GraphicsDrawLine($hGraphic, 1998, 0, 1998, 1, $hPenGreyEF);Right Grey Line 1
    _GDIPlus_GraphicsDrawLine($hGraphic, 1998, 1, 1998, 2, $hPenGreyE6);Right Grey Line 1
    _GDIPlus_GraphicsDrawLine($hGraphic, 1998, 2, 1998, 3, $hPenGreyD7);Right Grey Line 1
    _GDIPlus_GraphicsDrawLine($hGraphic, 1998, 3, 1998, 4, $hPenGreyCC);Right Grey Line 1
    _GDIPlus_GraphicsDrawLine($hGraphic, 1999, 0, 1999, 1, $hPenGreyFF);Right Grey Line 1
    _GDIPlus_GraphicsDrawLine($hGraphic, 1999, 1, 1999, 2, $hPenGreyF5);Right Grey Line 1
    _GDIPlus_GraphicsDrawLine($hGraphic, 1999, 2, 1999, 3, $hPenGreyEF);Right Grey Line 1

    _GDIPlus_GraphicsFillRect($hGraphic, 0, 2, 1995, 3998, $hBrushColour)
    _GDIPlus_GraphicsDrawLine($hGraphic, 0, 0, 1995, 0, $hPenColour);Top Colour Line
    _GDIPlus_GraphicsDrawLine($hGraphic, 1995, 0, 1995, 1, $hPenColour);Top Colour Dot
    _GDIPlus_GraphicsDrawLine($hGraphic, 1996, 2, 1996, 4000, $hPenColour);Right Colour Line

    _GDIPlus_GraphicsDrawLine($hGraphic, 1997, 4, 1997, 4000, $hPenGrey99);Right Grey Line 1
    _GDIPlus_GraphicsDrawLine($hGraphic, 1998, 4, 1998, 4000, $hPenGreyCE);Right Grey Line 2
    _GDIPlus_GraphicsDrawLine($hGraphic, 1999, 5, 1999, 4000, $hPenGreyE0);Right Grey Line 3
    _GDIPlus_GraphicsDrawLine($hGraphic, 1999, 3, 1999, 4, $hPenGreyE6);Right Grey Line 4

    _GDIPlus_ImageSaveToFile ($hLtrHeadR, "bmp2.bmp")

    $hBitmap = _GDIPlus_ImageLoadFromFile("bmp2.bmp")
    $hBMP = _GDIPlus_BitmapCreateHBITMAPFromBitmap ($hBitmap)
    _ScreenCapture_SetBMPFormat(0)

    _ScreenCapture_SaveImage("gif2.gif", $hBMP, True)

EndFunc   ;==>_LTR_Head_r

It saves the generated bitmap (bmp2.bmp) at ~22MB and saves the generated gif2.gif at 22KB. Which is great for the GIF, HOWEVER if you zoom in as far as you can and look at the TOP RIGHT of the image on both the attached gif1.gif and the gif2.gif, gif1.gif looks exactly how i want it (but is manually created) and gif2.gif completely messes up, and seems to do dotted lines for greys brighter than #CCCCCC, the generated bmp2.bmp looks exactly how i want it to too, so i am unsure whether the problem is in my code.

I really want to have the file format in GIF, and for it to be around the 20KB mark, which i know is possible, as when i use Paint or another such program, it saves a GIF at around 19KB (and looking how i want it to!!)

I have attached:

- bmp1.bmp = the original blank BMP file. (~123KB)

- gif1.gif = a GIF file looking how i want it, manually made in paint. (~19KB)

- gif2.gif = a GIF file generated using this code (~22KB)

Any suggestions as to where it is all going horribly wrong would be great!

Thanks in advance!

post-39869-0-21994600-1301221881_thumb.g

post-39869-0-25152300-1301221886_thumb.g

bmp1.bmp


Instant Lockerz Invite - www.instantlockerzinvite.co.uk

Share this post


Link to post
Share on other sites



First, bmp1.bmp is a jpg and not a bmp.

As you might know GIF is only 8bit and thus when saving it to GIF the internal function is dithering

the image and that is the reason why it is messed up.

How about to save it to PNG?

Just replace _GDIPlus_ImageSaveToFile ($hLtrHeadR, "bmp2.bmp") to _GDIPlus_ImageSaveToFile ($hLtrHeadR, "bmp2.png"). The rest of the lines are not needed anymore.

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

Share this post


Link to post
Share on other sites

Thank you ever so much UEZ, although the image is still around 120KB, it should be ok.

Cheers for putting the time in!


Instant Lockerz Invite - www.instantlockerzinvite.co.uk

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