在内存中从(wpf)visual创建Ximage

时间:2012-06-13 09:20:48

标签: wpf vb.net printing pdfsharp

我正在尝试将视觉转换为Ximage(以pdf格式打印) 目前我正在使用它:

 Dim img As XImage = XImage.FromFile(path.LocalPath)

但它需要我将它保存在硬盘上 我想使用它,但我不知道如何从视觉(在内存中)创建图像并加载它。

 Dim img As XImage = XImage.FromGdiPlusImage(myVisualAsImage)

2 个答案:

答案 0 :(得分:1)

如果您使用PDFsharp的WPF版本,您还可以使用:

public static XImage FromBitmapSource(BitmapSource image)

几个类(包括BitmapImage和WriteableBitmap)都是从BitmapSource派生的,也可以在这里传递。

另见: http://ryancdavidson.com/blog/2009/09/getting-and-using-the-pixels-of-your-visual-in-wpf/

答案 1 :(得分:-1)

vb.net - 克服PDFSharp 1.5中遗漏的方法FromGdiPlusImage

Function getTiffImage(sourceImage As Image, pageNumber As Integer) As XImage
    Dim ms As MemoryStream
    Dim returnImage As XImage
    Try
        ms = New MemoryStream()
        Dim objGuid As Guid = sourceImage.FrameDimensionsList(0)
        Dim objDimension As Imaging.FrameDimension = New Imaging.FrameDimension(objGuid)
        sourceImage.SelectActiveFrame(objDimension, pageNumber)
        sourceImage.Save(ms, Imaging.ImageFormat.Tiff)
        returnImage = XImage.FromStream(ms)
    Catch ex As Exception
        returnImage = Nothing
    End Try
    Return returnImage