I had to implement a custom Image Resizer for SharePoint. The image resizer is able to scale any images to a requested dimension and the new image is automatically stored in the source SharePoint image library. For subsequent requests the produced image will be used from the library. Due to the fact that the resizer is used for an internet facing sharepoint web site, it was important to manage the anonymous access, the moderation and version control settings of the image library. After the implementation I noticed that all image types (jpg,gif,png...) were generated without any problem. But if I tried to open a generated jpg Image in Internet Explorer I saw only a red cross. I tried the same in FireFox and the image was displayed correctly. I analyzed the network traffic with fiddler and everything looks good.
Long story short: The problem: missing proper Image Codec Information. Without these explicit settings the generated jpg is encoded using CMYK. But the Internet Explorer has issues displaying JPG/JPEG images that have been encoded with CMYK. To solve this issue I had to change the encoding:
ImageCodecInfo codec = GetEncoderInfo(string.Format("image/{0}",fileType));
if (codec != null)
{
int quality = 100;
EncoderParameter qualityParam = new EncoderParameter
(System.Drawing.Imaging.Encoder.Quality, quality);
encoderParameters = new EncoderParameters(1);
encoderParameters.Param[0] = qualityParam;
}
And the GetEncoderInfo:
private static ImageCodecInfo GetEncoderInfo(string mimeType)
{
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
foreach (ImageCodecInfo codec in codecs)
if (string.Compare(codec.MimeType, mimeType, true, CultureInfo.InvariantCulture) == 0)
return codec;
return null;
}
That's all.