[英]C#: Unable to assign Image to PictureBox image source path from text file in winforms
我正在尝试在PictureBox
动态显示图像。 图像源存储在文本文件中。 从文件中读取图像路径时,它一直显示图像错误符号。 当我在代码中包含路径时,它会起作用。
文本文件行示例
F01,椰浆饭,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);
}
}
问题出在文本文件的路径中
@"Food\NasiLemak.jpg"
这应该像没有@ 和“
Food\NasiLemak.jpg
或者你应该写更多的代码来删除这些符号
foodPicBox.ImageLocation = parts[3].Replace("@", "").Replace("\"", "");
这将删除样本,您的问题将得到解决。
此时您还需要关闭 foreach 语句
foreach (string part in parts)
{Console.WriteLine("{0}:{1}", i, part);}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.