Click here to Skip to main content
15,890,690 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB
worksheet.Pictures.Add("Penguins.jpg", PositioningMode.MoveAndSize,

    new AnchorCell(worksheet.Columns[0], worksheet.Rows[0], true),

    new AnchorCell(worksheet.Columns[10], worksheet.Rows[10], false));


How to Pass First Parameter which is image path Dynamically oR relative path in first parameter


Thank You
Posted

If relative is not working then just use the full path. You can get the path to your workbook by using

C#
Application.ActiveWorkbook.Path
 
Share this answer
 
Comments
Maciej Los 2-Aug-13 8:51am    
I think you misunderstood the question...
ZurdoDev 2-Aug-13 8:53am    
Perhaps but how so? They asked how to do relative path for image.
Maciej Los 2-Aug-13 9:15am    
Essentially nothing... It's your choice what you wanna do with it... ;)
bhavesh002 2-Aug-13 9:23am    
in above methos i pass first parameter
"D:\\My Projects\\kkk\\BackstopWebserviceTest\\Images\\logo.png";

but i want to pass it dynamic like @".\\Images\\logo.png" somthing like this but it not work so any other syntax or way to pass this first parameter
ZurdoDev 2-Aug-13 9:37am    
The term you need is relative, not dynamic. Solution 2 addresses dynamic and mine addressed relative.

Excel does not support a relative path like you would do in the web. So, what you do is get the current path by using Applicaiton.ActiveWorkbook.Path and use that to build your path.
[EDIT]
You can use application StartupPath[^], which returns the path for the executable file that started the application, not including the executable name.

HOW TO: Determine the Executing Application's Path[^]

[/EDIT]
C#
String sFile = "MyPicture.jpg";
String sPath = Application.StartupPath + "\\Images\\" + sFile;

worksheet.Pictures.Add(sPath + , PositioningMode.MoveAndSize,  new AnchorCell(worksheet.Columns[0], worksheet.Rows[0], true),  new AnchorCell(worksheet.Columns[10], worksheet.Rows[10], false));


Have a look here:
1) pictures
Adding pictures to a spreadsheet is very easy[^]
2) using shapes
How to insert a picture in excel from C# App[^]
3) bitmap from Clipboard
How to programmatically add a Picture or Icon to an Excel WorkSheet using C#[^]
 
Share this answer
 
v3
Comments
bhavesh002 2-Aug-13 9:26am    
String sPath = "Drive:\\Folder1\\NameOfTheFile.jpg";
this parameter i want dynamic path because if i pass this path and when i change project location it is not work or i need to change manually
Maciej Los 2-Aug-13 10:39am    
See my updated answer. Is that what you want?
bhavesh002 3-Aug-13 5:32am    
thanks,

not exactly Because i am working with WPF application but it's help me to solve my problem,
i solve my problem using below code

string currentDirectory = AppDomain.CurrentDomain.BaseDirectory;
string filename = string.Empty;
string[] array = currentDirectory.Split('\\');

for (int i = 0; i < array.Length - 3; i++)
{
filename = filename + array[i] + "\\";
}

filename=filename +"Images\\logo.png";
Maciej Los 4-Aug-13 14:25pm    
Please, mark helpful answers as solution (green button).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900