hi

can anyone help me out in detecting broken/corrupt images (jpeg, gif, png) using c# code.

- rahul

Recommended Answers

All 3 Replies

You can use this:

using System;
using System.Drawing;
using System.IO;

namespace daniweb
{
  public static class ImageUtils
  {
    public static bool IsValidImage(string FileName)
    {
      if (string.IsNullOrEmpty(FileName))
        throw new ArgumentException("File name cannot be empty", "FileName");
      if (!File.Exists(FileName))
        throw new FileNotFoundException("File could not be found", FileName);

      try
      {
        //This lets us open up a file that is in use by another application
        using (FileStream fs = new FileStream(FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
        {
          using (StreamReader sr = new StreamReader(fs))
          {
            return IsValidImage(fs);
          }
        }
      }
      catch (Exception ex)
      {
        if (ex is IOException)
          throw;
        return false;
      }
    }
    public static bool IsValidImage(Stream stream)
    {
      try
      {
        try
        {
          //Stream should be at beginning -- if not try to reset it
          //if the stream won't allow seeking it will throw exception
          //but try loading the image anyway
          stream.Position = 0;
          stream.Seek(0, SeekOrigin.Begin);
        }
        catch { }
        using (Image img = Image.FromStream(stream))
        {
          img.Dispose();
          return true;
        }
      }
      catch
      {
        return false;
      }
    }
  }
}

Calling it:

private void button3_Click(object sender, EventArgs e)
    {
      bool isImg = ImageUtils.IsValidImage(textEdit1.Text);
      MessageBox.Show((isImg ? "YES" : "NO"));
    }
commented: As always, instructive! +7

You can use this:

using System;
using System.Drawing;
using System.IO;

namespace daniweb
{
  public static class ImageUtils
  {
    public static bool IsValidImage(string FileName)
    {
      if (string.IsNullOrEmpty(FileName))
        throw new ArgumentException("File name cannot be empty", "FileName");
      if (!File.Exists(FileName))
        throw new FileNotFoundException("File could not be found", FileName);

      try
      {
        //This lets us open up a file that is in use by another application
        using (FileStream fs = new FileStream(FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
        {
          using (StreamReader sr = new StreamReader(fs))
          {
            return IsValidImage(fs);
          }
        }
      }
      catch (Exception ex)
      {
        if (ex is IOException)
          throw;
        return false;
      }
    }
    public static bool IsValidImage(Stream stream)
    {
      try
      {
        try
        {
          //Stream should be at beginning -- if not try to reset it
          //if the stream won't allow seeking it will throw exception
          //but try loading the image anyway
          stream.Position = 0;
          stream.Seek(0, SeekOrigin.Begin);
        }
        catch { }
        using (Image img = Image.FromStream(stream))
        {
          img.Dispose();
          return true;
        }
      }
      catch
      {
        return false;
      }
    }
  }
}

Calling it:

private void button3_Click(object sender, EventArgs e)
    {
      bool isImg = ImageUtils.IsValidImage(textEdit1.Text);
      MessageBox.Show((isImg ? "YES" : "NO"));
    }

hey :) thank u so much for the info

- rahul

You're welcome

Please mark this thread as solved if you have found an answer to your question and good luck!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.