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

asp.net – 在渲染到位图之前缩放WPF内容

发布时间:2020-12-15 20:39:13 所属栏目:asp.Net 来源:网络整理
导读:背景: 我正在使用一个silverlight(1.0)应用程序,动态构建美国的地图,图标和文字覆盖在特定的位置.该地图在浏览器中工作得很好,现在我需要获得静态(可打印并插入到文档/ powerpoint中)显示的地图副本. 目的: 为了获得可以在powerpoint幻灯片,单词等中使用的
背景:
我正在使用一个silverlight(1.0)应用程序,动态构建美国的地图,图标和文字覆盖在特定的位置.该地图在浏览器中工作得很好,现在我需要获得静态(可打印并插入到文档/ powerpoint中)显示的地图副本.

目的:
为了获得可以在powerpoint幻灯片,单词等中使用的地图的可打印副本,我选择创建一个ASP.NET HttpHandler,以在WPF的服务器端重新创建xaml,然后将WPF渲染为作为png文件返回的位图图像,以300dpi生成,以获得更好的打印质量.

问题:
这有一个很好的一个问题,我无法使图像缩放到指定的大小.我尝试了几个不同的东西,其中一些可以在注释的行中看到.我需要能够以英寸或像素指定图像的高度和宽度,我不一定关心哪些,并且为生成的位图使xaml缩放到该大小.目前,如果我使尺寸大于根画布,画布将以生成的图像的左上角以指定的大小呈现原始大小.以下是我的httphandler的重要组成部分.存储为“MyImage”的根画布的高度为600,宽度为800.我缺少获取内容以适应指定大小的内容?

我完全不了解传递到Arrange()和Measure()中的维度,因为这些代码中的一些代码是从在线示例中获取的.我也不完全明白RenderTargetBitmap的东西.任何指导将不胜感激.

Public Sub Capture(ByVal MyImage As Canvas)
    ' Determine the constraining scale to maintain the aspect ratio and the bounds of the image size
    Dim scale As Double = Math.Min(Width / MyImage.Width,Height / MyImage.Height)

    'Dim vbox As New Viewbox()
    'vbox.Stretch = Stretch.Uniform
    'vbox.StretchDirection = StretchDirection.Both
    'vbox.Height = Height * scale * 300 / 96.0
    'vbox.Width = Width * scale * 300 / 96.0
    'vbox.Child = MyImage

    Dim bounds As Rect = New Rect(0,MyImage.Width * scale,MyImage.Height * scale)
    MyImage.Measure(New Size(Width * scale,Height * scale))
    MyImage.Arrange(bounds)
    'MyImage.UpdateLayout()

    ' Create the target bitmap
    Dim rtb As RenderTargetBitmap = New RenderTargetBitmap(CInt(Width * scale * 300 / 96.0),CInt(Height * scale * 300 / 96.0),300,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)
        'Dim vb As New VisualBrush(vbox)
        ctx.DrawRectangle(vb,Nothing,New Rect(New System.Windows.Point(),bounds.Size))
    End Using
    rtb.Render(dv)

    ' Encode the image in the format selected
    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()
    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

解决方法

这应该足以让你开始:
private void ExportCanvas(int width,int height)
{
    string path = @"c:tempTest.tif";
    FileStream fs = new FileStream(path,FileMode.Create);


    RenderTargetBitmap renderBitmap = new RenderTargetBitmap(width,height,1/300,PixelFormats.Pbgra32);

    DrawingVisual visual = new DrawingVisual();
    using (DrawingContext context = visual.RenderOpen())
    {
        VisualBrush brush = new VisualBrush(MyCanvas);
        context.DrawRectangle(brush,null,new Rect(new Point(),new Size(MyCanvas.Width,MyCanvas.Height)));
    }

    visual.Transform = new ScaleTransform(width / MyCanvas.ActualWidth,height / MyCanvas.ActualHeight);

    renderBitmap.Render(visual);

    BitmapEncoder encoder = new TiffBitmapEncoder();
    encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
    encoder.Save(fs);
    fs.Close();
}

(编辑:李大同)

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

    推荐文章
      热点阅读