Convert high DPI images to lower DPI for printing throws OutOfMemoryException Convert high DPI images to lower DPI for printing throws OutOfMemoryException wpf wpf

Convert high DPI images to lower DPI for printing throws OutOfMemoryException


There is no need to do any DPI conversion. Just create a DrawingVisual and draw a BitmapImage into it with an appropriate size:

Dim image As New BitmapImage()image.BeginInit()image.CacheOption = BitmapCacheOption.OnLoadimage.UriSource = New Uri(path)image.EndInit()image.Freeze()Dim size As New Size()If image.Width < printDialog.PrintableAreaWidth Then    size.Width = image.Width    size.Height = image.HeightElse    size.Width = printDialog.PrintableAreaWidth    size.Height = size.Width / image.Width * image.HeightEnd IfDim visual As New DrawingVisual()Using dc As DrawingContext = visual.RenderOpen()    dc.DrawImage(image, New Rect(size))End UsingprintDialog.PrintVisual(visual, "Billede")