<p>I'm loading an image from a file, and I want to know how to validate the image before it is fully read from the file.</p>

<pre><code>string filePath = "image.jpg";
Image newImage = Image.FromFile(filePath);
</code></pre>

<p>The problem occurs when image.jpg isn't really a jpg.  For example, if I create an empty text file and rename it to image.jpg, an OutOfMemory Exception  will be thrown when image.jpg is loaded.</p>

<p>I'm looking for a function that will validate an image given a stream or a file path of the image.</p>

<p>Example function prototype</p>

<pre><code>bool IsValidImage(string fileName);
bool IsValidImage(Stream imageStream);
</code></pre>

I would create a method like:
```
```
Image openImage(string filename);
```
```
in which I handle the exception. If the returned value is Null, there is an invalid file name / type.