When I insert secord picture in Xlsx file, it always replace first one.
Any help?
IWorkbook workbook = new XSSFWorkbook();
ISheet sheet1 = workbook.CreateSheet("PictureSheet");
IDrawing patriarch = sheet1.CreateDrawingPatriarch();
//create the anchor
XSSFClientAnchor anchor = new XSSFClientAnchor(500, 200, 0, 0, 2, 2, 4, 7);
anchor.AnchorType = 2;
//load the picture and get the picture index in the workbook
XSSFPicture picture = (XSSFPicture)patriarch.CreatePicture(anchor, LoadImage("../../image/HumpbackWhale.jpg", workbook));
picture.Resize(); //Note: Resize will reset client anchor you set.
XSSFClientAnchor anchor2 = new XSSFClientAnchor(500, 200, 0, 0, 12, 2, 14, 7);
anchor2.AnchorType = 2;
XSSFPicture picture2 = (XSSFPicture)patriarch.CreatePicture(anchor2, LoadImage("../../image/pic2.jpg", workbook));
picture2.Resize();
FileStream sw = File.Create("test.xlsx");
workbook.Write(sw);
sw.Close();
Comments: ** Comment from web user: BruceLiu911 **
That is a Static method case this problem.
Method code[..\npoi\ooxml\XSSF\UserModel\XSSPicture.cs]
```
internal static CT_Picture Prototype()
{
if (prototype == null)
{
CT_Picture pic = new CT_Picture();
CT_PictureNonVisual nvpr = pic.AddNewNvPicPr();
CT_NonVisualDrawingProps nvProps = nvpr.AddNewCNvPr();
nvProps.id = (1);
nvProps.name = ("Picture 1");
nvProps.descr = ("Picture");
CT_NonVisualPictureProperties nvPicProps = nvpr.AddNewCNvPicPr();
nvPicProps.AddNewPicLocks().noChangeAspect = true;
CT_BlipFillProperties blip = pic.AddNewBlipFill();
blip.AddNewBlip().embed = "";
blip.AddNewStretch().AddNewFillRect();
CT_ShapeProperties sppr = pic.AddNewSpPr();
CT_Transform2D t2d = sppr.AddNewXfrm();
CT_PositiveSize2D ext = t2d.AddNewExt();
//should be original picture width and height expressed in EMUs
ext.cx = (0);
ext.cy = (0);
CT_Point2D off = t2d.AddNewOff();
off.x = (0);
off.y = (0);
CT_PresetGeometry2D prstGeom = sppr.AddNewPrstGeom();
prstGeom.prst = (ST_ShapeType.rect);
prstGeom.AddNewAvLst();
prototype = pic;
}
return prototype;
}
```
Call method code[..\npoi\ooxml\XSSF\UserModel\XSSFDrawing.cs]
```
public IPicture CreatePicture(XSSFClientAnchor anchor, int pictureIndex)
{
PackageRelationship rel = AddPictureReference(pictureIndex);
long shapeId = newShapeId();
CT_TwoCellAnchor ctAnchor = CreateTwoCellAnchor(anchor);
NPOI.OpenXmlFormats.Dml.Spreadsheet.CT_Picture ctShape = ctAnchor.AddNewPic();
___ctShape.Set(XSSFPicture.Prototype());___
ctShape.nvPicPr.cNvPr.id = (uint)shapeId;
XSSFPicture shape = new XSSFPicture(this, ctShape);
shape.anchor = anchor;
shape.SetPictureReference(rel);
return shape;
}
public void Set(CT_Picture pict)
{
this.nvPicPr = pict.nvPicPr;
this.spPr = pict.spPr;
this.macro = pict.macro;
this.macroSpecified = this.macroSpecified;
this.style = pict.style;
this.styleSpecified = pict.styleSpecified;
this.fPublished = pict.fPublished;
this.fPublishedSpecified = pict.fPublishedSpecified;
___this.blipFill = pict.blipFill;___
}
```
This code every time blipFill is same reference.Then you insert second picture, the first picture will be replace