Smrf.NodeXL.ExcelTemplate.TableImagePopulator.PopulateAreaWithImages C# (CSharp) Method

PopulateAreaWithImages() private static method

private static PopulateAreaWithImages ( Range oKeyColumnArea, Range oImageColumnArea, String sImageColumnName, SizeF oImageSizePt, Microsoft.Office.Interop.Excel.Shape>.Dictionary oOldImagesInColumn, TemporaryImages oTemporaryImages ) : void
oKeyColumnArea Range
oImageColumnArea Range
sImageColumnName String
oImageSizePt System.Drawing.SizeF
oOldImagesInColumn Microsoft.Office.Interop.Excel.Shape>.Dictionary
oTemporaryImages TemporaryImages
return void
    PopulateAreaWithImages
    (
        Range oKeyColumnArea,
        Range oImageColumnArea,
        String sImageColumnName,
        SizeF oImageSizePt,
        Dictionary<String, Microsoft.Office.Interop.Excel.Shape>
            oOldImagesInColumn,
        TemporaryImages oTemporaryImages
    )
    {
        Debug.Assert(oKeyColumnArea != null);
        Debug.Assert(oImageColumnArea != null);
        Debug.Assert( !String.IsNullOrEmpty(sImageColumnName) );
        Debug.Assert(oOldImagesInColumn != null);
        Debug.Assert(oTemporaryImages != null);

        // Gather some required information.

        Int32 iRows = oKeyColumnArea.Rows.Count;

        Debug.Assert(iRows == oImageColumnArea.Rows.Count);

        Debug.Assert(oKeyColumnArea.Parent is Worksheet);

        Worksheet oWorksheet = (Worksheet)oKeyColumnArea.Parent;

        Microsoft.Office.Interop.Excel.Shapes oShapes = oWorksheet.Shapes;

        Object [,] aoKeyValues = ExcelUtil.GetRangeValues(oKeyColumnArea);

        Dictionary<String, String> oFileNames = oTemporaryImages.FileNames;

        // Set the row heights to fit the images.

        oKeyColumnArea.RowHeight = oImageSizePt.Height + 2 * ImageMarginPt;

        // Get the first cell in the image column.

        Range oImageCell = (Range)oImageColumnArea.Cells[1, 1];

        // Loop through the area's rows.

        for (Int32 iRow = 1; iRow <= iRows; iRow++)
        {
            String sKey, sFileName;

            // Check whether the row's key cell has a corresponding file name
            // in the dictionary.

            if (
                ExcelUtil.TryGetNonEmptyStringFromCell(aoKeyValues, iRow, 1,
                    out sKey)
                &&
                oFileNames.TryGetValue(sKey, out sFileName)
                )
            {
                // Give the picture a name that can be recognized by
                // GetImagesInColumn().

                String sPictureName = sImageColumnName + "-" + sKey;

                Microsoft.Office.Interop.Excel.Shape oPicture;

                // If an old version of the picture remains from a previous
                // call to this method, delete it.

                if ( oOldImagesInColumn.TryGetValue(sPictureName,
                    out oPicture) )
                {
                    oPicture.Delete();
                }

                String sFileNameWithPath = Path.Combine(
                    oTemporaryImages.Folder, sFileName);

                oPicture = oShapes.AddPicture(sFileNameWithPath,
                    MsoTriState.msoFalse, MsoTriState.msoCTrue,
                    (Single)(Double)oImageCell.Left + ImageMarginPt,
                    (Single)(Double)oImageCell.Top + ImageMarginPt,
                    oImageSizePt.Width,
                    oImageSizePt.Height
                    );

                oPicture.Name = sPictureName;
            }

            // Move down one cell in the image column.

            oImageCell = oImageCell.get_Offset(1, 0);
        }
    }