加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

wpf – 将高DPI图像转换为较低的DPI以进行打印抛出OutOfMemoryEx

发布时间:2020-12-17 00:11:38 所属栏目:大数据 来源:网络整理
导读:我有一些想要打印的图像.这些图像可以有不同的格式,从不同的DPI到不同的格式(JPEG,PNG等) 现在,我现在所做的是将图像加载到我的应用程序中并尝试 转换dpi说96.但是在这个过程中我得到一个OutOfMemoryException,我不知道如何继续. Private Sub PrintImage(Opt
我有一些想要打印的图像.这些图像可以有不同的格式,从不同的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,bitmap.Width,bitmap.Height),System.Drawing.Imaging.ImageLockMode.[ReadOnly],bitmap.PixelFormat)
        Dim bmSource = BitmapSource.Create(
            bitmapData.Width,bitmapData.Height,96,PixelFormats.Bgr24,Nothing,bitmapData.Scan0,bitmapData.Stride * bitmapData.Height,bitmapData.Stride)
            bitmap.UnlockBits(bitmapData)
        Return bmSource
    End Using
End Function
无需进行任何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")

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读