Skip to content

Possible bugs in AddPictureFromBytes method. #2001

Closed
@Sirius2020

Description

@Sirius2020

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions