在我的應用程序中,我有一個文本框,我可以在其中通過運行時命令生成圖像,但現(xiàn)在我想檢查圖像是否存在。如果沒有圖像,那么我想生成一個標簽。這是我的代碼。我試圖通過正則表達式來實現(xiàn)它。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; }}例如,我在文件夾中有一個名為 ABC.bmp 的圖像。所以如果我輸入它會生成圖像,如果沒有名為 ABC 的圖像,那么它會生成一個標簽“”。如果我輸入錯誤的名稱,它會顯示一個異常。對不起,錯誤的解釋。
1 回答

不負相思意
TA貢獻1777條經(jīng)驗 獲得超10個贊
您可以修改代碼以使用File.Exists方法檢查文件是否存在:
if (File.Exists($"{imageLabel}.bmp"))
{
var image = System.Drawing.Image.FromFile($"{imageLabel}.bmp");
....
- 1 回答
- 0 關注
- 308 瀏覽
添加回答
舉報
0/150
提交
取消