I have an error on this line of code and I couldn't step into it because the error pops right away. Could anyone please tell me what is this line of code trying to do exactly?
var image = ws2.Drawings.AddPicture(
imagesLocations[i].Name,
Image.FromFile(imagesLocations[i].Tests
.FirstOrDefault(t => t.IsReference)
.ImagePath));
Error:
Exception thrown: 'System.IO.FileNotFoundException' in System.Drawing.dll
Additional information: C:\\Users\\.."full path"..\\Initial.png
Some declarations:
ExcelWorksheet ws2 = package.Workbook.Worksheets.Add("Images");
// this is a list of locations, and "location" is a custom class
var imagesLocations = SelectedSession.GetTests()
.Where(t => t.IsReference)
.Select(t => t.Location)
.OrderBy(t => t.DateCreated)
.ThenBy(t => t.Name)
.ToList();
Test
is a custom object of a written class
IsReference
is a local Boolean
for Test
under certain condition
ImagePath
is a local string for Test
So the error is that it is trying to take an image from a path to put it in the excel file. However, the image doesn't exist. I need to understand that line of code so I can prevent it from happening. Any help of how can I do that is very helpful. Please and thanks
So as @HimBromBeere suggested and he was right.
This is what I did that got me the error checking up and running:
var temp = imagesLocations[i].Tests.FirstOrDefault(t => t.IsReference).ImagePath;
if (File.Exists(temp))
{
var temp2 = Image.FromFile(temp);
var image = ws2.Drawings.AddPicture(imagesLocations[i].Name, temp2);
image.SetSize(375, 375);
image.SetPosition(i, 0, 1, 0);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.