我试图动态地在PictureBox
中显示图片。图像源存储在文本文件中。从文件中读取图像路径时,它会一直显示图像错误符号。当我在代码中包含路径时,它就可以工作。
文本文件行示例
F01,Nasi Lemak,RM 2,@" Food \ NasiLemak.jpg"
public void readData()
{
try
{
int i = 0;
foreach (string line in File.ReadAllLines("food.txt"))
{
string[] parts = line.Split(',');
foreach (string part in parts)
{
Console.WriteLine("{0}:{1}", i, part);
{
Label LblFId = new Label();
{
//LblFId.AutoSize = true;
LblFId.Size = new System.Drawing.Size(70, 20);
}
Label LblFName = new Label();
{
LblFName.Size = new System.Drawing.Size(70, 20);
}
Label LblFPrice = new Label();
{
LblFPrice.Size = new System.Drawing.Size(70, 20);
}
PictureBox foodPicBox = new PictureBox();
{
foodPicBox.Size = new System.Drawing.Size(200, 200);
foodPicBox.SizeMode = PictureBoxSizeMode.StretchImage;
foodPicBox.BorderStyle = BorderStyle.Fixed3D;
}
Panel fPanel = new Panel();
LblFId.Text = parts[0];
LblFName.Text = parts[1];
LblFPrice.Text = parts[2];
foodPicBox.ImageLocation = parts[3];
fPanel.Controls.Add(LblFId);
fPanel.Controls.Add(LblFName);
fPanel.Controls.Add(LblFPrice);
fPanel.Controls.Add(foodPicBox);
foodFlow.Controls.Add(fPanel);
}
}
i++;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
答案 0 :(得分:1)
问题出在文本文件
的路径中@"Food\NasiLemak.jpg"
这应该像没有@和"
一样保存Food\NasiLemak.jpg
或者您应该编写更多代码来删除这些符号
foodPicBox.ImageLocation = parts[3].Replace("@", "").Replace("\"", "");
这将删除样本,您的问题将得到解决。
此时您还需要关闭foreach语句
foreach (string part in parts)
{Console.WriteLine("{0}:{1}", i, part);}