Managing Pictures in a Worksheet

Adding Pictures

To add a hyperlink to a cell using Aspose.Cells.GridDesktop, please follow the steps below:

  • Add Aspose.Cells.GridDesktop control to your Form
  • Access any desired Worksheet
  • Add Picture to the worksheet by specifying the file path of picture and cell name where the picture will be inserted

Pictures collection in the Worksheet object provides an overloaded Add method. Developers can use any overloaded version of Add method according to their specific needs. Using these overloaded versions of Add method, it is possible to add a picture from file, stream or Image object.

Below is the sample code for adding pictures into worksheets.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = Utils.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Accessing first worksheet of the Grid
Worksheet sheet = gridDesktop1.GetActiveWorksheet();
// Adding picture to "b2" cell from file
sheet.Pictures.Add("b2", dataDir + "AsposeGrid.jpg");
// Creating a stream contain picture
FileStream fs = new FileStream(dataDir + "AsposeLogo.jpg", FileMode.Open);
try
{
// Adding picture to "b3" cell from stream
sheet.Pictures.Add(2, 1, fs);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
// Closing stream
fs.Close();
}

Accessing Pictures

To access and modify an existing picture in the worksheet, developers can access the picture from the Pictures collection of the Worksheet by specifying the cell (using cell name or its location in terms of row and column number) in which the picture is inserted. Once the picture is accessed, developers can modify its Image at runtime.

Below is the sample code to access and modify the pictures in a worksheet.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = Utils.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Accessing first worksheet of the Grid
Worksheet sheet = gridDesktop1.Worksheets[0];
// Accessing a picture added to "c3" cell (specified using its row & column number)
Aspose.Cells.GridDesktop.Data.GridPicture picture1 = sheet.Pictures[1];
// Modifying the image
picture1.Image = Image.FromFile(dataDir + "Aspose.Grid.jpg");

Removing Pictures

To remove an existing picture, developers can simply access a desired worksheet and then Remove picture from the Pictures collection of the Worksheet by specifying the cell (using its name or row & column number) that contains the picture.

In code below it is shown how to remove pictures from worksheet.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Accessing first worksheet of the Grid
Worksheet sheet = gridDesktop1.Worksheets[0];
// Removing picture from "c3" cell
sheet.Pictures.Remove(2, 2);