Document Solutions for Excel, .NET Edition | Document Solutions
Features / Shapes and Pictures / Size and Position of Image
In This Topic
    Size and Position of Image
    In This Topic

    As a common use case scenario, you may want to render image at a particular position.

    DsExcel allows you to fetch the exact position and size of the specified cell range in pixels by using CellInfo.GetAccurateRangeBoundary method that returns RectangleF object of type double. You can then use the AddPictureinPixel method to add an image at the fetched location. For more information about adding pictures, see Shapes and Pictures topic.

    Following example code shows how to get the absolute location and size of the range and insert an image to that location.

    C#
    Copy Code
    // Get the absolute location and size of the Range["F1:G1"] in the worksheet.
    IRange range = worksheet.Range["F1:G1"];
    // Get the RectangleF object of GetAccurateRangeBoundary method
    RectangleF rect = GrapeCity.Documents.Excel.CellInfo.GetAccurateRangeBoundary(range);
    // Add the image to the Range["F1:G1"]
    worksheet.Shapes.AddPictureInPixel("logo.png", rect.X, rect.Y, rect.Width, rect.Height);
    Note: Version v5.2 onwards, we obsoleted CellInfo.GetRangeBoundary method and replaced it with the GetAccurateRangeBoundary method which gives a more accurate position and size of the specified range.