1

In my application i have a textbox from where i can generate image through command in runtime but now i want to check if the image is existing or not.If there is no image then i want to generate a label. here is my code. i was trying to implement it through regular expression.

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

For example i have an image named ABC.bmp in the folder.So if i type it would generate the image and if there is no image named ABC then it would generate a label ''. It shows an exception if I type wrong name.Sorry for the bad Explanation.

Clemens
  • 123,504
  • 12
  • 155
  • 268

1 Answers1

1

You could modify your code to check if the file exists with the File.Exists method :

 if (File.Exists($"{imageLabel}.bmp"))
 {
    var image = System.Drawing.Image.FromFile($"{imageLabel}.bmp");
    ....
PaulF
  • 6,673
  • 2
  • 18
  • 29
  • @ShantoSiddiq I see you have a lot of unanswered questions. Please do read this post: https://meta.stackexchange.com/q/5234/147063 – default Aug 09 '18 at 13:43
  • You could have voted to close the question as duplicate. – Clemens Aug 09 '18 at 13:47
  • I probably should have thought of doing that - just answered without thinking about searching for duplicates. – PaulF Aug 09 '18 at 13:49