using ClosedXML.Excel;
using ClosedXML.Excel.Drawings;
using System.IO;
using System.Reflection;
namespace ClosedXML_Examples
{
public class ImageAnchors : IXLExample
{
public void Create(string filePath)
{
using (var wb = new XLWorkbook())
{
IXLWorksheet ws;
using (Stream fs = Assembly.GetExecutingAssembly().GetManifestResourceStream("ClosedXML_Examples.Resources.ImageHandling.png"))
{
ws = wb.Worksheets.Add("Images1");
#region AbsoluteAnchor
ws.AddPicture(fs, XLPictureFormat.Png, "Image10")
.MoveTo(220, 150);
#endregion AbsoluteAnchor
#region OneCellAnchor
fs.Position = 0;
ws.AddPicture(fs, XLPictureFormat.Png, "Image11")
.MoveTo(ws.Cell(1, 1).Address);
#endregion OneCellAnchor
ws = wb.Worksheets.Add("Images2");
#region TwoCellAnchor
fs.Position = 0;
ws.AddPicture(fs, XLPictureFormat.Png, "Image20")
.MoveTo(ws.Cell(6, 5).Address, ws.Cell(9, 7).Address);
#endregion TwoCellAnchor
}
using (Stream fs = Assembly.GetExecutingAssembly().GetManifestResourceStream("ClosedXML_Examples.Resources.SampleImage.jpg"))
{
// Moving images around and scaling them
ws = wb.Worksheets.Add("Images3");
ws.AddPicture(fs, XLPictureFormat.Jpeg)
.MoveTo(ws.Cell(2, 2).Address, 20, 5, ws.Cell(5, 5).Address, 30, 10)
.MoveTo(ws.Cell(2, 2).Address, ws.Cell(5, 5).Address);
ws.AddPicture(fs, XLPictureFormat.Jpeg)
.MoveTo(ws.Cell(6, 2).Address, 2, 2, ws.Cell(9, 5).Address, 2, 2)
.MoveTo(ws.Cell(6, 2).Address, 20, 5, ws.Cell(9, 5).Address, 30, 10);
ws.AddPicture(fs, XLPictureFormat.Jpeg)
.MoveTo(ws.Cell(10, 2).Address, 20, 5)
.Scale(0.2, true)
.MoveTo(ws.Cell(10, 1).Address);
}
using (Stream fs = Assembly.GetExecutingAssembly().GetManifestResourceStream("ClosedXML_Examples.Resources.SampleImage.jpg"))
{
// Changing of placement
ws = wb.Worksheets.Add("Images4");
ws.AddPicture(fs, XLPictureFormat.Jpeg)
.MoveTo(100, 100)
.WithPlacement(XLPicturePlacement.FreeFloating);
}
wb.SaveAs(filePath);
}
}
}
}