ImageResizer  3.4.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events
Public Member Functions | Static Public Member Functions | Properties | List of all members
ImageResizer.Plugins.Basic.DefaultEncoder Class Reference

Provides basic encoding functionality for Jpeg, png, and gif output. Allows adjustable Jpeg compression, but doesn't implement indexed PNG files or quantized GIF files. More...

Inheritance diagram for ImageResizer.Plugins.Basic.DefaultEncoder:
Inheritance graph
[legend]
Collaboration diagram for ImageResizer.Plugins.Basic.DefaultEncoder:
Collaboration graph
[legend]

Public Member Functions

 DefaultEncoder (ImageFormat outputFormat)
 
 DefaultEncoder (ImageFormat outputFormat, int jpegQuality)
 
 DefaultEncoder (ResizeSettings settings, object original)
 
virtual IEncoder CreateIfSuitable (ResizeSettings settings, object original)
 If the encoder can handle the requirements specified by 'settings' and 'source', it should return an encoder instance. If not, it should return null. More...
 
bool IsValidOutputFormat (ImageFormat f)
 Returns true if the this encoder supports the specified image format More...
 
void Write (Image image, System.IO.Stream s)
 Writes the specified image to the stream using Quality and OutputFormat More...
 
virtual IEnumerable< string > GetSupportedQuerystringKeys ()
 Returns the querystring keys used by DefaultEncoder (quality, format, and thumbnail) More...
 
IPlugin Install (Configuration.Config c)
 
bool Uninstall (Configuration.Config c)
 
IEnumerable< FileSignatureGetSignatures ()
 Returns signatures for jpeg, bmp, gif, png, wmf, ico, and tif More...
 
- Public Member Functions inherited from ImageResizer.Encoding.IEncoder
void Write (Image i, Stream s)
 Encodes the image to the specified stream More...
 
- Public Member Functions inherited from ImageResizer.Plugins.IPlugin
IPlugin Install (Config c)
 Installs the plugin in the specified Config instance. The plugin must handle all the work of loading settings, registering the plugin etc. More...
 
bool Uninstall (Config c)
 Uninstalls the plugin. Should reverse all changes made during Install More...
 

Static Public Member Functions

static ImageFormat GetRequestedFormat (string format, ImageFormat defaultValue)
 Tries to parse an ImageFormat from the settings.Format value. If an unrecogized format is specified, returns null. If an unsupported format is specified, it is returned. If no format is specified, returns defaultValue. More...
 
static ImageFormat GetOriginalFormat (object original)
 Attempts to determine the ImageFormat of the source image. First attempts to parse the path, if a string is present in original.Tag. (or if 'original' is a string) Falls back to using original.RawFormat. Returns null if both 'original' is null. RawFormat has a bad reputation, so this may return unexpected values, like MemoryBitmap or something in some situations. More...
 
static ImageFormat GetImageFormatFromPhysicalPath (string path)
 Returns the ImageFormat enumeration value based on the extension in the specified physical path. Extensions can lie, just a guess. More...
 
static string GetExtensionFromImageFormat (ImageFormat format)
 Returns an string instance from the specfied ImageFormat. First matching entry in imageExtensions is used. Returns null if not recognized. More...
 
static ImageFormat GetImageFormatFromExtension (string ext)
 Returns an ImageFormat instance from the specfied file extension. Extensions lie sometimes, just a guess. returns null if not recognized. More...
 
static void AddImageExtension (string extension, ImageFormat matchingFormat)
 
static string GetContentTypeFromImageFormat (ImageFormat format)
 Supports Png, Jpeg, Gif, Bmp, and Tiff. Throws a ArgumentOutOfRangeException if not png, jpeg, gif, bmp, or tiff More...
 
static ImageCodecInfo GetImageCodeInfo (string mimeType)
 Returns the first ImageCodeInfo instance with the specified mime type. Returns null if there are no matches. More...
 
static void SaveJpeg (Image b, Stream target, int quality)
 Saves the specified image to the specified stream using jpeg compression of the specified quality. More...
 
static void SavePng (Image img, Stream target)
 
static void SaveBmp (Image img, Stream target)
 
static void SaveGif (Image img, Stream target)
 

Properties

ImageFormat OutputFormat [get, set]
 If you set this to anything other than Gif, Png, or Jpeg, it will throw an exception. Defaults to Jpeg More...
 
int Quality [get, set]
 0..100 value. The Jpeg compression quality. 90 is the best setting. Not relevant in Png or Gif compression More...
 
bool SupportsTransparency [get]
 Returns true if the desired output type supports transparency. More...
 
string MimeType [get]
 Returns the default mime-type for the OutputFormat More...
 
string Extension [get]
 Returns the default file extesnion for OutputFormat More...
 
- Properties inherited from ImageResizer.Encoding.IEncoder
bool SupportsTransparency [get]
 True if the output format will support transparency as it is currently configured. More...
 
string MimeType [get]
 Returns the appropriate mime-time for the output format as currently configured. More...
 
string Extension [get]
 Returns a file extension appropriate for the output format as currently configured, without a leading dot. More...
 

Detailed Description

Provides basic encoding functionality for Jpeg, png, and gif output. Allows adjustable Jpeg compression, but doesn't implement indexed PNG files or quantized GIF files.

Definition at line 18 of file DefaultEncoder.cs.

Member Function Documentation

virtual IEncoder ImageResizer.Plugins.Basic.DefaultEncoder.CreateIfSuitable ( ResizeSettings  settings,
object  original 
)
inlinevirtual

If the encoder can handle the requirements specified by 'settings' and 'source', it should return an encoder instance. If not, it should return null.

Parameters
settingsRequest settings, like format, quality, colors, dither, etc.
originalMay be a Drawing.Image instance, a path, or null. To provide both, set Image.tag to the path. Helps the encoder detect the original format if the format was not specified. May also be used for palette generation hinting by some encoders.
Returns

Implements ImageResizer.Encoding.IEncoder.

Reimplemented in ImageResizer.Plugins.WicEncoder.WicEncoderPlugin, and ImageResizer.Plugins.WpfBuilder.WpfEncoderPlugin.

Definition at line 51 of file DefaultEncoder.cs.

51  {
52  ImageFormat requestedFormat = GetRequestedFormat(settings.Format, ImageFormat.Jpeg);
53  if (requestedFormat == null || !IsValidOutputFormat(requestedFormat)) return null; //An unsupported format was explicitly specified.
54  return new DefaultEncoder(settings, original);
55  }
static ImageFormat GetRequestedFormat(string format, ImageFormat defaultValue)
Tries to parse an ImageFormat from the settings.Format value. If an unrecogized format is specified...
bool IsValidOutputFormat(ImageFormat f)
Returns true if the this encoder supports the specified image format
static string ImageResizer.Plugins.Basic.DefaultEncoder.GetContentTypeFromImageFormat ( ImageFormat  format)
inlinestatic

Supports Png, Jpeg, Gif, Bmp, and Tiff. Throws a ArgumentOutOfRangeException if not png, jpeg, gif, bmp, or tiff

Parameters
format
Returns

Definition at line 261 of file DefaultEncoder.cs.

262  {
263  if (format == null) throw new ArgumentNullException();
264 
265  if (ImageFormat.Png.Equals(format))
266  return "image/png"; //Changed from image/x-png to image/png on May 14, 2011, per http://www.w3.org/Graphics/PNG/
267  else if (ImageFormat.Jpeg.Equals(format))
268  return "image/jpeg";
269  else if (ImageFormat.Gif.Equals(format))
270  return "image/gif";
271  else if (ImageFormat.Bmp.Equals(format))
272  return "image/x-ms-bmp";
273  else if (ImageFormat.Tiff.Equals(format))
274  return "image/tiff";
275  else
276  {
277  throw new ArgumentOutOfRangeException("Unsupported format " + format.ToString());
278  }
279 
280  }
static string ImageResizer.Plugins.Basic.DefaultEncoder.GetExtensionFromImageFormat ( ImageFormat  format)
inlinestatic

Returns an string instance from the specfied ImageFormat. First matching entry in imageExtensions is used. Returns null if not recognized.

Parameters
format
Returns

Definition at line 184 of file DefaultEncoder.cs.

185  {
186  lock (_syncExts) {
187  foreach (KeyValuePair<string, ImageFormat> p in imageExtensions) {
188  if (p.Value.Guid.Equals(format.Guid)) return p.Key;
189  }
190  }
191  return null;
192  }
static ImageCodecInfo ImageResizer.Plugins.Basic.DefaultEncoder.GetImageCodeInfo ( string  mimeType)
inlinestatic

Returns the first ImageCodeInfo instance with the specified mime type. Returns null if there are no matches.

Parameters
mimeType
Returns

Definition at line 287 of file DefaultEncoder.cs.

287  {
288  ImageCodecInfo[] info = ImageCodecInfo.GetImageEncoders();
289  foreach (ImageCodecInfo ici in info)
290  if (ici.MimeType.Equals(mimeType, StringComparison.OrdinalIgnoreCase)) return ici;
291  return null;
292  }
static ImageFormat ImageResizer.Plugins.Basic.DefaultEncoder.GetImageFormatFromExtension ( string  ext)
inlinestatic

Returns an ImageFormat instance from the specfied file extension. Extensions lie sometimes, just a guess. returns null if not recognized.

Parameters
ext
Returns

Definition at line 231 of file DefaultEncoder.cs.

232  {
233  if (string.IsNullOrEmpty(ext)) return null;
234  lock (_syncExts) {
235  ext = ext.Trim(' ', '.').ToLowerInvariant();
236  if (!imageExtensions.ContainsKey(ext)) return null;
237  return imageExtensions[ext];
238  }
239  }
static ImageFormat ImageResizer.Plugins.Basic.DefaultEncoder.GetImageFormatFromPhysicalPath ( string  path)
inlinestatic

Returns the ImageFormat enumeration value based on the extension in the specified physical path. Extensions can lie, just a guess.

Parameters
path
Returns

Definition at line 173 of file DefaultEncoder.cs.

174  {
175  return GetImageFormatFromExtension(System.IO.Path.GetExtension(path));
176  }
static ImageFormat GetImageFormatFromExtension(string ext)
Returns an ImageFormat instance from the specfied file extension. Extensions lie sometimes, just a guess. returns null if not recognized.
static ImageFormat ImageResizer.Plugins.Basic.DefaultEncoder.GetOriginalFormat ( object  original)
inlinestatic

Attempts to determine the ImageFormat of the source image. First attempts to parse the path, if a string is present in original.Tag. (or if 'original' is a string) Falls back to using original.RawFormat. Returns null if both 'original' is null. RawFormat has a bad reputation, so this may return unexpected values, like MemoryBitmap or something in some situations.

Parameters
originalThe source image that was loaded from a stream, or a string path
Returns

Definition at line 149 of file DefaultEncoder.cs.

149  {
150  if (original == null) return null;
151  //Try to parse the original file extension first.
152  string path = original as string;
153 
154  if (path == null && original is Image) path = ((Image)original).Tag as string;
155 
156  if (path == null && original is Image && ((Image)original).Tag is BitmapTag) path = ((BitmapTag)((Image)original).Tag).Path;
157 
158  //We have a path? Parse it!
159  if (path != null) {
160  ImageFormat f = DefaultEncoder.GetImageFormatFromPhysicalPath(path);
161  if (f != null) return f; //From the path
162  }
163  //Ok, I guess it there (a) wasn't a path, or (b), it didn't have a recognizeable extension
164  if (original is Image) return ((Image)original).RawFormat;
165  return null;
166  }
static ImageFormat ImageResizer.Plugins.Basic.DefaultEncoder.GetRequestedFormat ( string  format,
ImageFormat  defaultValue 
)
inlinestatic

Tries to parse an ImageFormat from the settings.Format value. If an unrecogized format is specified, returns null. If an unsupported format is specified, it is returned. If no format is specified, returns defaultValue.

Parameters
format
defaultValue
Returns

Definition at line 132 of file DefaultEncoder.cs.

132  {
133  ImageFormat f = null;
134  if (!string.IsNullOrEmpty(format)) {
135  f = DefaultEncoder.GetImageFormatFromExtension(format);
136  return f;
137  }
138  //Fallback. No encoder was explicitly specified, so let's try to infer it from the image data.
139  return defaultValue;
140 
141  }
IEnumerable<FileSignature> ImageResizer.Plugins.Basic.DefaultEncoder.GetSignatures ( )
inline

Returns signatures for jpeg, bmp, gif, png, wmf, ico, and tif

Returns

Implements ImageResizer.Plugins.IFileSignatureProvider.

Definition at line 405 of file DefaultEncoder.cs.

406  {
407  //Source http://www.filesignatures.net/
408  return new FileSignature[]{
409  new FileSignature(new byte[] {0xFF, 0xD8, 0xFF}, "jpg", "image/jpeg"),
410  new FileSignature(new byte[] {0x42, 0x4D}, "bmp", "image/x-ms-bmp"), //Can be a BMP or DIB
411  new FileSignature(new byte[] {0x47,0x49,0x46, 0x38}, "gif", "image/gif"),
412  new FileSignature(new byte[] {0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A}, "png","image/png"),
413  new FileSignature(new byte[] {0xD7, 0xCD, 0xC6, 0x9A}, "wmf", "image/x-wmf"),
414  new FileSignature(new byte[] {0x00, 0x00,0x01, 0x00}, "ico", "image/x-icon"), //Can be a printer spool or an icon
415  new FileSignature(new byte[] {0x49, 0x20, 0x49}, "tif", "image/tiff"),
416  new FileSignature(new byte[] {0x49, 0x49, 0x2A, 0x00}, "tif", "image/tiff"),
417  new FileSignature(new byte[] {0x4D, 0x4D, 0x00, 0x2A}, "tif", "image/tiff"),
418  new FileSignature(new byte[] {0x4D, 0x4D, 0x00, 0x2B}, "tif", "image/tiff")
419  };
420  }
virtual IEnumerable<string> ImageResizer.Plugins.Basic.DefaultEncoder.GetSupportedQuerystringKeys ( )
inlinevirtual

Returns the querystring keys used by DefaultEncoder (quality, format, and thumbnail)

Returns

Implements ImageResizer.Plugins.IQuerystringPlugin.

Definition at line 387 of file DefaultEncoder.cs.

387  {
388  return new string[] { "quality", "format", "thumbnail" };
389  }
bool ImageResizer.Plugins.Basic.DefaultEncoder.IsValidOutputFormat ( ImageFormat  f)
inline

Returns true if the this encoder supports the specified image format

Parameters
f
Returns

Definition at line 73 of file DefaultEncoder.cs.

73  {
74  return (ImageFormat.Gif.Equals(f) || ImageFormat.Png.Equals(f) || ImageFormat.Jpeg.Equals(f));
75  }
static void ImageResizer.Plugins.Basic.DefaultEncoder.SaveJpeg ( Image  b,
Stream  target,
int  quality 
)
inlinestatic

Saves the specified image to the specified stream using jpeg compression of the specified quality.

Parameters
b
qualityA number between 0 and 100. Defaults to 90 if passed a negative number. Numbers over 100 are truncated to 100. 90 is a very good setting.
target

Definition at line 303 of file DefaultEncoder.cs.

303  {
304  #region Encoder paramater notes
305  //image/jpeg
306  // The parameter list requires 172 bytes.
307  // There are 4 EncoderParameter objects in the array.
308  // Parameter[0]
309  // The category is Transformation.
310  // The data type is Long.
311  // The number of values is 5.
312  // Parameter[1]
313  // The category is Quality.
314  // The data type is LongRange.
315  // The number of values is 1.
316  // Parameter[2]
317  // The category is LuminanceTable.
318  // The data type is Short.
319  // The number of values is 0.
320  // Parameter[3]
321  // The category is ChrominanceTable.
322  // The data type is Short.
323  // The number of values is 0.
324 
325 
326  // http://msdn.microsoft.com/en-us/library/ms533845(VS.85).aspx
327  // http://msdn.microsoft.com/en-us/library/ms533844(VS.85).aspx
328  // TODO: What about ICC profiles
329  #endregion
330 
331  //Validate quality
332  if (quality < 0) quality = 90; //90 is a very good default to stick with.
333  if (quality > 100) quality = 100;
334  //Prepare paramater for encoder
335  using (EncoderParameters p = new EncoderParameters(1)) {
336  using (var ep = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)quality))
337  {
338  p.Param[0] = ep;
339  //save
340  b.Save(target, GetImageCodeInfo("image/jpeg"), p);
341  }
342  }
343  }
static ImageCodecInfo GetImageCodeInfo(string mimeType)
Returns the first ImageCodeInfo instance with the specified mime type. Returns null if there are no m...
static void ImageResizer.Plugins.Basic.DefaultEncoder.SavePng ( Image  img,
Stream  target 
)
inlinestatic

Saves the image in png form. If Stream 'target' is not seekable, a temporary MemoryStream will be used to buffer the image data into the stream

Parameters
img
target

Definition at line 350 of file DefaultEncoder.cs.

351  {
352  if (!target.CanSeek) {
353  //Write to an intermediate, seekable memory stream (PNG compression requires it)
354  using (MemoryStream ms = new MemoryStream(4096)) {
355  img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
356  ms.WriteTo(target);
357  }
358  } else {
359  //image/png
360  // The parameter list requires 0 bytes.
361  img.Save(target, System.Drawing.Imaging.ImageFormat.Png);
362  }
363  }
void ImageResizer.Plugins.Basic.DefaultEncoder.Write ( Image  image,
System.IO.Stream  s 
)
inline

Writes the specified image to the stream using Quality and OutputFormat

Parameters
image
s

Definition at line 92 of file DefaultEncoder.cs.

92  {
93  if (ImageFormat.Jpeg.Equals(OutputFormat)) SaveJpeg(image, s, this.Quality);
94  else if (ImageFormat.Png.Equals(OutputFormat)) SavePng(image, s);
95  else if (ImageFormat.Gif.Equals(OutputFormat)) SaveGif(image, s);
96  }
static void SaveJpeg(Image b, Stream target, int quality)
Saves the specified image to the specified stream using jpeg compression of the specified quality...
static void SavePng(Image img, Stream target)
ImageFormat OutputFormat
If you set this to anything other than Gif, Png, or Jpeg, it will throw an exception. Defaults to Jpeg
int Quality
0..100 value. The Jpeg compression quality. 90 is the best setting. Not relevant in Png or Gif compre...

Property Documentation

string ImageResizer.Plugins.Basic.DefaultEncoder.Extension
get

Returns the default file extesnion for OutputFormat

Definition at line 116 of file DefaultEncoder.cs.

string ImageResizer.Plugins.Basic.DefaultEncoder.MimeType
get

Returns the default mime-type for the OutputFormat

Definition at line 110 of file DefaultEncoder.cs.

ImageFormat ImageResizer.Plugins.Basic.DefaultEncoder.OutputFormat
getset

If you set this to anything other than Gif, Png, or Jpeg, it will throw an exception. Defaults to Jpeg

Definition at line 61 of file DefaultEncoder.cs.

int ImageResizer.Plugins.Basic.DefaultEncoder.Quality
getset

0..100 value. The Jpeg compression quality. 90 is the best setting. Not relevant in Png or Gif compression

Definition at line 82 of file DefaultEncoder.cs.

bool ImageResizer.Plugins.Basic.DefaultEncoder.SupportsTransparency
get

Returns true if the desired output type supports transparency.

Definition at line 101 of file DefaultEncoder.cs.


The documentation for this class was generated from the following file: