Search Results for

    Show / Hide Table of Contents

    ExcelFile.GetImageAlternate Method

    Returns an image and its type. Currently this method is the same as GetImage(Int32, String, TXlsImgType, Stream, Boolean) for all images except SVG. For SVG images, xlsx files store both a PNG and SVG image. In those cases, this method will return the SVG image. To get the PNG, call GetImage(Int32, String, TXlsImgType, Stream, Boolean)

    Syntax

    Namespace: FlexCel.Core

    public void GetImageAlternate(Int32 imageIndex, String objectPath, out 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)
    out 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)));
            }
    
    
        }
    

    See also

    • ExcelFile
    In This Article
    Back to top FlexCel Studio for the .NET Framework v7.24.0.0
    © 2002 - 2025 tmssoftware.com