Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is what ended up working for me:</p> <pre><code>Public Sub Capture(ByVal MyImage As Canvas) ' Normally we would obtain a user's configured DPI setting to account for the possibilty of a high DPI setting. ' However, this code is running server side so the client's DPI is not obtainable. Const SCREEN_DPI As Double = 96.0 ' Screen DPI Const TARGET_DPI As Double = 300.0 ' Print Quality DPI ' Determine the constraining scale to maintain the aspect ratio and the bounds of the image size Dim scale As Double = Math.Min(Width * SCREEN_DPI / MyImage.Width, Height * SCREEN_DPI / MyImage.Height) ' Setup the bounds of the image Dim bounds As Rect = New Rect(0, 0, MyImage.Width * scale, MyImage.Height * scale) MyImage.Measure(New Size(MyImage.Width * scale, MyImage.Height * scale)) MyImage.Arrange(bounds) ' Create the target bitmap Dim rtb As RenderTargetBitmap = New RenderTargetBitmap(CDbl(MyImage.Width * scale / SCREEN_DPI * TARGET_DPI), CDbl(MyImage.Height * scale / SCREEN_DPI * TARGET_DPI), TARGET_DPI, TARGET_DPI, PixelFormats.Pbgra32) ' Render the image to the target bitmap Dim dv As DrawingVisual = New DrawingVisual() Using ctx As DrawingContext = dv.RenderOpen() Dim vb As New VisualBrush(MyImage) ctx.DrawRectangle(vb, Nothing, New Rect(New System.Windows.Point(), bounds.Size)) End Using ' Transform the visual to scale the image to our desired size. 'dv.Transform = New ScaleTransform(scale, scale) ' Render the visual to the bitmap. rtb.Render(dv) ' Encode the image in the format selected. If no valid format was selected, default to png. Dim encoder As System.Windows.Media.Imaging.BitmapEncoder Select Case Encoding.ToLower Case "jpg" encoder = New System.Windows.Media.Imaging.JpegBitmapEncoder() Case "png" encoder = New System.Windows.Media.Imaging.PngBitmapEncoder() Case "gif" encoder = New System.Windows.Media.Imaging.GifBitmapEncoder() Case "bmp" encoder = New System.Windows.Media.Imaging.BmpBitmapEncoder() Case "tif" encoder = New System.Windows.Media.Imaging.TiffBitmapEncoder() Case "wmp" encoder = New System.Windows.Media.Imaging.WmpBitmapEncoder() Case Else encoder = New System.Windows.Media.Imaging.PngBitmapEncoder() End Select encoder.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(rtb)) ' Create the memory stream to save the encoded image. retImageStream = New System.IO.MemoryStream() encoder.Save(retImageStream) retImageStream.Flush() retImageStream.Seek(0, System.IO.SeekOrigin.Begin) MyImage = Nothing End Sub </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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.
    1. 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