Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A website I work on generates its thumbnails using the WPF APIs instead of GDI+. You need to add two references to your project to enable this: WindowsBase, PresentationFramework and PresentationCore. Here’s a basic example of how the code might be used:</p> <pre><code>try { using (var input = File.Open(inputFilename, FileMode.Open, FileAccess.Read, FileShare.Read)) using (var thumb = File.Open(thumbFilename, FileMode.Create, FileAccess.Write, FileShare.None)) { Thumbnail(input, thumb, 200, 100); } } catch (MyException) { File.Delete(thumbFilename); } </code></pre> <p>This fits the thumbnail into a 200x100 rectangle, while preserving aspect ratio.</p> <p>(The real website doesn’t do it quite like the above. What we actually do is attempt to generate the smallest thumbnail in the file upload POST handler. We use a memory stream to hold the resulting thumbnail. If the thumbnail could be generated correctly, we save the upload and the small thumbnail, otherwise we return an error response to the client. Other thumbnail sizes are generated on the fly and cached.)</p> <p>Here’s the code - note that I may have messed up a bit while transforming this into something reusable, but the core bits should all be there. Note that it saves all thumbnails as JPEG, but allows multiple input formats, including JPEG and PNG. This might or might not be OK for you.</p> <pre><code>private static void Thumbnail(Stream source, Stream destination, int maxWidth, int maxHeight) { int width = 0, height = 0; BitmapFrame frame = null; try { frame = BitmapDecoder.Create(source, BitmapCreateOptions.None, BitmapCacheOption.None).Frames[0]; width = frame.PixelWidth; height = frame.PixelHeight; } catch { throw new MyException("The image file is not in any of the supported image formats."); } if (width &gt; AbsoluteLargestUploadWidth || height &gt; AbsoluteLargestUploadHeight) throw new MyException("This image is too large"); try { int targetWidth, targetHeight; ResizeWithAspect(width, height, maxWidth, maxHeight, out targetWidth, out targetHeight); BitmapFrame targetFrame; if (frame.PixelWidth == targetWidth &amp;&amp; frame.PixelHeight == targetHeight) targetFrame = frame; else { var group = new DrawingGroup(); RenderOptions.SetBitmapScalingMode(group, BitmapScalingMode.HighQuality); group.Children.Add(new ImageDrawing(frame, new Rect(0, 0, targetWidth, targetHeight))); var targetVisual = new DrawingVisual(); var targetContext = targetVisual.RenderOpen(); targetContext.DrawDrawing(group); var target = new RenderTargetBitmap(targetWidth, targetHeight, 96, 96, PixelFormats.Default); targetContext.Close(); target.Render(targetVisual); targetFrame = BitmapFrame.Create(target); } var enc = new JpegBitmapEncoder(); enc.Frames.Add(targetFrame); enc.QualityLevel = 80; enc.Save(destination); } catch { throw new MyException("The image file appears to be corrupt."); } } /// &lt;summary&gt;Generic helper to compute width/height that fit into specified maxima while preserving aspect ratio.&lt;/summary&gt; public static void ResizeWithAspect(int origWidth, int origHeight, int maxWidth, int maxHeight, out int sizedWidth, out int sizedHeight) { if (origWidth &lt; maxWidth &amp;&amp; origHeight &lt; maxHeight) { sizedWidth = origWidth; sizedHeight = origHeight; return; } sizedWidth = maxWidth; sizedHeight = (int) ((double) origHeight / origWidth * sizedWidth + 0.5); if (sizedHeight &gt; maxHeight) { sizedHeight = maxHeight; sizedWidth = (int) ((double) origWidth / origHeight * sizedHeight + 0.5); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload