0

I am having trouble loading Tiff files in C# application. When tiff file is uploaded into application, it gets hanged up. This was happening because that tiff file is corrupt.

Please recommended a solution to identify this corrupt tiff files, so that application does not crash or hangs up when uploaded.

Below is the code snippet where when the file is opened in bmp object, the application hangs at that line of code.

public void ReadTiff(byte[] fileData)
{
        try
        {
            using (var ms = new MemoryStream(fileData))
            {
                using (var bmp = new Bitmap(ms))
                {
                    // Some code
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
}

I have tried following with the file:

  1. Tried opening in paint, it couldn't open.
  2. Also tried opening in Windows viewer, it couldn't open.
  3. Tried opening in multiple online image viewers, still it couldn't open.
Fuzzybear
  • 1,388
  • 2
  • 25
  • 42
  • 2
    You need to show how you're creating the file that you want to open in paint. You also need to show how you're reading the file into the byte array. – Terry Carmen Jul 02 '18 at 14:05
  • `catch (Exception ex) { throw ex; }` is completely pointless and just adds the overhead of re-throwing the exception. Are you planning to do some logging which you haven't implemented yet? If not, then just remove the try/catch, it's not useful – ADyson Jul 02 '18 at 14:07
  • 1
    "the application hangs"...it just sits there? How long did you wait? There's no exception of any kind? – ADyson Jul 02 '18 at 14:08
  • Have you run this in a debugger and put a breakpoint on the `throw ex` line, to see if an exception is being thrown? – Joe Sewell Jul 02 '18 at 14:11
  • File a security bug w/ MS. Corrupted TIFF should throw OutOfMemoryException. The TIFF parser is in native code. This is bad. – Joshua Jul 02 '18 at 14:18
  • 1
    Have you opened the TIFF file in a binary editor to see what it is that might be corrupt? It could be as simple as an incorrect file extension (e.g. the image is actually JPEG format but the file extension is .TIF) That might give you an idea what to look for to determine if it is corrupt or not. – Duston Jul 02 '18 at 14:27
  • 1
    @ADyson `throw ex;` is not only completely pointless, is also **harmful**, as it loses the stacktrace of the exception and makes debugging more difficult. You should use `throw;` instead. Look here also : https://stackoverflow.com/q/730250/2557263 – Alejandro Jul 02 '18 at 15:53

1 Answers1

0

Use below function to get an image type if it returns ImageFormat.unknown for your image, it's corrupted and it's not a valid image

public static ImageFormat GetImageFormat(byte[] bytes)
{
    var bmp    = Encoding.ASCII.GetBytes("BM");     // BMP
    var gif    = Encoding.ASCII.GetBytes("GIF");    // GIF
    var png    = new byte[] { 137, 80, 78, 71 };    // PNG
    var tiff   = new byte[] { 73, 73, 42 };         // TIFF
    var tiff2  = new byte[] { 77, 77, 42 };         // TIFF
    var jpeg   = new byte[] { 255, 216, 255, 224 }; // jpeg
    var jpeg2  = new byte[] { 255, 216, 255, 225 }; // jpeg canon

    if (bmp.SequenceEqual(bytes.Take(bmp.Length)))
        return ImageFormat.bmp;

    if (gif.SequenceEqual(bytes.Take(gif.Length)))
        return ImageFormat.gif;

    if (png.SequenceEqual(bytes.Take(png.Length)))
        return ImageFormat.png;

    if (tiff.SequenceEqual(bytes.Take(tiff.Length)))
        return ImageFormat.tiff;

    if (tiff2.SequenceEqual(bytes.Take(tiff2.Length)))
        return ImageFormat.tiff;

    if (jpeg.SequenceEqual(bytes.Take(jpeg.Length)))
        return ImageFormat.jpeg;

    if (jpeg2.SequenceEqual(bytes.Take(jpeg2.Length)))
        return ImageFormat.jpeg;

    return ImageFormat.unknown;
}

public enum ImageFormat
{
    bmp,
    jpeg,
    gif,
    tiff,
    png,
    unknown
}

Ref:- Validate image from file in C#

Mihir Dave
  • 3,954
  • 1
  • 12
  • 28