将高DPI图像转换为较低的DPI以进行打印会抛出OutOfMemoryException

时间:2017-08-25 10:58:57

标签: wpf vb.net image printing dpi

我有一些想要打印的图片。这些图像可以有不同的格式,从不同的DPI到不同的格式(JPEG,PNG等)

现在我所做的就是将图像加载到我的应用程序中并尝试  转换dpi说96.但是在这个过程中我得到一个OutOfMemoryException,我不知道如何继续。

Private Sub PrintImage(Optional providedPrintDialog As PrintDialog = Nothing)
    Dim objPrintDialog As PrintDialog
    If providedPrintDialog IsNot Nothing Then
        objPrintDialog = providedPrintDialog
    Else
        objPrintDialog = New PrintDialog()
    End If
    Dim myPanel As New StackPanel
    myPanel.Margin = New Thickness(15)
    Dim myImage As New Controls.Image
    Dim tempBitmapImage = ConvertBitmapToXDPI(Me.SelectedFileViewModel.File.GetPath, 96)
    Dim tempBitmapImageWidth As Integer = CInt(objPrintDialog.PrintableAreaWidth)
    ' A4 max width = 793
    If tempBitmapImage.Width > tempBitmapImageWidth Then
        myImage.Stretch = System.Windows.Media.Stretch.Uniform
    Else
        myImage.Stretch = System.Windows.Media.Stretch.None
    End If
    myImage.Source = tempBitmapImage
    myPanel.Children.Add(myImage)
    myPanel.Measure(New System.Windows.Size(objPrintDialog.PrintableAreaWidth, objPrintDialog.PrintableAreaHeight))
    myPanel.Arrange(New Rect(New System.Windows.Point(0, 0), myPanel.DesiredSize))
    objPrintDialog.PrintVisual(myPanel, "Billede") ' <- OutOfMemoryException thrown here
End Sub

Private Function ConvertBitmapToXDPI(path As String, newDpi As Integer) As BitmapSource
    Using bitmap As Bitmap = DirectCast(System.Drawing.Image.FromFile(path), Bitmap)
        Dim bitmapData = bitmap.LockBits(New System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), System.Drawing.Imaging.ImageLockMode.[ReadOnly], bitmap.PixelFormat)
        Dim bmSource = BitmapSource.Create(
            bitmapData.Width,
            bitmapData.Height, 96, 96, PixelFormats.Bgr24, Nothing,
            bitmapData.Scan0,
            bitmapData.Stride * bitmapData.Height,
            bitmapData.Stride)
            bitmap.UnlockBits(bitmapData)
        Return bmSource
    End Using
End Function

1 个答案:

答案 0 :(得分:4)

无需进行任何DPI转换。只需创建一个DrawingVisual并使用适当的大小绘制BitmapImage:

Dim image As New BitmapImage()
image.BeginInit()
image.CacheOption = BitmapCacheOption.OnLoad
image.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.Height
Else
    size.Width = printDialog.PrintableAreaWidth
    size.Height = size.Width / image.Width * image.Height
End If

Dim visual As New DrawingVisual()

Using dc As DrawingContext = visual.RenderOpen()
    dc.DrawImage(image, New Rect(size))
End Using

printDialog.PrintVisual(visual, "Billede")