[]
        
(Showing Draft Content)

GrapeCity.Documents.Excel.Drawing.IShapes.AddPicture

AddPicture Method

AddPicture(string, double, double, double, double)

Creates a picture from an existing file and returns the IShape object that represents the new picture.

Use this method to place a picture at a specific position on the current sheet. The left and top values are measured in points relative to the upper-left corner of the document.

Declaration
IShape AddPicture(string filename, double left, double top, double width, double height)
Function AddPicture(filename As String, left As Double, top As Double, width As Double, height As Double) As IShape
Parameters
Type Name Description
string filename

The file from which the object is to be created. null is not supported.

double left

The position, in points, of the upper-left corner of the picture relative to the upper-left corner of the document.

double top

The position, in points, of the upper-left corner of the picture relative to the top of the document.

double width

The width of the picture, in points.

double height

The height of the picture, in points.

Returns
Type Description
IShape

The IShape object that represents the new picture.

Examples
try
{
    string imagePath = System.IO.Path.Combine("Users", "DefaultApps", "Documents", "logo.png");
    IShape picture = worksheet.Shapes.AddPicture(imagePath, 20, 20, 120, 80);
    picture.Name = "Logo";
}
catch (IOException ex)
{
    throw new InvalidOperationException("Unable to add picture from file", ex);
}
Exceptions
Type Condition
IOException

Thrown if the specified file cannot be read.

AddPicture(string, IRange)

Creates a picture from an existing file at the specified range on the current sheet.

The worksheet of targetRange and the worksheet that receives the new shape must be the same.

Declaration
IShape AddPicture(string filename, IRange targetRange)
Function AddPicture(filename As String, targetRange As IRange) As IShape
Parameters
Type Name Description
string filename

The file from which the picture is created.

IRange targetRange

The target range on the current sheet.

Returns
Type Description
IShape

The IShape object that represents the new picture.

Examples
IRange targetRange = worksheet.Range["B2:D8"];
string imagePath = System.IO.Path.Combine("Users", "DefaultApps", "Documents", "logo.png");
IShape picture = worksheet.Shapes.AddPicture(imagePath, targetRange);
picture.AlternativeText = "Product image";
Exceptions
Type Condition
IOException

Thrown if the specified file cannot be read.

AddPicture(string, string, double, double, double, double)

Creates a picture from an existing file and adds it to the worksheet at the specified position and size.

The picture is positioned in points relative to the upper-left corner of the document.

Declaration
IShape AddPicture(string name, string filename, double left, double top, double width, double height)
Function AddPicture(name As String, filename As String, left As Double, top As Double, width As Double, height As Double) As IShape
Parameters
Type Name Description
string name

The picture name.

string filename

The file from which the object is to be created.

double left

The position (in points) of the upper-left corner of the picture relative to the upper-left corner of the document.

double top

The position (in points) of the upper-left corner of the picture relative to the top of the document.

double width

The width of the picture, in points.

double height

The height of the picture, in points.

Returns
Type Description
IShape

The IShape object that represents the new picture.

Examples
try
{
    string imagePath = System.IO.Path.Combine("Users", "DefaultApps", "Documents", "logo.png");
    IShape picture = worksheet.Shapes.AddPicture("CompanyLogo", imagePath, 20, 20, 120, 80);
    picture.Name = "CompanyLogo";
}
catch (IOException ex)
{
    throw new InvalidOperationException("Unable to add picture by name from file", ex);
}
Exceptions
Type Condition
IOException

Thrown if the picture file cannot be read.

AddPicture(string, string, IRange)

Creates a picture from an existing file at the specified range on the current sheet.

The worksheet of targetRange and the worksheet that receives the new shape must be the same.

Declaration
IShape AddPicture(string name, string filename, IRange targetRange)
Function AddPicture(name As String, filename As String, targetRange As IRange) As IShape
Parameters
Type Name Description
string name

The picture name.

string filename

The file from which the picture is created.

IRange targetRange

The target range on the current sheet.

Returns
Type Description
IShape

The IShape object that represents the new picture.

Examples
IRange targetRange = worksheet.Range["B2:D8"];
string imagePath = System.IO.Path.Combine("Users", "DefaultApps", "Documents", "logo.png");
IShape picture = worksheet.Shapes.AddPicture("ProductImage", imagePath, targetRange);
picture.AlternativeText = "Product image";
Exceptions
Type Condition
IOException

Thrown if the picture file cannot be read.

AddPicture(Stream, ImageType, double, double, double, double)

Creates a picture from an existing stream.

Returns the IShape object that represents the new picture.

Declaration
IShape AddPicture(Stream stream, ImageType type, double left, double top, double width, double height)
Function AddPicture(stream As Stream, type As ImageType, left As Double, top As Double, width As Double, height As Double) As IShape
Parameters
Type Name Description
Stream stream

The stream from which the object is to be created.

ImageType type

Specifies the type of picture to create.

double left

The position (in points) of the upper-left corner of the picture relative to the upper-left corner of the document.

double top

The position (in points) of the upper-left corner of the picture relative to the top of the document.

double width

The width of the picture, in points.

double height

The height of the picture, in points.

Returns
Type Description
IShape

The IShape object that represents the new picture.

Examples
using (MemoryStream stream = CreateSampleImageStream())
{
    IShape picture = worksheet.Shapes.AddPicture(stream, ImageType.PNG, 20, 20, 80, 60);
    picture.Name = "Logo";
}
Exceptions
Type Condition
IOException

Thrown if an I/O error occurs while reading the stream.

AddPicture(Stream, ImageType, IRange)

Creates a picture from an existing stream at the specified range on the current sheet.

The sheet of targetRange and the sheet of the shape being added must be the same sheet. Returns the IShape object that represents the new picture.

Declaration
IShape AddPicture(Stream stream, ImageType type, IRange targetRange)
Function AddPicture(stream As Stream, type As ImageType, targetRange As IRange) As IShape
Parameters
Type Name Description
Stream stream

The stream from which the object is to be created.

ImageType type

Specifies the type of picture to create.

IRange targetRange

The target range on the current sheet.

Returns
Type Description
IShape

The IShape object that represents the new picture.

Examples
IRange targetRange = worksheet.Range["B2:D6"];
using (MemoryStream stream = CreateSampleImageStream())
{
    IShape picture = worksheet.Shapes.AddPicture(stream, ImageType.PNG, targetRange);
    picture.AlternativeText = "Company logo";
}
Exceptions
Type Condition
IOException

Thrown if an I/O error occurs while reading the stream.

AddPicture(string, Stream, ImageType, double, double, double, double)

Creates a picture from an existing stream.

Returns the IShape object that represents the new picture.

Declaration
IShape AddPicture(string name, Stream stream, ImageType type, double left, double top, double width, double height)
Function AddPicture(name As String, stream As Stream, type As ImageType, left As Double, top As Double, width As Double, height As Double) As IShape
Parameters
Type Name Description
string name

The picture name.

Stream stream

The stream from which the object is to be created.

ImageType type

Specifies the type of picture to create.

double left

The position (in points) of the upper-left corner of the picture relative to the upper-left corner of the document.

double top

The position (in points) of the upper-left corner of the picture relative to the top of the document.

double width

The width of the picture, in points.

double height

The height of the picture, in points.

Returns
Type Description
IShape

The IShape object that represents the new picture.

Examples
using (MemoryStream stream = CreateSampleImageStream())
{
    IShape picture = worksheet.Shapes.AddPicture("CompanyLogo", stream, ImageType.PNG, 20, 20, 120, 80);
    picture.AlternativeText = "Company logo";
}
Exceptions
Type Condition
IOException

Thrown if an I/O error occurs while reading the stream.

AddPicture(string, Stream, ImageType, IRange)

Creates a picture from an existing stream at the specified range on the current sheet.

The worksheet of targetRange and the worksheet to which the shape is added must be the same. Returns the IShape object that represents the new picture.

Declaration
IShape AddPicture(string name, Stream stream, ImageType type, IRange targetRange)
Function AddPicture(name As String, stream As Stream, type As ImageType, targetRange As IRange) As IShape
Parameters
Type Name Description
string name

The picture name.

Stream stream

The stream from which the picture is created.

ImageType type

Specifies the type of picture to create.

IRange targetRange

The target range on the current sheet.

Returns
Type Description
IShape

The IShape object that represents the new picture.

Examples
IRange targetRange = worksheet.Range["B2:D6"];
using (MemoryStream stream = CreateSampleImageStream())
{
    IShape picture = worksheet.Shapes.AddPicture("CompanyLogo", stream, ImageType.PNG, targetRange);
    picture.AlternativeText = "Company logo";
}
Exceptions
Type Condition
IOException

Thrown if an I/O error occurs while reading the stream.