Solved

How Do I Prevent the Loss of EXIF Data when RESAMPLING a file with C# System.Drawing?

Posted on 2013-05-11
3
588 Views
Last Modified: 2013-09-05
When images are resampled in ASP.NET they lose their Original Date of Capture and other important EXIF data such as Latitude and Longitude. The images will inevitably be encoded to reduce filesize as well so any solutions need to consider this also. The SetPropertyItem is the one of interest. Is it also possible to use this method to extract the EXIF data values without using specific EXIF HEX code methods. Below is my basic code prior to processing;
 using (Bitmap originalImage = new Bitmap(file.InputStream))
            {

                Bitmap bitmapImage = ResizeImage(RadUpload1.UploadedFiles[0].InputStream);        
                
                //bitmapImage.SetPropertyItem(bitmapImage.PropertyItems[0]);

                System.IO.MemoryStream stream = new System.IO.MemoryStream();
                bitmapImage.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);

Open in new window

0
Comment
Question by:XGIS
  • 2
3 Comments
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 39159213
my solution for you would be to extracting the EXIT data before resampling the image and reset them again afterwards, since when loading the image to Bitmap u essentially losing the EXIT data.
check this code of extracting EXIT data from image:
http://www.codeproject.com/Articles/11305/EXIFextractor-library-to-extract-EXIF-information

here's a code usage:
            var imagefile = @"C:\Users\m.rivkin\Pictures\20130504_112103.jpg";
//extract all EXIF data before resizing image
            var props = EXIFextractor.GetExifProperties(imagefile);
            using (Bitmap originalImage = new Bitmap(imagefile))
            {
                Bitmap bmp = new Bitmap(originalImage.Width / 2, originalImage.Height / 2);
                
                using (Graphics g = Graphics.FromImage(bmp))
                {
                    g.DrawImage(originalImage, 0, 0, originalImage.Width / 2, originalImage.Height / 2);
                }

//set EXIF data back to modified image
                foreach (var item in props)
                {
                    bmp.SetPropertyItem(item);
                }

                bmp.Save(@"c:\temp\1.jpg");
            }

Open in new window

0
 
LVL 7

Author Comment

by:XGIS
ID: 39166933
Thankyou for that i will check it out.  I did use a different exif class in a sample but it did not get lat longs. Hopefully this one will.
0
 
LVL 7

Author Comment

by:XGIS
ID: 39386154
I may need to supply a code sample to further this one, as my integration has not worked yet
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now