Home > Net >  C# WPF Path to picture in excel
C# WPF Path to picture in excel

Time:08-18

i have a problem, i wanted to shorten filepath to my image

xlWorkSheet.Shapes.AddPicture("C:\\Users\my.name\\source\\repos\test\test\\Resource\\dog.jpg", Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, Left4 10, Top4 12, -1, -1);

to be something like this

xlWorkSheet.Shapes.AddPicture("\\Resource\\dog.jpg", Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, Left4 10, Top4 12, -1, -1)

i need to change it because my program will be used on different computers so users will not have the same filepath

CodePudding user response:

The simplest way to do this would be to use an embedded resource with a temporary file.

First, add the image to your project and set it as an embedded resource. You can set it as an embedded resource in Visual Studio by selecting the file in the Solution Explorer window and hitting the F4 button, then changing the "Build Action" to "Embedded Resource".

Then, you'll need to pull that image out of your DLL and write it out to a temporary file. How can I extract a file from an embedded resource and save it to disk? https://docs.microsoft.com/en-us/dotnet/api/system.io.path.gettempfilename

Lastly, use your new temporary file path as the argument to your AddPicture method.

CodePudding user response:

Excel needs an actual file location to load; hence your premise is a failure.

What you are showing is a path to a build path and depending on how the resource is compiled into the executable, there is no way for an outside process to get an image from an exe/dll as this implies.

  • Related