
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?

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
.
private int GetQualityLevel() {
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