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); |
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?
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(); |
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,
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(); |
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?
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?
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(); |