This project is read-only.

Images created in foreach-loop not resizing correctly

Feb 3, 2015 at 5:23 PM
Edited Feb 3, 2015 at 5:24 PM
Hi there,

so I am not sure if this is an issue or just my stupidity, but I don't get the Images I create inside a loop to resize correctly. I asked the question at StackOverflow over here, but noone answered yet so I hope I'll find help here, at the source :)

The following is a c&p from my stackoverflow-question:

I'm using NPOI 2.1.3 to create an excelsheet for a dynamic menuplan.

I have this code so far, which creates me pictures based on a list of imagepath-strings I get before:
var priceRow = sheet.GetRow(priceRowNum);
var priceCell = priceRow.CreateCell(i + 1);
...adding other contents...
List<string> imgList = createImagesList(codes);

foreach (string image in imgList)
    MemoryStream stream = new MemoryStream();
    Image.FromFile(image).Save(stream, ImageFormat.Png);
    var anc = new HSSFClientAnchor(0, 0, 20, 20, 0, 0, 0, 0)
        Col1 = priceCell.ColumnIndex
    anc.Row1 = priceRow.RowNum;
    anc.AnchorType = 0;
    int pictureIndex = excel.AddPicture(stream.ToArray(), PictureType.PNG);
    drawing.CreatePicture(anc, pictureIndex).Resize();
My Problem might be best described by the following picture:


so it seems that only the pictures in the last foreach-iteration are resized correctly to the containing cell and I'm out of ideas why.

I searched a bit and found some other problems, but nothing seems to fit. Here are my insights so far: I didn't change the font, cellstyle etc. for my priceRow/priceCell, for I am aware that resize is just working correctly when the default size is used. Although, I used some style/fontchanges for other cells, but I always created a new font/cellstyle, so I never touched the "default" workbook-style.

Also, my patriarch drawing is created once per sheet (found this in another answer for a resizeproblem).

I also tried switching the anchortype from zero to two and back, and to manually add a height to my priceRow, but nothing of that took effect, so I hope someone could help me out here for I am really absolutely out of Ideas.

I don't think this is a "nested for/foreach-logic"-error, because with other plans there's sometimes only the first iteration not resizing correctly,... - it just seems to be very weird.

Feel free to ask for more code/pictures!

Best regards,
Feb 4, 2015 at 9:50 AM
I just messed up my anchor-constructor. got it working now.