title: Converting from Bitmap to JPEG and retaining original Bitmap resolution
body: <p>I am using the below code to transform a bitmap to jpeg. The bitmap is being passed through at 300 dpi (horizontal/vertical resolution) but the CreateBitmapSourcefromHBitmap method always changes the subsequent jpeg to be saved at 96dpi.</p>
  
  <p>Is there any way to set the source to retain the original 300dpi? The dpiX and dpiY values are read only.</p>
  
  <p>Thanks in advance.</p>
  
  <pre><code>public static MemoryStream GetJpgMemoryStream(Bitmap bitMap, int jpgQuality)
  {
      IntPtr hBitmap = bitMap.GetHbitmap();
  
      try
      {      
  
          BitmapSource source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
  
          var jpegBitmapEncoder = new JpegBitmapEncoder();
  
          jpegBitmapEncoder.QualityLevel = jpgQuality;
          jpegBitmapEncoder.Frames.Add(BitmapFrame.Create(source));
  
          var jpegStream = new MemoryStream();
  
          jpegBitmapEncoder.Save(jpegStream);
  
          jpegStream.Flush();
  
  
          return jpegStream;
  
      }
  }
  </code></pre>