Skip to main content Skip to footer

Download an Image from a Cell

With the release of Spread.NET v17 for WinForms, images can now be added to cells. Inserted images can also be downloaded by users, however the approach to this will depend on how the image was inserted. 

Consider the following example: an image was added to the cell via the Spread Designer in cell A1, while an image inserted via code was added to cell B1. 

To download the image added via Designer in cell A1, we do so similar to the following code: 

            // Image added via designer
            var img = fpSpread1.ActiveSheet.Cells["A1"].Value as LocalImage;
            if (img != null)
            {
                try
                {
                    var data = img.Data as MemoryStream;
                    Bitmap bitmap = new Bitmap(data);
                    bitmap.Save("D://temp//testImage1.png", ImageFormat.Png); //replace with desired output path/filename
                    MessageBox.Show("Image added via designer saved successfully!! \n D://temp//testImage1.png");
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error occured!!\n" + ex.Message);
                }
            }

To download the image added by code in cell B1, we can instead use this approach: 

            // Image added via code
            var imgCode = fpSpread1.ActiveSheet.Cells["B1"].Value as Bitmap;
            if (imgCode != null)
            {
                try
                {
                    imgCode.Save("D://temp//testImage1.png", ImageFormat.Png); //replace with desired output path/filename
                    MessageBox.Show("Image added via code saved successfully!! \n D://temp//testImage1.png");
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error occured!!\n" + ex.Message);
                }
            }

 

You will have to edit the paths for both ways per your project's requirements.

Tye Glenz