ExcelFile.GetImage Method
Overloads
- ExcelFile.GetImage(Int32, TXlsImgType)
- ExcelFile.GetImage(Int32, TXlsImgType, Stream)
- ExcelFile.GetImage(Int32, String, TXlsImgType, Stream, Boolean)
ExcelFile.GetImage(Int32, TXlsImgType)
Returns an image bytes and type. Note that for SVG images, xlsx files store both a PNG and SVG image. In those cases, for backward compatibility reasons, this method will return the PNG image. To get the SVG, call GetImageAlternate
Syntax
Namespace: FlexCel.Core
public Byte[] GetImage(Int32 imageIndex, ref TXlsImgType imageType)
Parameters
<-> | Parameter | Type | Description |
---|---|---|---|
imageIndex | Int32 | Index of the image. (1 based) | |
ref | imageType | TXlsImgType | Returns the image type for the data returned. (If it is a bmp, jpg or other) |
Returns
Image data. Use the returned imageType to find out the format of the stored image.
See also
ExcelFile.GetImage(Int32, TXlsImgType, Stream)
Returns an image and its type. Note that for SVG images, xlsx files store both a PNG and SVG image. In those cases, for backward compatibility reasons, this method will return the PNG image. To get the SVG, call GetImageAlternate
Syntax
Namespace: FlexCel.Core
public void GetImage(Int32 imageIndex, ref TXlsImgType imageType, Stream outStream)
Parameters
<-> | Parameter | Type | Description |
---|---|---|---|
imageIndex | Int32 | Index of the image. (1 based) | |
ref | imageType | TXlsImgType | Returns the image type for the data returned. (If it is a bmp, jpg or other) |
outStream | Stream | Stream where the image data will be copied. |
Examples
The method "GetImageFromName" in the code below will extract an image in the active sheet if you know the name of the image. Note that the name must be a real name that was manually added to the image. Image names like "Image 1" might or might not work depending in the Excel version.
The method "GetAllImagesInSheet" will loop over all the existing images in the active sheet and save them.
static string GetExtension(TXlsImgType ImgType)
{
switch (ImgType)
{
case TXlsImgType.Bmp: return ".bmp";
case TXlsImgType.Emf: return ".emf";
case TXlsImgType.Jpeg: return ".jpg";
case TXlsImgType.Wmf: return ".wmf";
case TXlsImgType.Tiff: return ".tif";
case TXlsImgType.Gif: return ".gif";
case TXlsImgType.Png: return ".png";
default: return "";
}
}
public static void GetImageFromName()
{
var xls = new XlsFile("myfile.xlsx");
//Get an image named "myimage" and save it
//as myimage.png, myimage.jpeg, etc depending in the
//image type.
string filename = "myimage";
TXlsImgType imageType = TXlsImgType.Unknown;
using (var ImageStream = new FileStream(filename, FileMode.Create))
{
xls.GetImage(-1, "@myimage", ref imageType, ImageStream, true); //For this to work, last parameter must be true.
}
File.Move(filename, Path.ChangeExtension(filename, GetExtension(imageType)));
}
public static void GetAllImagesInSheet()
{
var xls = new XlsFile("myfile.xlsx");
//Get all the images in the active sheet and save them
//as myimage1.png, myimage2.jpeg, etc depending in the
//image type.
TXlsImgType imageType = TXlsImgType.Unknown;
for (int i = 1; i <= xls.ImageCount; i++)
{
string filename = "myimage" + i.ToString();
using (var ImageStream = new FileStream(filename, FileMode.Create))
{
xls.GetImage(i, ref imageType, ImageStream);
}
File.Move(filename, Path.ChangeExtension(filename, GetExtension(imageType)));
}
}
See also
ExcelFile.GetImage(Int32, String, TXlsImgType, Stream, Boolean)
Returns an image and its type. Note that for SVG images, xlsx files store both a PNG and SVG image. In those cases, for backward compatibility reasons, this method will return the PNG image. To get the SVG, call GetImageAlternate
Syntax
Namespace: FlexCel.Core
public void GetImage(Int32 imageIndex, String objectPath, ref TXlsImgType imageType, Stream outStream, Boolean usesObjectIndex)
Parameters
<-> | Parameter | Type | Description |
---|---|---|---|
imageIndex | Int32 | Index of the image. (1 based) | |
objectPath | String | Object path to the image when it is a grouped image. For toplevel images you can use String.Empty. In other case, you need to use the value returned by GetObjectProperties(Int32, Boolean) Important: ObjectPath is ignored if usesObjectIndex is false. If it is "absolute"(it starts with "\\"), then the path includes the objectIndex, and the objectIndex is not used. An object path of "\\1\\2\\3" is exactly the same as using objectIndex = 1 and objectPath = "2\\3" You might also use the name of the image as object path, like in xls.GetImage(-1, "@myimage", ref imageType, ImageStream, true) |
|
ref | imageType | TXlsImgType | Returns the image type for the data returned. (If it is a bmp, jpg or other) |
outStream | Stream | Stream where the image data will be copied. | |
usesObjectIndex | Boolean | If false (the default) then imageIndex is an index to the list of images. When true imageIndex is an index to the list of all objects in the sheet. When you have the object id, you can avoid calling ObjectIndexToImageIndex which is a slow method, by setting this parameter to true. |
Examples
The method "GetImageFromName" in the code below will extract an image in the active sheet if you know the name of the image. Note that the name must be a real name that was manually added to the image. Image names like "Image 1" might or might not work depending in the Excel version.
The method "GetAllImagesInSheet" will loop over all the existing images in the active sheet and save them.
static string GetExtension(TXlsImgType ImgType)
{
switch (ImgType)
{
case TXlsImgType.Bmp: return ".bmp";
case TXlsImgType.Emf: return ".emf";
case TXlsImgType.Jpeg: return ".jpg";
case TXlsImgType.Wmf: return ".wmf";
case TXlsImgType.Tiff: return ".tif";
case TXlsImgType.Gif: return ".gif";
case TXlsImgType.Png: return ".png";
default: return "";
}
}
public static void GetImageFromName()
{
var xls = new XlsFile("myfile.xlsx");
//Get an image named "myimage" and save it
//as myimage.png, myimage.jpeg, etc depending in the
//image type.
string filename = "myimage";
TXlsImgType imageType = TXlsImgType.Unknown;
using (var ImageStream = new FileStream(filename, FileMode.Create))
{
xls.GetImage(-1, "@myimage", ref imageType, ImageStream, true); //For this to work, last parameter must be true.
}
File.Move(filename, Path.ChangeExtension(filename, GetExtension(imageType)));
}
public static void GetAllImagesInSheet()
{
var xls = new XlsFile("myfile.xlsx");
//Get all the images in the active sheet and save them
//as myimage1.png, myimage2.jpeg, etc depending in the
//image type.
TXlsImgType imageType = TXlsImgType.Unknown;
for (int i = 1; i <= xls.ImageCount; i++)
{
string filename = "myimage" + i.ToString();
using (var ImageStream = new FileStream(filename, FileMode.Create))
{
xls.GetImage(i, ref imageType, ImageStream);
}
File.Move(filename, Path.ChangeExtension(filename, GetExtension(imageType)));
}
}