Click here to Skip to main content
15,885,366 members
Articles / Programming Languages / C#
Tip/Trick

JPG Tag Stripper And Why You Might Want That

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
14 Aug 2012CPOL3 min read 10.2K   266   4   1
Utility in C# to strip tags from JPG images.

Sample Image

Introduction

First I apologize in advance in case this is a well-known problem with other tried and true solutions. I didn't find one searching CodeProject articles and tips. Some websites (I won't mention names) limit the size of an image file that members can upload for their profile to something like 15 KB - a limit that is amazingly small. But it's hard to be too critical. Suppose you had over nine million members and they all had a 15 KB photo in their profile. That would be over 130 GB of storage the website would need to support on line just for member pictures.

This article shows a utility that can help meet this limit.

Background

Can you tell the difference between these two pictures?

Member at the beach   Member at the beach, Photoshopped

There are subtle differences but unless you blow both up it's very hard to see them. It's surprising then that the one on the left is only 5 KB, which is only a third of the limit, while the one on the right is 21 KB, which is oh-oh too big.

I'll make a long story short and tell you that my trusted and beloved Adobe Photoshop 7.0 is the reason for the difference. All I did was open the left image and then save as the one of the right, telling Photoshop not to include a preview nor ICC profile. If you use Photoshop 7.0, or possibly other versions, you may have already seen this problem.

I don't know what else Photoshop adds besides EXIF tags, but this utility lets you remove them to make a smaller JPG file.

By the way, this utility wasn't intended to strip very large images. There is a file size limit of about 8 MB.  Also, since images are decoded in memory, this utility gets a little sluggish for large images.

Using the code

The toolstrip in this utility has only a 'File' menu with Open, Save, Save As, and Exit. Load a source image by choosing Open.

You can stretch the lower right corner of this utility's window to get a bigger better view of the loaded image.

This utility gives dimensions and size of the loaded image and the size of the image that could be saved at the current Quality Level. The Quality Level is given in Photoshop JPG Quality values, [1..12]. The Windows.Media.Imaging API's require the range to be [1..100] so I used this code to map from the range familiar to me to the one needed by JpegBitmapEncoder.Frames.Add.

C#
private int GetQualityLevel() // Maps range[1..12] to [1..100]
{
    float hundredBased = (((float)trackBar1.Value - 1.0F) * 99.0F / 11.0F);
    int ret = (int)(hundredBased + 1.0F);
    return ret;
}

If you slide the trackbar slider to different quality levels, you will see the effect quality has on the image than can be saved and the size of the saved file. Try sliding to Quality Level 1. You should see a severely deteriorated image.

Points of Interest

I have often saved Photoshop images at Quality 12 because I wanted the best image possible. Creating this utility showed me that sometimes I have increased the size of an image file with no benefit. Loading an image in this utility defaults the Quality Level to 12. If you load an image and see that the Image Size Saved is greater than the original Image Size, its probably safe to save the stripped image at a lower quality level.

On the other hand, if the Quality 12 Image Size Saved is smaller than the original, there may be a slight loss of quality in stripping the source to the file you save. Remember, JPG is a lossy compression. Repeated re-compressions is like re-multiplying by .999.

And, yes, I used this utility to make my little profile photo, all 14 KB of it.

Reference

History

  • Submitted to CodeProject 14 Aug 2012

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
CEO
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Alexander Schwoch18-Oct-12 9:32
Alexander Schwoch18-Oct-12 9:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.