Insert image into cell

Hi,


how can I insert an image into a selected cell?


regards,

Mark


9 Replies 1 reply marked as answer

AR Arulpriya Ramalingam Syncfusion Team December 13, 2021 12:38 PM UTC

Hi Mark, 
 
To load image in the selected cell range, the AddImage method can be used. We already provided the details in our user guide to load images in spreadsheet with specific cell range. Please make use of the below code and UG for further reference. 
 
Example code 
 
var worksheet = spreadsheet.ActiveSheet; 
Image img = Image.FromFile(@"..\..\Icon\syncfusion.png"); 
Stream stream = new MemoryStream(); 
img.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);  
var shape = spreadsheet.AddImage(worksheet, spreadsheet.ActiveGrid.CurrentCell.CellRowColumnIndex, stream); 
 
 
 
Please get back to us if you have any other queries. 
 
Regards, 
Arulpriya Ramalingam 



MJ Mark Jayvee December 14, 2021 08:19 AM UTC

Thank you for the response.


It works importing the image to the spreadsheet but the image size is with its original scale.

How can I import the image that autofit to the current cell?



AR Arulpriya Ramalingam Syncfusion Team December 15, 2021 02:49 PM UTC

Hi Mark, 
 
The Spreadsheet does not have built-in support to auto fit the image into cell. However, the Height and Width properties of Shape can be used to update the size manually for provided image based on our requirement. Please make use of below code and modified sample. 
 
Example code 
 
var worksheet = spreadsheet.ActiveSheet; 
Image img = Image.FromFile(@"..\..\Icon\syncfusion.png"); 
Stream stream = new MemoryStream(); 
img.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);  
var shape = spreadsheet.AddImage(worksheet, spreadsheet.ActiveGrid.CurrentCell.CellRowColumnIndex, stream); 
 
//Re-sizing a Picture 
shape.Height = (int)spreadsheet.ActiveGrid.RowHeights[spreadsheet.ActiveGrid.CurrentCell.RowIndex]; 
shape.Width = (int)spreadsheet.ActiveGrid.ColumnWidths[spreadsheet.ActiveGrid.CurrentCell.ColumnIndex]; 
//To invalidate the image cell. 
spreadsheet.ActiveGrid.GraphicModel.InvalidateGraphicVisual(true); 
spreadsheet.ActiveGrid.GraphicModel.InvalidateGraphicObjects(); 
 
 
Regards, 
Arulpriya Ramalingam 



MJ Mark Jayvee replied to Arulpriya Ramalingam December 17, 2021 11:56 PM UTC

Thank you for the response.


I tried the code and import successfully. Only issue is that it import to a random cell.

What I want to do is to import this image to the current selected cell.


Regards,



SS Sampathnarayanan Sankaralingam Syncfusion Team December 20, 2021 11:00 AM UTC

Hi Mark,
Please find the below code snippet to load image in the current selected cell.


var worksheet = spreadsheet.ActiveSheet;
Image img = Image.FromFile(@"..\..\Icon\syncfusion.png");
Stream stream = new MemoryStream();
img.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
BitmapShapeImpl shape = spreadsheet.AddImage(worksheet, spreadsheet.ActiveGrid.CurrentCell.CellRowColumnIndex, stream) as BitmapShapeImpl;
if (shape != null)
{
shape.LeftColumn = spreadsheet.ActiveGrid.CurrentCell.CellRowColumnIndex.ColumnIndex - 1;
shape.TopRow = spreadsheet.ActiveGrid.CurrentCell.CellRowColumnIndex.RowIndex;
shape.EvaluateTopLeftPosition();
}
//Re-sizing a Picture
shape.Height = (int)spreadsheet.ActiveGrid.RowHeights[spreadsheet.ActiveGrid.CurrentCell.RowIndex];
shape.Width = (int)spreadsheet.ActiveGrid.ColumnWidths[spreadsheet.ActiveGrid.CurrentCell.ColumnIndex];
spreadsheet.ActiveGrid.GraphicModel.InvalidateGraphicVisual(true);
spreadsheet.ActiveGrid.GraphicModel.InvalidateGraphicObjects();


Regards,
Sampath Narayanan.S




MJ Mark Jayvee December 20, 2021 07:28 PM UTC

Hi  Sampath Narayanan.S,


Thank you for the response. It works as tested.

One last question, How can I consider if the image is imported in a Merge Cell?



SS Sampathnarayanan Sankaralingam Syncfusion Team December 21, 2021 01:07 PM UTC

Hi Mark, 
 
We are unable to understand the exact requirement with merged cells. We are able to inset image to merged cells. Kindly revert to us with more details about your requirement with image illustration. So that we can understand the exact scenario and provide a prompt solution. 
 
Regards, 
Sampath Narayanan.S 



MJ Mark Jayvee replied to Sampathnarayanan Sankaralingam December 21, 2021 06:05 PM UTC

Hi Sampath Narayanan.S,


Thank you for your email.


Insert image into 1 selected cell is working well with size is alsi correct.


Could you please merge like 4 cells and try to import the image? It can import but the size is not matching?



SS Sampathnarayanan Sankaralingam Syncfusion Team December 22, 2021 01:26 PM UTC

Hi Mark, 
 
Please find the below code snippet to autofit image into the merge cell. 
 
Code Snippet 

var worksheet = spreadsheet.ActiveSheet; 
 
 
Image img = Image.FromFile(@"..\..\Icon\syncfusion.png"); 
Stream stream = new MemoryStream(); 
 
img.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg); 
BitmapShapeImpl shape = spreadsheet.AddImage(worksheet, spreadsheet.ActiveGrid.CurrentCell.CellRowColumnIndex, stream) as BitmapShapeImpl; 
 
if (shape != null) 
{ 
    shape.LeftColumn = spreadsheet.ActiveGrid.CurrentCell.CellRowColumnIndex.ColumnIndex - 1; 
    shape.TopRow = spreadsheet.ActiveGrid.CurrentCell.CellRowColumnIndex.RowIndex; 
    shape.EvaluateTopLeftPosition(); 
} 
 
if (spreadsheet.ActiveSheet.Range[spreadsheet.ActiveGrid.CurrentCell.RowIndex, spreadsheet.ActiveGrid.CurrentCell.ColumnIndex].IsMerged) 
{ 
    if (spreadsheet.ActiveSheet.MergedCells != null) 
    { 
        double height = 0.0; 
        double width = 0.0; 
        for (int i = 0; i < spreadsheet.ActiveSheet.MergedCells.Length; i++) 
        { 
            IRange curRange = spreadsheet.ActiveSheet.MergedCells[i]; 
            GridRangeInfo mergdRange = GridRangeInfo.Cells(curRange.Row, curRange.Column, curRange.LastRow, curRange.LastColumn); 
            if (mergdRange.IntersectsWith(GridRangeInfo.Cell(spreadsheet.ActiveGrid.CurrentCell.RowIndex, spreadsheet.ActiveGrid.CurrentCell.ColumnIndex))) 
            { 
                for (int j = mergdRange.Left; j <= mergdRange.Right; j++) 
                { 
                    width += (int)spreadsheet.ActiveGrid.ColumnWidths[j]; 
                } 
 
                for (int k = mergdRange.Top; k <= mergdRange.Bottom; k++) 
                { 
                    height += (int)spreadsheet.ActiveGrid.RowHeights[k]; 
                } 
            } 
        } 
 
        shape.Height = (int)height; 
        shape.Width = (int)width; 
    } 
} 
else 
{ 
    //Re-sizing a Picture 
    shape.Height = (int)spreadsheet.ActiveGrid.RowHeights[spreadsheet.ActiveGrid.CurrentCell.RowIndex]; 
    shape.Width = (int)spreadsheet.ActiveGrid.ColumnWidths[spreadsheet.ActiveGrid.CurrentCell.ColumnIndex]; 
} 
 
spreadsheet.ActiveGrid.GraphicModel.InvalidateGraphicVisual(true); 
spreadsheet.ActiveGrid.GraphicModel.InvalidateGraphicObjects(); 

 
Regards, 
Sampath Narayanan.S 


Marked as answer
Loader.
Up arrow icon