C# 如何检查图像是否在WPF中的文件夹中?

C# 如何检查图像是否在WPF中的文件夹中?,c#,C#,在我的应用程序中,我有一个文本框,可以在运行时通过命令从中生成图像,但现在我想检查图像是否存在。如果没有图像,那么我想生成一个标签。这是我的密码。我试图通过正则表达式实现它 else if (Regex.IsMatch(label, "^<IMG.*>")) { var imageLabel = Regex.Replace(label, "<IMG|>", ""); if (System.Drawing.Image.FromFile($"{imageLa

在我的应用程序中,我有一个文本框,可以在运行时通过命令从中生成图像,但现在我想检查图像是否存在。如果没有图像,那么我想生成一个标签。这是我的密码。我试图通过正则表达式实现它

else if (Regex.IsMatch(label, "^<IMG.*>"))
{
    var imageLabel = Regex.Replace(label, "<IMG|>", "");

    if (System.Drawing.Image.FromFile($"{imageLabel}.bmp") != null)
    {
        var image = System.Drawing.Image.FromFile($"{imageLabel}.bmp");
        graphics.DrawImage(image, x, y, image.Width, image.Height);

        x = image.Width + 5f;

        if (image.Height > rowHeight)
        {
            rowHeight = image.Height;
        }
    }
    else
    {
        var font = GetRowFont(isBold, isUnderLine, isHigh, selectedCharwidth);

        graphics.DrawString("<?>", font, Brushes.Black, new PointF(x, y));

        x += label.Length * font.Size;
    }
}
else if(Regex.IsMatch(标签“^”))
{
var imageLabel=Regex.Replace(标签“,”);
if(System.Drawing.Image.FromFile($“{imageLabel}.bmp”)!=null
{
var image=System.Drawing.image.FromFile($“{imageLabel}.bmp”);
图形.绘图图像(图像,x,y,图像.宽度,图像.高度);
x=图像。宽度+5f;
如果(image.Height>行高)
{
rowHeight=image.Height;
}
}
其他的
{
var font=GetRowFont(isBold、isUnderLine、isHigh、selectedCharwidth);
图形。抽绳(“”,字体,画笔。黑色,新的点F(x,y));
x+=标签长度*字体大小;
}
}

例如,我在文件夹中有一个名为ABC.bmp的图像。因此,如果我键入该图像,将生成该图像,如果没有名为ABC的图像,则将生成标签“”。如果我输入了错误的名称,它会显示一个异常。很抱歉解释得不好

您可以通过以下方法修改代码以检查文件是否存在:


@ShantoSiddiq我知道你有很多未回答的问题。请务必阅读这篇文章:你本可以投票以重复的方式结束这个问题。我可能应该考虑这样做——只是回答而没有考虑搜索重复的问题。
 if (File.Exists($"{imageLabel}.bmp"))
 {
    var image = System.Drawing.Image.FromFile($"{imageLabel}.bmp");
    ....