How To Add Pictures Or Images In Excel Sheet Using EPPlus .NET Application (C#) - Part Five

First, we need to add two namespaces:
  • using System.Drawing;
  • using OfficeOpenXml.Drawing;
Example
  • Image img = Image.FromFile(@"D:\sample.png");
Now, the next thing that hits our mind is that image is an abstract class and we never create an instance of abstract class. So, the above line violates the rules of Object Oriented Programming(OOP) concept.  
 
The answer is - No, OOP concept is still valid for this above line.

This is because the real fact that Image.FromFile(@"D:\sample.png") returns an image reference variable. It does not mean that it returns an instance of abstract class. It can only return a subclass (means that class is inherited) of abstract Image class.

The method FromFile(@"D:\sample.png") determines what it returns based on what it finds in the file and gives to the subclass of Image class. I think now your concept is much clearer.

Next lines,
  • int RowIndex = 4;
  • int ColIndex = 2;
  • ExcelPicture pic = wsSheet1.Drawings.AddPicture("Picture_Name", img);
  • pic.SetPosition(RowIndex, 0, ColIndex, 0);
Here, ExcelPicture is seal class and it is inherited from ExcelDrawing class. Here, Drawings property is the type of ExcelDrawings class and assigns a property of ExcelSheet class object wsSheet1.

In the next line, if we go to the definition of ExcelPicture class, as we can see - it has no SetPosition(RowIndex - 1, 0, ColIndex - 1, 5) method. This method is given from parent class ExcelDrawing.

Here, SetPosition() is two overloaded versions. We have already seen the first overload method; it accepts SetPosition(int RowIndex, int RowOffsetPixels, int ColIndex, int ColumnOffsetPixels) & second overload is SetPosition(PixelTop, PixelLeft), both parameters are int type. PixelTop count the pixel from top & PixelLeft count the pixel from the left size of your computer screen.

For Example
  • ExcelPicture pic = wsSheet1.Drawings.AddPicture("Picture_Name", img);
  • int PixelTop = 88;
  • int PixelLeft = 129;
  • pic.SetPosition(PixelTop, PixelLeft);
At the end, ExcelPicture has own method SetSize(int Width, int Height) & SetSize(int percent) (two overloaded versions). It can accept picture width & height as integer type parameter & second overload method accepts overall original picture width & height in percentage.

Note

Resizing columns and rows after using SetSize(int percent) function will affect the size of picture.

For example -
  • int Width = 320;
  • int Height= 200;
  • pic.SetSize(Width, Height);
  • pic.SetSize(40);
Output in Excel sheet will look like below.


Source code
  1. using OfficeOpenXml;  
  2. using System.IO;  
  3. //add two new namespace  
  4. using OfficeOpenXml.Drawing;  
  5. using System.Drawing;  
  6. class Program {  
  7.     static void Main(string[] args) {  
  8.         ExcelPackage ExcelPkg = new ExcelPackage();  
  9.         ExcelWorksheet wsSheet1 = ExcelPkg.Workbook.Worksheets.Add("Sheet1");  
  10.         using(ExcelRange Rng = wsSheet1.Cells[2, 2, 2, 2]) {  
  11.             Rng.Value = "Welcome to Everyday be coding - tutorials for beginners";  
  12.             //Rng.Merge = true;  
  13.             Rng.Style.Font.Size = 16;  
  14.             Rng.Style.Font.Bold = true;  
  15.             Rng.Style.Font.Italic = true;  
  16.         }  
  17.         int rowIndex = 4;  
  18.         int colIndex = 2;  
  19.         int PixelTop = 88;  
  20.         int PixelLeft = 129;  
  21.         int Height = 320;  
  22.         int Width = 200;  
  23.         Image img = Image.FromFile(@ "D:\Sample.png");  
  24.         ExcelPicture pic = wsSheet1.Drawings.AddPicture("Sample", img);  
  25.         pic.SetPosition(rowIndex, 0, colIndex, 0);  
  26.         //pic.SetPosition(PixelTop, PixelLeft);  
  27.         pic.SetSize(Height, Width);  
  28.         //pic.SetSize(40);  
  29.         wsSheet1.Protection.IsProtected = false;  
  30.         wsSheet1.Protection.AllowSelectLockedCells = false;  
  31.         ExcelPkg.SaveAs(new FileInfo(@ "D:\New.xlsx"));  
  32.     }  
  33. }  
Now, build and execute this code. File is (New.xlsx) stored on the D: drive of computer.
Thank you for reading this article. Please subscribe to my YouTube Channel & don't forget to like and share.

YouTube :https://goo.gl/rt4tHH
Facebook :https://goo.gl/m2skDb
Twitter :https://goo.gl/nUwGnf