user1889838
user1889838

Reputation: 343

Read Image file metadata

I want to upload an image file and then extract its basic information (author, dimensions, date created, modified, etc) and display it to the user. How can I do it.

A solution or reference to this problem in asp.net c# code would be helpful. But javascript or php would be ok as well.

Upvotes: 1

Views: 12117

Answers (4)

Hosea Kambonde
Hosea Kambonde

Reputation: 301

try this...

    private string doUpload()
    {
        // Initialize variables
        string sSavePath;

        sSavePath = "images/";

        // Check file size (mustn’t be 0)
        HttpPostedFile myFile = FileUpload1.PostedFile;
        int nFileLen = myFile.ContentLength;
        if (nFileLen == 0)
        {
            //**************
            //lblOutput.Text = "No file was uploaded.";
            return null;
        }

        // Check file extension (must be JPG)
        if (System.IO.Path.GetExtension(myFile.FileName).ToLower() != ".jpg")
        {
            //**************
            //lblOutput.Text = "The file must have an extension of JPG";
            return null;
        }

        // Read file into a data stream
        byte[] myData = new Byte[nFileLen];
        myFile.InputStream.Read(myData, 0, nFileLen);

        // Make sure a duplicate file doesn’t exist.  If it does, keep on appending an 
        // incremental numeric until it is unique
        string sFilename = System.IO.Path.GetFileName(myFile.FileName);
        int file_append = 0;
        while (System.IO.File.Exists(Server.MapPath(sSavePath + sFilename)))
        {
            file_append++;
            sFilename = System.IO.Path.GetFileNameWithoutExtension(myFile.FileName)
                             + file_append.ToString() + ".jpg";
        }

        // Save the stream to disk
        System.IO.FileStream newFile
                = new System.IO.FileStream(Server.MapPath(sSavePath + sFilename),
                                           System.IO.FileMode.Create);
        newFile.Write(myData, 0, myData.Length);
        newFile.Close();

        return sFilename;
    }

Upvotes: -1

Mahmoud Samy
Mahmoud Samy

Reputation: 2852

Bitmap image = new Bitmap(fileName);
PropertyItem[] propItems = image.PropertyItems;

foreach (PropertyItem item in propItems)
{
  Console.WriteLine("iD: 0x" + item.Id.ToString("x"));
}

MSDN Reference

C# Tutorial Reference

Upvotes: 0

muzieh
muzieh

Reputation: 98

C# solution could be found here: Link1 Link2

Upvotes: 0

Rajeshkumar Kandhasamy
Rajeshkumar Kandhasamy

Reputation: 6461

Check this Link. You will get more Clearance about GetDetailsOf() and its File Properties based on the Win-OS version wise.

If you want to use C# code use below code to get Metadata's:

List<string> arrHeaders = new List<string>();

 Shell shell = new ShellClass();
 Folder rFolder = shell.NameSpace(_rootPath);
 FolderItem rFiles = rFolder.ParseName(filename);

 for (int i = 0; i < short.MaxValue; i++)
 {
      string value = rFolder.GetDetailsOf(rFiles, i).Trim();
      arrHeaders.Add(value);
 }

Upvotes: 2

Related Questions