asp.net – 在渲染到位图之前缩放WPF内容
|
背景:
目的: 问题: 我完全不了解传递到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();
} (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net – 将GridView导出到多个Excel工作表
- 在ASP.NET中实现多文件上传的方法
- asp.net – Sys.WebForms.PageRequestManagerServerErrorEx
- 使用可靠的WPF / .NET背景学习ASP.NET MVC
- ASP.NET错误:页面Y.ascx无法使用用户控件X.ascx
- asp.net-mvc – 如何在会话较少的负载平衡环境中配置dotNet
- asp.net – App_Code中的类无法通过Global.asax.cs访问
- MvcBuildViews真实与实体框架在ASP.NET MVC 2
- asp.net – 链接按钮属性在新选项卡中打开?
- 实体框架 – 对ASP.NET MVC Onion架构的意见
- asp.net – 如何在运行时动态地在另一个ASPX的DI
- asp.net-mvc – 在自定义Html帮助器中访问模型对
- asp.net – 检查ValidationGroup是否从代码隐藏有
- ASP.NET中下载文件的几种实例代码
- asp.net-web-api – ASP.NET WEB API 2 OWIN身份
- asp.net-mvc – 如何在ASP.NET MVC中实现分页?
- ASP.NET – 从客户端访问两次网页
- asp.net – MaintainScrollPositionOnPostback属
- asp.net – VB.NET – 如何使用Active Directory
- 使用NUnit与ASP.NET WebApi控制器执行集成测试
