2
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by sknake
Featured Replies
  • 1
    sknake 1,622   7 Years Ago

    You can use this: [code=c#] 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 … Read More

1

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"));
    }
Comments
As always, instructive!
0

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

0

You're welcome

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.