簡體   English   中英

這行代碼是什么意思,我該如何解決錯誤?

[英]What does this line of code mean and how can I fix the error?

我在這行代碼中有錯誤,我無法介入,因為錯誤會立即彈出。 誰能告訴我這行代碼到底想做什么?

var image = ws2.Drawings.AddPicture(
    imagesLocations[i].Name, 
    Image.FromFile(imagesLocations[i].Tests
                                     .FirstOrDefault(t => t.IsReference)
                                     .ImagePath));

錯誤:

引發異常:System.Drawing.dll中的“ System.IO.FileNotFoundException”

附加信息:C:\\ Users \\ ..“完整路徑” .. \\ Initial.png

一些聲明:

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是書面課程的自定義對象

IsReference是在特定條件下用於Test的局部Boolean

ImagePath是用於Test的本地字符串

所以錯誤是它試圖從路徑中獲取圖像以將其放入excel文件。 但是,該圖像不存在。 我需要了解這一行代碼,以便防止發生這種情況。 我該怎么做的任何幫助都非常有幫助。 請和謝謝

因此,正如@HimBromBeere所建議的那樣,他是對的。

這是我所做的,使我檢查並運行錯誤:

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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM