Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible bugs in AddPictureFromBytes method. #2001

Open
Sirius2020 opened this issue Sep 26, 2024 · 1 comment
Open

Possible bugs in AddPictureFromBytes method. #2001

Sirius2020 opened this issue Sep 26, 2024 · 1 comment

Comments

@Sirius2020
Copy link

Sirius2020 commented Sep 26, 2024

Code to reproduce the issue

package main

import (
	_ "image/jpeg"
	"os"
	"path/filepath"

	"github.com/xuri/excelize/v2"
)

func addPictureToA1WithOffset(f *excelize.File, data []byte, ext string, offset int) error {
	enable := true
	disable := false
	if err := f.AddPictureFromBytes("Sheet1", "A1", &excelize.Picture{Extension: ext, File: data, Format: &excelize.GraphicOptions{
		PrintObject: &enable,
		Locked:      &disable,
		ScaleX:      1,
		ScaleY:      1,
		OffsetX:     offset,
		Positioning: "oneCell",
	}}); err != nil {
		panic(err)
	}
	return nil
}

func main() {
	f := excelize.NewFile()
	defer func() {
		if err := f.Close(); err != nil {
			panic(err)
		}
	}()
	f.SetColWidth("Sheet1", "A", "H", 20)
	imagePath := "./pic.jpg" // pixel 60*60
	ext := ".jpg"
	data, _ := os.ReadFile(filepath.Clean(imagePath))

	addPictureToA1WithOffset(f, data, ext, 0)
	addPictureToA1WithOffset(f, data, ext, 60)
	addPictureToA1WithOffset(f, data, ext, 120)
	addPictureToA1WithOffset(f, data, ext, 180)

	if err := f.SaveAs("result.xlsx"); err != nil {
		panic(err)
	}
}

Description
In the code example below, I try to add 4 60x60 sized pictures into cell A1, I'm expecting the pictures to maintain the size of 60x60 and be arranged horizontally end to end.
But there are two problems occur.

  1. The size of the pictures is incorrect, the first and second picture size is 90x100, while the third picture size is 112x100.
  2. The fourth picture is invisible.
image

The fourth picture is misplaced and I believe it's a bug. As the debug screenshot show below, in method addDrawingPicture, from.Col is equal to to.Col while from.ColOff(1714500) is larger than to.ColOff(895350).

image2

Output of go version:

go version go1.20.6 linux/amd64

Excelize version or commit ID:

commit ID: 41c7dd30ce7e
go.sum: github.com/xuri/excelize/v2 v2.8.2-0.20240921073936-41c7dd30ce7e h1:9UoqZmYeb3ZN7rj3PEdm1rfP9dSPMwGfbaFyt+idd5E=

Environment details (OS, Microsoft Excel™ version, physical, etc.):
Linux

@carcamano
Copy link

Facing same situation here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants