Loading PNG image from file to memory buffer using libharu

762 views Asked by At

I want to load PNG image from file to buffer and then to HPDF_Image, but I can't get image from buffer using HPDF_LoadPngImageFromMem, as it returns HPDF_INVALID_PNG_IMAGE.

    // Getting size
    std::ifstream t("3CM//A_3cm2.png");
    t.seekg(0, std::ios::end);
    size_t size = t.tellg();

    // Filling buffer
    std::string buffer(size, ' ');
    t.seekg(0);
    t.read(&buffer[0], size);

    // Loading Image from buffer
    HPDF_Image image = HPDF_LoadPngImageFromMem(pdf, (HPDF_BYTE*) buffer.c_str(), buffer.size());
1

There are 1 answers

0
jbrouwer On

Why do you want to load the image in de memory first ? This code code work fine

HPDF_Doc   Pdf     = HPDF_New                  ( error_handler , NULL   ) ;
HPDF_Page  Page    = HPDF_AddPage              ( Pdf                    ) ;

HPDF_Image MyImage = HPDF_LoadPngImageFromFile ( Pdf ,"3CM//A_3cm2.png" ) ; 
HPDF_REAL  Width   = HPDF_Image_GetWidth       ( MyImage                ) ;
HPDF_REAL  Height  = HPDF_Image_GetHeight      ( MyImage                ) ;

HPDF_Page_DrawImage ( Page , MyImage , x , y , Width , Height ) ;

...