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

Ghostscript image resizer decoder capable of rendering postscript-based files to bitmaps. More...

Inheritance diagram for ImageResizer.Plugins.PdfRenderer.PdfRendererPlugin:
Inheritance graph
[legend]
Collaboration diagram for ImageResizer.Plugins.PdfRenderer.PdfRendererPlugin:
Collaboration graph
[legend]

Public Member Functions

override Bitmap DecodeStream (Stream s, ResizeSettings settings, string optionalPath)
 Extend this to support alternate image source formats. If the Bitmap class is used for decoding, read gdi-bugs.txt and make sure you set b.Tag to new BitmapTag(optionalPath,stream); More...
 
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...
 
IEnumerable< IIssueGetIssues ()
 
IEnumerable< string > GetSupportedFileExtensions ()
 If the plugin adds support for new file extensions (such as "psd"), they should be returned by this method. More...
 
IEnumerable< string > GetSupportedQuerystringKeys ()
 If the plugin reads any values from the querystring, the names of the keys should be specified here. This information is required so that the HttpModule knows when to handle an image request. More...
 
- Public Member Functions inherited from ImageResizer.Resizing.AbstractImageProcessor
 AbstractImageProcessor ()
 Creates a new AbstractImageProcessor with no extensions More...
 
 AbstractImageProcessor (IEnumerable< BuilderExtension > extensions)
 Creates a new AbstractImageProcessor which will run the specified extensions with each method call. More...
 
virtual Bitmap DecodeStreamFailed (Stream s, ResizeSettings settings, string optionalPath)
 Extensions are executed until one extension returns a non-null value. This is taken to mean that the error has been resolved. Extensions should not throw an exception unless they wish to cause subsequent extensions to not execute. If extensions throw an ArgumentException or ExternalException, it will be wrapped in an ImageCorruptedException instance. If the Bitmap class is used for decoding, read gdi-bugs.txt and make sure you set b.Tag to new BitmapTag(optionalPath,stream); More...
 

Properties

int MaxHeight [get, set]
 Gets or sets maximum height in pixels for rendered output. More...
 
int MaxWidth [get, set]
 Gets or sets maximum width in pixels for rendered output. More...
 
int DefaultHeight [get, set]
 Gets or sets the default height in pixels for rendered output if neither height nor width are specified. More...
 
int DefaultWidth [get, set]
 Gets or sets the default width in pixels for rendered output if neither height nor width are specified. More...
 

Additional Inherited Members

- Protected Member Functions inherited from ImageResizer.Resizing.AbstractImageProcessor
virtual void PreLoadImage (ref object source, ref string path, ref bool disposeSource, ref ResizeSettings settings)
 Extend this to allow additional types of source objects to be accepted by transforming them into Bitmap instances. More...
 
virtual Stream GetStream (object source, ResizeSettings settings, ref bool disposeStream, out string path, out bool restoreStreamPosition)
 Extend this to allow additional types of source objects to be accepted by transforming them into Stream instances. First plugin to return a Stream wins. More...
 
virtual RequestedAction PostDecodeStream (ref Bitmap img, ResizeSettings settings)
 Extend this to modify the Bitmap instance after it has been decoded by DecodeStream or DecodeStreamFailed More...
 
virtual void PreAcquireStream (ref object dest, ResizeSettings settings)
 Extend this to allow additional types of destination objects to be accepted by transforming them into a stream. More...
 
virtual RequestedAction BuildJob (ImageResizer.ImageJob job)
 The method to override if you want to replace the entire pipeline. All Build() calls call this method first. Does nothing in ImageBuilder More...
 
virtual RequestedAction buildToStream (Bitmap source, Stream dest, ResizeSettings settings)
 Called for Build() calls that want the result encoded. (Not for Bitmap Build(source,settings) calls. Only override this method if you need to replace the behavior of image encoding and image processing together, such as adding support for resizing multi-page TIFF files or animated GIFs. More...
 
virtual Bitmap buildToBitmap (Bitmap source, ResizeSettings settings, bool transparencySupported)
 Most calls funnel through here. Default behavior configures an ImageState instance and calls Process(imageState); Shouldn't be overriden for any reason I can think of - use the appropriate virtual method under Process(). If an extension returns a Bitmap instance, it will be used instead of the default behavior. Does NOT dispose of 'source' or 'source's underlying stream. More...
 
virtual RequestedAction OnProcess (ImageState s)
 Process.0 First step of the Process() method. Can replace the entire Process method if RequestAction.Cancel is returned. Can be used to add points to translate (for image maps), and also to modify the settings More...
 
virtual RequestedAction PrepareSourceBitmap (ImageState s)
 Process.1 Switches the bitmap to the correct frame or page, and applies source flipping commands. More...
 
virtual RequestedAction PostPrepareSourceBitmap (ImageState s)
 Process.2 Extend this to apply any pre-processing to the source bitmap that needs to occur before Layout begins More...
 
virtual RequestedAction Layout (ImageState s)
 Process.3(Layout).0: This is the last point at which points to translate should be added. Only return RequestedAction.Cancel if you wish to replace the entire Layout sequence logic. More...
 
virtual RequestedAction FlipExistingPoints (ImageState s)
 Process.3(Layout).1: This is where the points in the layout are flipped the same way the source bitmap was flipped (unless their flags specify otherwise) More...
 
virtual RequestedAction LayoutImage (ImageState s)
 Process.3(Layout).2: Rings 'image' and 'imageArea' are added to the layout. More...
 
virtual RequestedAction PostLayoutImage (ImageState s)
 Process.3(Layout).3: Add rings here to insert them between the image area and the padding More...
 
virtual RequestedAction LayoutPadding (ImageState s)
 Process.3(Layout).4: Ring "padding" is added to the layout More...
 
virtual RequestedAction PostLayoutPadding (ImageState s)
 Process.3(Layout).5: Add rings here to insert them between the padding and the border More...
 
virtual RequestedAction LayoutBorder (ImageState s)
 Process.3(Layout).6: Ring "border" is added to the layout More...
 
virtual RequestedAction PostLayoutBorder (ImageState s)
 Process.3(Layout).7: Add rings here to insert them between the border and the effect rings More...
 
virtual RequestedAction LayoutEffects (ImageState s)
 Process.3(Layout).8: Effects such as 'shadow' are added here. More...
 
virtual RequestedAction PostLayoutEffects (ImageState s)
 Process.3(Layout).9: Add rings here to insert them between the effects and the margin More...
 
virtual RequestedAction LayoutMargin (ImageState s)
 Process.3(Layout).10: Margins are added to the layout More...
 
virtual RequestedAction PostLayoutMargin (ImageState s)
 Process.3(Layout).11: Add rings here to insert them around the margin. Rings will be outermost More...
 
virtual RequestedAction LayoutRotate (ImageState s)
 Process.3(Layout).anytime: Occurs when the layout is rotated. May be called anytime during Layout() More...
 
virtual RequestedAction PostLayoutRotate (ImageState s)
 Process.3(Layout).anytime: Occurs after the layout is rotated. May be called anytime during Layout() More...
 
virtual RequestedAction LayoutNormalize (ImageState s)
 Process.3(Layout).anytime: Occurs when the layout is normalized to 0,0. May be called anytime during Layout() More...
 
virtual RequestedAction PostLayoutNormalize (ImageState s)
 Process.3(Layout).anytime: Occurs after the layout is normalized. May be called anytime during Layout() More...
 
virtual RequestedAction LayoutRound (ImageState s)
 Process.3(Layout).anytime: Occurs when the layout point values are rounded to integers. May be called anytime during Layout() More...
 
virtual RequestedAction PostLayoutRound (ImageState s)
 Process.3(Layout).anytime: Occurs after the layout point values are rounded to integers. May be called anytime during Layout() More...
 
virtual RequestedAction EndLayout (ImageState s)
 Process.3(Layout).12: Occurs once layout has finished. No more changes should occur to points or rings in the layout after this method. destSize is calculated here. More...
 
virtual RequestedAction PrepareDestinationBitmap (ImageState s)
 Process.4: The destination bitmap is created and sized based destSize. A graphics object is initialized for rendering. More...
 
virtual RequestedAction Render (ImageState s)
 Process.5(Render) Rendering. Do not return RequestedAction.Cancel unless you want to replace the entire rendering system. More...
 
virtual RequestedAction RenderBackground (ImageState s)
 Process.5(Render).1 The background color is rendered More...
 
virtual RequestedAction PostRenderBackground (ImageState s)
 Process.5(Render).2 After the background color is rendered More...
 
virtual RequestedAction RenderEffects (ImageState s)
 Process.5(Render).3 Effects (such as a drop shadow or outer glow) are rendered More...
 
virtual RequestedAction PostRenderEffects (ImageState s)
 Process.5(Render).4 After outer effects are rendered More...
 
virtual RequestedAction RenderPadding (ImageState s)
 Process.5(Render).5 Image padding is drawn More...
 
virtual RequestedAction PostRenderPadding (ImageState s)
 Process.5(Render).6 After image padding is drawn More...
 
virtual RequestedAction CreateImageAttribues (ImageState s)
 Process.5(Render).7: An ImageAttributes instance is created if it doesn't already exist. More...
 
virtual RequestedAction PostCreateImageAttributes (ImageState s)
 Process.5(Render).8: The ImageAttributes instance exists and can be modified or replaced. More...
 
virtual RequestedAction PreRenderImage (ImageState s)
 Process.5(Render).9: Plugins have a chance to pre-process the source image before it gets rendered, and save it to s.preRenderBitmap More...
 
virtual RequestedAction RenderImage (ImageState s)
 Process.5(Render).10: The image is copied to the destination parallelogram specified by ring 'image'. More...
 
virtual RequestedAction PostRenderImage (ImageState s)
 Process.5(Render).11: After the image is drawn More...
 
virtual RequestedAction RenderBorder (ImageState s)
 Process.5(Render).12: The border is rendered More...
 
virtual RequestedAction PostRenderBorder (ImageState s)
 Process.5(Render).13: After the border is drawn More...
 
virtual RequestedAction PreRenderOverlays (ImageState s)
 Process.5(Render).14: Any last-minute changes before watermarking or overlays are applied More...
 
virtual RequestedAction RenderOverlays (ImageState s)
 Process.5(Render).15: Watermarks can be rendered here. All image processing should be done More...
 
virtual RequestedAction PreFlushChanges (ImageState s)
 Process.5(Render).16: Called before changes are flushed and the graphics object is destroyed. More...
 
virtual RequestedAction FlushChanges (ImageState s)
 Process.5(Render).17: Changes are flushed to the bitmap here and the graphics object is destroyed. More...
 
virtual RequestedAction PostFlushChanges (ImageState s)
 Process.5(Render).18: Changes have been flushed to the bitmap, but the final bitmap has not been flipped yet. More...
 
virtual RequestedAction ProcessFinalBitmap (ImageState s)
 Process.6: Non-rendering changes to the bitmap object occur here, such as flipping. The graphics object is unavailable. More...
 
virtual RequestedAction EndProcess (ImageState s)
 Process.7: Layout and rendering are both complete. More...
 
- Protected Attributes inherited from ImageResizer.Resizing.AbstractImageProcessor
volatile IEnumerable
< BuilderExtension
exts
 Contains the set of extensions that are called for every method. More...
 

Detailed Description

Ghostscript image resizer decoder capable of rendering postscript-based files to bitmaps.

Definition at line 37 of file PdfRenderer.cs.

Member Function Documentation

override Bitmap ImageResizer.Plugins.PdfRenderer.PdfRendererPlugin.DecodeStream ( Stream  s,
ResizeSettings  settings,
string  optionalPath 
)
inlinevirtual

Extend this to support alternate image source formats. If the Bitmap class is used for decoding, read gdi-bugs.txt and make sure you set b.Tag to new BitmapTag(optionalPath,stream);

Parameters
s
settings
optionalPath
Returns

Reimplemented from ImageResizer.Resizing.AbstractImageProcessor.

Definition at line 149 of file PdfRenderer.cs.

150  {
151  if(string.IsNullOrEmpty(optionalPath))
152  {
153  if (s.CanSeek) {
154  //Check the header instead if no filename is present.
155  byte[] header = new byte[4];
156  s.Read(header, 0, 4);
157  bool isPdf = (header[0] == '%' && header[1] == 'P' && header[2] == 'D' && header[3] == 'F');
158  s.Seek(-4, SeekOrigin.Current); //Restore position.
159 
160  if (!isPdf) return null;
161  } else {
162  return null; //It's not seekable, we can't check the header.
163  }
164  } else if(!_supportedExtensions.Contains(Path.GetExtension(optionalPath), StringComparer.OrdinalIgnoreCase))
165  {
166  // Not a supported format
167  return null;
168  }
169 
170  // Do not allow decoding if Ghostscript there are issues with performing this decode
171  IIssue[] issues = GetIssues().ToArray();
172  if(issues.Length > 0)
173  {
174  string message = string.Join(Environment.NewLine, issues.Select(x => x.Summary).ToArray());
175  throw new InvalidOperationException(message);
176  }
177 
178  // Must write input stream to a temporary file for Ghostscript to process.
179  FileInfo tempInputPathInfo = new FileInfo(Path.GetTempFileName());
180  try
181  {
182  using(FileStream tempInputStream = tempInputPathInfo.Create())
183  {
184  StreamExtensions.CopyToStream(s, tempInputStream);
185  }
186 
187  // Get information about the PDF such as page count and media boxes
188  // Although this creates a second trip to Ghostscript engine, it's not possible to generate a rendered image in exact
189  // dimensions requested. Skipping this step will cause a rendered image, of some size, to be resized further.
190  PdfInfo pdfInfo = GetPdfInfo(tempInputPathInfo.FullName);
191 
192  // Extract the requested page number from resize settings, or default to first page
193  int pageNumber = settings.GetValueOrDefault("page", 1);
194 
195  // Try to get the page number from PDF info. If not available, abort. This is caused by
196  // requesting a page that does not exist.
197  PageInfo pageInfo = pdfInfo.Pages.SingleOrDefault(x => x.Number == pageNumber);
198  if(pageInfo == null)
199  {
200  return null;
201  }
202 
203  // We only support media box (as opposed to clip, bleed, art, etc.) If this is not available, abort.
204  if(pageInfo.MediaBox == null)
205  {
206  return null;
207  }
208 
209  // Get the output size of the generated bitmap by applying the resize settings and media box.
210  Size outputSize = (pageInfo.Rotate == -90 || pageInfo.Rotate == 90) ?
211  GetOutputSize(settings, pageInfo.MediaBox.Height,pageInfo.MediaBox.Width)
212  : GetOutputSize(settings, pageInfo.MediaBox.Width, pageInfo.MediaBox.Height) ;
213 
214 
215 
216 
217  // Create default Ghostscript settings and apply the resize settings.
218  GhostscriptSettings ghostscriptSettings = new GhostscriptSettings
219  {
220  GhostscriptArgument.NoPause,
221  GhostscriptArgument.Quiet,
222  GhostscriptArgument.Safer,
223  GhostscriptArgument.Batch,
224  {GhostscriptArgument.OutputDevice, "pngalpha"},
225  {GhostscriptArgument.MaxBitmap, 24000000},
226  {GhostscriptArgument.RenderingThreads, 4},
227  {GhostscriptArgument.GridFitTT, 0},
228  {GhostscriptArgument.AlignToPixels, 0}, //Subpixel rendering depends on output device... perhaps testing would help determine if 1 would be better?
229  {GhostscriptArgument.FirstPage, pageNumber},
230  {GhostscriptArgument.LastPage, pageNumber},
231  GhostscriptArgument.Printed,
232  GhostscriptArgument.PdfFitPage,
233  GhostscriptArgument.FixedMedia,
234  {GhostscriptArgument.Height, outputSize.Height},
235  {GhostscriptArgument.Width, outputSize.Width}
236  };
237  ApplyResizeSettings(settings, ghostscriptSettings);
238 
239  // Have Ghostscript process the input to a PNG file with transparency.
240  // The PNG will be reloaded and further processed by the resizer pipeline.
241  FileInfo tempOutputPathInfo = new FileInfo(Path.GetTempFileName());
242  try
243  {
244  // Add output file and input file. The input file must be the very last argument.
245  ghostscriptSettings.Add(GhostscriptArgument.OutputFile, tempOutputPathInfo.FullName);
246  ghostscriptSettings.Add(tempInputPathInfo.FullName);
247  _engine.Execute(ghostscriptSettings);
248 
249  // NOTE: Do not dispose of memory stream because it is used as the backing source for the loaded bitmap.
250  MemoryStream memoryStream = new MemoryStream((int)tempOutputPathInfo.Length);
251  using(FileStream fileStream = tempOutputPathInfo.Open(FileMode.Open))
252  {
253  StreamExtensions.CopyToStream(fileStream, memoryStream);
254  }
255 
256  // Per ImagerResizer plugin example source code:
257  // NOTE: If the Bitmap class is used for decoding, read gdi-bugs.txt and make sure you set b.Tag to new BitmapTag(optionalPath,stream);
258  BitmapTag bitmapTag = new BitmapTag("ghostscript.png", memoryStream);
259  return new Bitmap(memoryStream) { Tag = bitmapTag };
260  }
261  //catch(GhostscriptException)
262  //{
263  // // Conversion failed
264  // return null; //or maybe we should show details? If it's a valid PDF?
265  //}
266  finally
267  {
268  tempOutputPathInfo.Delete();
269  }
270  }
271  finally
272  {
273  tempInputPathInfo.Delete();
274  }
275  }
IEnumerable<string> ImageResizer.Plugins.PdfRenderer.PdfRendererPlugin.GetSupportedFileExtensions ( )
inline

If the plugin adds support for new file extensions (such as "psd"), they should be returned by this method.

Returns

Implements ImageResizer.Plugins.IFileExtensionPlugin.

Definition at line 306 of file PdfRenderer.cs.

307  {
308  return _supportedExtensions;
309  }
IEnumerable<string> ImageResizer.Plugins.PdfRenderer.PdfRendererPlugin.GetSupportedQuerystringKeys ( )
inline

If the plugin reads any values from the querystring, the names of the keys should be specified here. This information is required so that the HttpModule knows when to handle an image request.

Returns

Implements ImageResizer.Plugins.IQuerystringPlugin.

Definition at line 311 of file PdfRenderer.cs.

312  {
313  return _queryStringKeys;
314  }
IPlugin ImageResizer.Plugins.PdfRenderer.PdfRendererPlugin.Install ( Config  c)
inline

Installs the plugin in the specified Config instance. The plugin must handle all the work of loading settings, registering the plugin etc.

Parameters
c
Returns

Implements ImageResizer.Plugins.IPlugin.

Definition at line 277 of file PdfRenderer.cs.

278  {
279  if(c != null)
280  {
281  c.Plugins.add_plugin(this);
282  }
283  return this;
284  }
bool ImageResizer.Plugins.PdfRenderer.PdfRendererPlugin.Uninstall ( Config  c)
inline

Uninstalls the plugin. Should reverse all changes made during Install

Parameters
c
Returns

Implements ImageResizer.Plugins.IPlugin.

Definition at line 286 of file PdfRenderer.cs.

287  {
288  if(c != null)
289  {
290  c.Plugins.remove_plugin(this);
291  }
292  return true;
293  }

Property Documentation

int ImageResizer.Plugins.PdfRenderer.PdfRendererPlugin.DefaultHeight
getset

Gets or sets the default height in pixels for rendered output if neither height nor width are specified.

Exceptions
ArgumentOutOfRangeExceptionValue is less than or equal to zero.

Definition at line 121 of file PdfRenderer.cs.

int ImageResizer.Plugins.PdfRenderer.PdfRendererPlugin.DefaultWidth
getset

Gets or sets the default width in pixels for rendered output if neither height nor width are specified.

Exceptions
ArgumentOutOfRangeExceptionValue is less than or equal to zero.

Definition at line 137 of file PdfRenderer.cs.

int ImageResizer.Plugins.PdfRenderer.PdfRendererPlugin.MaxHeight
getset

Gets or sets maximum height in pixels for rendered output.

Exceptions
ArgumentOutOfRangeExceptionValue is less than or equal to zero.

Definition at line 91 of file PdfRenderer.cs.

int ImageResizer.Plugins.PdfRenderer.PdfRendererPlugin.MaxWidth
getset

Gets or sets maximum width in pixels for rendered output.

Exceptions
ArgumentOutOfRangeExceptionValue is less than or equal to zero.

Definition at line 106 of file PdfRenderer.cs.


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