Files
ClosedXML/ClosedXML_Examples/ImageHandling/ImageAnchors.cs
T
Thom Lamb 2cd6df6481 Initial commit of the ClosedXML library
Establishes the foundational structure, core Excel functionality, and tooling.

- Implements base components for cells, ranges, columns, and rows.
- Integrates a custom calculation engine with a wide range of Excel functions.
- Adds comprehensive support for styling, conditional formatting, data validation, comments, pictures, and charts.
- Configures build setup, project metadata (NuGet), and developer guidelines.
- Includes editor and Git attributes for consistent code style and line endings.
2026-06-23 11:02:53 -05:00

85 lines
2.9 KiB
C#

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));
#endregion OneCellAnchor
ws = wb.Worksheets.Add("Images2");
#region TwoCellAnchor
fs.Position = 0;
ws.AddPicture(fs, XLPictureFormat.Png, "Image20")
.MoveTo(ws.Cell(6, 5), ws.Cell(9, 7));
#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), 20, 5, ws.Cell(5, 5), 30, 10)
.MoveTo(ws.Cell(2, 2), ws.Cell(5, 5));
ws.AddPicture(fs, XLPictureFormat.Jpeg)
.MoveTo(ws.Cell(6, 2), 2, 2, ws.Cell(9, 5), 2, 2)
.MoveTo(ws.Cell(6, 2), 20, 5, ws.Cell(9, 5), 30, 10);
ws.AddPicture(fs, XLPictureFormat.Jpeg)
.MoveTo(ws.Cell(10, 2), 20, 5)
.Scale(0.2, true)
.MoveTo(ws.Cell(10, 1));
}
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);
// Add and delete picture immediately
ws.AddPicture(fs, XLPictureFormat.Jpeg)
.MoveTo(100, 600)
.Delete();
}
wb.SaveAs(filePath);
}
}
}
}