SpreadsheetGear 2017
AddPicture(String,Double,Double,Double,Double) Method
Example 






SpreadsheetGear.Shapes Namespace > IShapes Interface > AddPicture Method : AddPicture(String,Double,Double,Double,Double) Method
The filename of the picture to add.
Left edge of the new picture in points.
Top edge of the new picture in points.
Width of the new picture in points.
Height of the new picture in points.
Adds the specified picture to the shapes collection at the specified position and with the specified size.
Syntax
'Declaration
 
Overloads Function AddPicture( _
   ByVal filename As System.String, _
   ByVal left As System.Double, _
   ByVal top As System.Double, _
   ByVal width As System.Double, _
   ByVal height As System.Double _
) As IShape
'Usage
 
Dim instance As IShapes
Dim filename As System.String
Dim left As System.Double
Dim top As System.Double
Dim width As System.Double
Dim height As System.Double
Dim value As IShape
 
value = instance.AddPicture(filename, left, top, width, height)
IShape AddPicture( 
   System.string filename,
   System.double left,
   System.double top,
   System.double width,
   System.double height
)
function AddPicture( 
    filename: System.String;
    left: System.Double;
    top: System.Double;
    width: System.Double;
    height: System.Double
): IShape; 
function AddPicture( 
   filename : System.String,
   left : System.double,
   top : System.double,
   width : System.double,
   height : System.double
) : IShape;
IShape* AddPicture( 
   System.string* filename,
   System.double left,
   System.double top,
   System.double width,
   System.double height
) 
IShape^ AddPicture( 
   System.String^ filename,
   System.double left,
   System.double top,
   System.double width,
   System.double height
) 

Parameters

filename
The filename of the picture to add.
left
Left edge of the new picture in points.
top
Top edge of the new picture in points.
width
Width of the new picture in points.
height
Height of the new picture in points.

Return Value

A shape object representing the newly created picture.
Remarks

Use SpreadsheetGear.IWorksheetWindowInfo.RowToPoints and SpreadsheetGear.IWorksheetWindowInfo.ColumnToPoints to convert row and column positions to points.

Pictures with PNG, JPEG and Enhanced Windows Metafiles magic signatures are stored in the workbook as is. All other images are converted to PNG before being stored in the workbook.

Example
Demonstrate adding a picture to a new workbook and returning the workbook from an ASP.NET page.
void Page_Load(Object sender, EventArgs e)
    {
        // Create a new empty workbook and get the first sheet. 
        SpreadsheetGear.IWorkbook workbook = SpreadsheetGear.Factory.GetWorkbook();
        SpreadsheetGear.IWorksheet worksheet = workbook.Worksheets["Sheet1"];
        
        // Get the image file name.
        String imageFile = Server.MapPath("files/ssgear.gif");
        
        // Get the width and height of the picture in pixels and convert to
        // points for use in the call to AddPicture.  This step is only
        // necessary if the actual picture size is to be used and that size
        // is unknown.  Another option is to calculate the width and height 
        // in row and column coordinates in the same manner as left and top below.
        double width;
        double height;
        System.Drawing.Image image = System.Drawing.Image.FromFile(imageFile);
        using (image)
        {
            width = image.Width * 72.0 / image.HorizontalResolution;
            height = image.Height * 72.0 / image.VerticalResolution;
        }
        
        // Calculate the left and top placement of the picture by converting 
        // row and column coordinates to points.  Use fractional values to 
        // get coordinates anywhere in between row and column boundaries.
        SpreadsheetGear.IWorksheetWindowInfo windowInfo = worksheet.WindowInfo;
        // Convert from the center of column B to points.
        double left = windowInfo.ColumnToPoints(1.5);
        // Convert from the center of row 1 to points.
        double top = windowInfo.RowToPoints(1.5);
        
        // Add the picture from file.
        worksheet.Shapes.AddPicture(imageFile, left, top, width, height);
 
        // Stream the Excel workbook to the client.
        Response.Clear();
        Response.ContentType = "application/vnd.ms-excel";
        workbook.SaveToStream(Response.OutputStream, SpreadsheetGear.FileFormat.Excel8);
        Response.End();
    }
Sub Page_Load(sender As Object, e As EventArgs)
        ' Create a new empty workbook and get the first sheet. 
        Dim workbook As SpreadsheetGear.IWorkbook = SpreadsheetGear.Factory.GetWorkbook()
        Dim worksheet As SpreadsheetGear.IWorksheet = workbook.Worksheets("Sheet1")
    
        ' Get the image file name.
        Dim imageFile As String = Server.MapPath("files/ssgear.gif")
   
        ' Get the width and height of the picture in pixels and convert to
        ' points for use in the call to AddPicture.  This step is only
        ' necessary if the actual picture size is to be used and that size
        ' is unknown.  Another option is to calculate the width and height 
        ' in row and column coordinates in the same manner as left and top below.
        Dim width As Double
        Dim height As Double
        Dim image As System.Drawing.Image = System.Drawing.Image.FromFile(imageFile)
        Try
            width = image.Width * 72.0 / image.HorizontalResolution
            height = image.Height * 72.0 / image.VerticalResolution
        Finally
            image.Dispose()
        End Try
            
        ' Calculate the left and top placement of the picture by converting 
        ' row and column coordinates to points.  Use fractional values to 
        ' get coordinates anywhere in between row and column boundaries.
        Dim windowInfo As SpreadsheetGear.IWorksheetWindowInfo = worksheet.WindowInfo
        ' Convert from the center of column B to points.
        Dim left As Double = windowInfo.ColumnToPoints(1.5)
        ' Convert from the center of row 2 to points.
        Dim top As Double = windowInfo.RowToPoints(1.5)
       
        ' Add the picture from file.
        worksheet.Shapes.AddPicture(imageFile, left, top, width, height)
   
        ' Stream the Excel workbook to the client.
        Response.Clear()
        Response.ContentType = "application/vnd.ms-excel"
        workbook.SaveToStream(Response.OutputStream, SpreadsheetGear.FileFormat.Excel8)
        Response.End()
    End Sub 'Page_Load
Requirements

Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

See Also

Reference

IShapes Interface
IShapes Members
Overload List
RowToPoints Method
ColumnToPoints Method