[GH-ISSUE #274] Incorrect image height #218

Closed
opened 2026-05-05 11:57:26 -06:00 by gitea-mirror · 8 comments
Owner

Originally created by @RaFaeL-NN on GitHub (Feb 29, 2020).
Original GitHub issue: https://github.com/jmcnamara/libxlsxwriter/issues/274

Originally assigned to: @jmcnamara on GitHub.

examples\images.c

`

lxw_workbook  *workbook  = workbook_new("images.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
worksheet_insert_image(worksheet, CELL("B2"), "logo.png");    
workbook_close(workbook);  `

Height is 103%. Why?

images

0.6 scale -> 63%, and so on. 1.1 -> 116%.
But 0.5 scaling is correct, but 0.5 only

Originally created by @RaFaeL-NN on GitHub (Feb 29, 2020). Original GitHub issue: https://github.com/jmcnamara/libxlsxwriter/issues/274 Originally assigned to: @jmcnamara on GitHub. examples\images.c ` lxw_workbook *workbook = workbook_new("images.xlsx"); lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL); worksheet_insert_image(worksheet, CELL("B2"), "logo.png"); workbook_close(workbook); ` Height is 103%. Why? ![images](https://user-images.githubusercontent.com/34895749/75614974-42d4ae80-5b4f-11ea-988e-92c0c26e8d3e.png) 0.6 scale -> 63%, and so on. 1.1 -> 116%. But 0.5 scaling is correct, but 0.5 only
gitea-mirror 2026-05-05 11:57:26 -06:00
Author
Owner

@jmcnamara commented on GitHub (Feb 29, 2020):

Could you attach the image you are using

<!-- gh-comment-id:592995673 --> @jmcnamara commented on GitHub (Feb 29, 2020): Could you attach the image you are using
Author
Owner

@RaFaeL-NN commented on GitHub (Feb 29, 2020):

images.zip

<!-- gh-comment-id:593000280 --> @RaFaeL-NN commented on GitHub (Feb 29, 2020): [images.zip](https://github.com/jmcnamara/libxlsxwriter/files/4271059/images.zip)
Author
Owner

@jmcnamara commented on GitHub (Feb 29, 2020):

It looks okay in my version of Excel when I run this program (same as your with the filename and image renamed):


#include "xlsxwriter.h"

int main() {
    lxw_workbook  *workbook  = workbook_new("gh274.xlsx");
    lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);

    worksheet_insert_image(worksheet, CELL("B2"), "gh274.png");

    workbook_close(workbook);

    return 0;
}

Output:

Screenshot 2020-02-29 at 23 15 59

What happens when you manually insert that image into Excel? What dimensions do you get?

<!-- gh-comment-id:593004853 --> @jmcnamara commented on GitHub (Feb 29, 2020): It looks okay in my version of Excel when I run this program (same as your with the filename and image renamed): ```C #include "xlsxwriter.h" int main() { lxw_workbook *workbook = workbook_new("gh274.xlsx"); lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL); worksheet_insert_image(worksheet, CELL("B2"), "gh274.png"); workbook_close(workbook); return 0; } ```` Output: ![Screenshot 2020-02-29 at 23 15 59](https://user-images.githubusercontent.com/94267/75616499-bd9acb00-5b49-11ea-98a8-536f681be845.png) What happens when you manually insert that image into Excel? What dimensions do you get?
Author
Owner

@RaFaeL-NN commented on GitHub (Mar 1, 2020):

I found an issue. _worksheet_size_row returns 19 instead of 20. I change


        if (row) {
        if (row->hidden && anchor != LXW_OBJECT_MOVE_AND_SIZE_AFTER)
            pixels = 0;
        else
            pixels = (uint32_t) (4.0 / 3.0 * row->height);
    }
    else {
        pixels = (uint32_t) (4.0 / 3.0 * self->default_row_height);
    }

to


       if (row) {
        if (row->hidden && anchor != LXW_OBJECT_MOVE_AND_SIZE_AFTER)
            pixels = 0;
        else
            pixels = (uint32_t) (row->height * 4.0 / 3.0);
    }
    else {
        pixels = (uint32_t) (self->default_row_height * 4.0 / 3.0);
    }

and it works correct now for me. I don't know a nature of this issue (old compiler, compiling options or smth other) but I hope you'll apply this patch too

<!-- gh-comment-id:593088403 --> @RaFaeL-NN commented on GitHub (Mar 1, 2020): I found an issue. _worksheet_size_row returns 19 instead of 20. I change ```C if (row) { if (row->hidden && anchor != LXW_OBJECT_MOVE_AND_SIZE_AFTER) pixels = 0; else pixels = (uint32_t) (4.0 / 3.0 * row->height); } else { pixels = (uint32_t) (4.0 / 3.0 * self->default_row_height); } ``` to ```C if (row) { if (row->hidden && anchor != LXW_OBJECT_MOVE_AND_SIZE_AFTER) pixels = 0; else pixels = (uint32_t) (row->height * 4.0 / 3.0); } else { pixels = (uint32_t) (self->default_row_height * 4.0 / 3.0); } ``` and it works correct now for me. I don't know a nature of this issue (old compiler, compiling options or smth other) but I hope you'll apply this patch too
Author
Owner

@jmcnamara commented on GitHub (Mar 1, 2020):

I found an issue. _worksheet_size_row returns 19 instead of 20

Good find. Does the following work? It may be more explicit. The 4.0/3.0 actually comes from the ratio of DPIs 96.0/72.0:

        if (row->hidden && anchor != LXW_OBJECT_MOVE_AND_SIZE_AFTER)
            pixels = 0;
        else
            pixels = (uint32_t) (0.75 * row->height);
    }
    else {
        pixels = (uint32_t) (0.75 * self->default_row_height);
    }
<!-- gh-comment-id:593095679 --> @jmcnamara commented on GitHub (Mar 1, 2020): > I found an issue. _worksheet_size_row returns 19 instead of 20 Good find. Does the following work? It may be more explicit. The `4.0/3.0` actually comes from the ratio of DPIs `96.0/72.0`: ```C if (row->hidden && anchor != LXW_OBJECT_MOVE_AND_SIZE_AFTER) pixels = 0; else pixels = (uint32_t) (0.75 * row->height); } else { pixels = (uint32_t) (0.75 * self->default_row_height); } ```
Author
Owner

@RaFaeL-NN commented on GitHub (Mar 1, 2020):

Do you mean (row->height) / 0.75 ? 4/3=1.3(3)

I check, row->height / 0.75 works too

<!-- gh-comment-id:593106966 --> @RaFaeL-NN commented on GitHub (Mar 1, 2020): Do you mean (row->height) / 0.75 ? 4/3=1.3(3) I check, row->height / 0.75 works too
Author
Owner

@jmcnamara commented on GitHub (Mar 1, 2020):

I check, row->height / 0.75 works too

Good, thanks. I’ll probably go with that.

<!-- gh-comment-id:593109768 --> @jmcnamara commented on GitHub (Mar 1, 2020): > I check, row->height / 0.75 works too Good, thanks. I’ll probably go with that.
Author
Owner

@jmcnamara commented on GitHub (Mar 21, 2020):

Ok. Thanks. Fixed on master.

<!-- gh-comment-id:602075332 --> @jmcnamara commented on GitHub (Mar 21, 2020): Ok. Thanks. Fixed on master.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: github-starred/libxlsxwriter#218
No description provided.