iea has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

i´m trying to use the CopyToGD function to "convert" a DIBitmap to a GD

Win32-GUI-DIBitmap

Here is what i've got.
use Win32::API; use Win32::GUI::DIBitmap; use GD; $GetDC = new Win32::API('user32','GetDC',['N'],'N'); my $dc = $GetDC->Call(0); my $dib = newFromDC Win32::GUI::DIBitmap ($dc); my $width = $dib->GetWidth() ; my $height = $dib->GetHeight() ; my $GDimage = new GD::Image($w,$h); $dib->CopyToGD($GDimage,$dib) ; #do something with the gd object #.... open (DISPLAY, ">test.png"); binmode DISPLAY; print DISPLAY $GDimage->png; close (DISPLAY);

I keep getting the error:
gd-png:  fatal libpng error: Image width or height is zero in IHDR
gd-png error: setjmp returns error condition

Any ideas what i´m missing?
Thanks

Replies are listed 'Best First'.
Re: Convert DIBitmap to GD
by BrowserUk (Patriarch) on Apr 10, 2010 at 09:51 UTC

    I suspect it is this line causing the problem:

    $dib->CopyToGD($GDimage,$dib); ........................^^^ ## should be a boolean indicating whether 8-bit or 24-bit color.

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Oh dear!
      Thanks

      Now the image is created and the error is gone, but the GD image stays black.
      use Win32::API; use Win32::GUI::DIBitmap; use GD; $GetDC = new Win32::API('user32','GetDC',['N'],'N'); my $dc = $GetDC->Call(0); my $dib = newFromDC Win32::GUI::DIBitmap ($dc); $dib->SaveToFile("DIBitmap.png"); my $width = $dib->GetWidth() ; my $height = $dib->GetHeight() ; my $GDimage = new GD::Image($width,$height,1); $dib->CopyToGD($GDimage,1) ; #do something with the gd object #.... open (DISPLAY, ">GD.png"); binmode DISPLAY; print DISPLAY $GDimage->png; close (DISPLAY);


      Thanks again

        That seems to be a bug in the CopyToGD() method.

        It can load the png you write to disk without error.

        It can also successfully create a GD image from the data returned by the SaveToData() method:

        use Win32::API; use Win32::GUI::DIBitmap; use GD; $GetDC = new Win32::API('user32','GetDC',['N'],'N'); my $dc = $GetDC->Call(0); my $dib = newFromDC Win32::GUI::DIBitmap ($dc); $dib->SaveToFile("DIBitmap.png"); #my $GDimage = GD::Image->newFromPng( "DIBitmap.png", 1 ); my $mem = $dib->SaveToData( Win32::GUI::DIBitmap::GetFIFFromFormat( 'P +NG' ) ); my $GDimage = GD::Image->newFromPngData( $mem, 1 ); open (DISPLAY, ">GD.png"); binmode DISPLAY; print DISPLAY $GDimage->png; close (DISPLAY);

        You should raise a bug report with the author.

        The problem probably stems from the mismatch between the version of the GDImageStruct used by DIBitmap & the version used by the latest GD incarnations:

        Compare: (<)C:\test\GDstruct.gd (3146 bytes) with: (>)C:\test\GDstruct.dib (3124 bytes) 63,67c63 < /* There should NEVER BE ACCESSOR MACROS FOR ITEMS BELOW HERE, s +o this < part of the structure can be safely changed in new releases. +*/ < < /* 2.0.12: anti-aliased globals. 2.0.26: just a few vestiges aft +er < switching to the fast, memory-cheap implementation from PHP-gd +. */ --- > /* 2.0.12: anti-aliased globals */ 71a67,78 > unsigned char **AA_opacity; > int AA_polygon; > /* Stored and pre-computed variables for determining the perpend +icular > distance from a point to the anti-aliased line being drawn: * +/ > int AAL_x1; > int AAL_y1; > int AAL_x2; > int AAL_y2; > int AAL_Bx_Ax; > int AAL_By_Ay; > int AAL_LAB_2; > float AAL_LAB; 79,82d87 < < /* 2.1.0: allows to specify resolution in dpi */ < unsigned int res_x; < unsigned int res_y; 84,85d88 < <

        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.