Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This can be solved through an <a href="http://msdn.microsoft.com/en-us/library/bb383977.aspx" rel="nofollow" title="extension method">extension method</a>:</p> <pre><code>using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Windows.Media.Imaging; /// &lt;remarks&gt; /// Requires reference to &lt;c&gt;System.Drawing&lt;/c&gt;, &lt;c&gt;PresentationCore&lt;/c&gt; and &lt;c&gt;WindowsBase&lt;/c&gt;. /// &lt;/remarks&gt; public static class JpegXr { public static MemoryStream SaveJpegXr(this Bitmap bitmap, float quality) { var stream = new MemoryStream(); SaveJpegXr(bitmap, quality, stream); stream.Seek(0, SeekOrigin.Begin); return stream; } public static void SaveJpegXr(this Bitmap bitmap, float quality, Stream output) { var bitmapSource = bitmap.ToWpfBitmap(); var bitmapFrame = BitmapFrame.Create(bitmapSource); var jpegXrEncoder = new WmpBitmapEncoder(); jpegXrEncoder.Frames.Add(bitmapFrame); jpegXrEncoder.ImageQualityLevel = quality / 100f; jpegXrEncoder.Save(output); } /// &lt;seealso cref="http://stackoverflow.com/questions/94456/load-a-wpf-bitmapimage-from-a-system-drawing-bitmap"/&gt; public static BitmapSource ToWpfBitmap(this Bitmap bitmap) { using (var stream = new MemoryStream()) { bitmap.Save(stream, ImageFormat.Bmp); stream.Position = 0; var result = new BitmapImage(); result.BeginInit(); // According to MSDN, "The default OnDemand cache option retains access to the stream until the image is needed." // Force the bitmap to load right now so we can dispose the stream. result.CacheOption = BitmapCacheOption.OnLoad; result.StreamSource = stream; result.EndInit(); result.Freeze(); return result; } } } </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. 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