Tag: rendertargetbitmap

RenderTargetBitmap内存泄漏

我试图使用RenderTargetBitmap渲染图像每次我从RenderTargetBitmap创建一个实例来渲染图像时内存增加,当完成时内存永远不会释放,这就是代码: RenderTargetBitmap rtb = new RenderTargetBitmap((int)(renderWidth * dpiX / 96.0), (int)(renderHeight * dpiY / 96.0), dpiX, dpiY, PixelFormats.Pbgra32); DrawingVisual dv = new DrawingVisual(); using (DrawingContext ctx = dv.RenderOpen()) { VisualBrush vb = new VisualBrush(target); ctx.DrawRectangle(vb, null, new System.Windows.Rect(new Point(0, 0), new Point(bounds.Width, bounds.Height))); } rtb.Render(dv); 请我帮忙如何释放内存并感谢所有人。

如何冻结无法冻结的freezable对象

在我的场景中,我想在我想在后台任务中渲染它之前冻结一个不变的BitmapCacheBrush。 不幸的是我收到错误“这个Freezable不能被冻结”。 是否有任何解决方法或hacky方式冻结也不是freezable对象? 也许可以通过reflection设置正确的属性来达到这个目标? 提前谢谢你们。 编辑:(我的示例代码请求) public static class ext { public static async Task RenderAsync(this Visual visual) { var bounds = VisualTreeHelper.GetDescendantBounds(visual); var bitmapCacheBrush = new BitmapCacheBrush(visual); bitmapCacheBrush.BitmapCache = new BitmapCache(); // We need to disconnect the visual here to make the freezable freezable :). Of course this will make our rendering blank //bitmapCacheBrush.Target […]

在Windows Phone 8.1中将IBuffer转换为字节数组,怎么做?

我正在为Windows Phone 8.1编写一个应用程序。 我需要将UIElement保存为图像文件(我更喜欢JPG或PNG)。 我正在使用RenderTargetBitmap类来执行此操作。 在我的UIElement上调用RenderAsync方法之后,我创建了一个IBuffer,它包含RenderTargetBitmap方法GetPixelsAsync()的结果。 现在我需要调用方法ToArray()来转换我的IBuffer在一个字节数组中使用类似BitmapEncoder保存我的图像,但似乎在Windows Phone 8.1上没有任何ToArray()方法用于IBuffer,而在Windows上8.1存在。 我该如何解决这个问题? 谢谢!

在代码而不是XAML中呈现UserControl

我想使用RenderTargetBitmap将UserControl呈现为位图,而不必为其编写XAML。 当我这样做时,我得到一张空白图片,我错过了关键一步吗? ValTool.Controls.VideoFisheyeOverlayControl vfoc = new Controls.VideoFisheyeOverlayControl(); vfoc.Width = (int)this.VideoContainer.ActualWidth; vfoc.Height = (int)this.VideoContainer.ActualHeight; vfoc.FieldsOfView=this.FieldsOfView; vfoc.CountLines = this.CountLines; vfoc.UpdateLayout(); vfoc.InvalidateVisual(); RenderTargetBitmap visual = new RenderTargetBitmap((int)this.VideoContainer.ActualWidth, (int)this.VideoContainer.ActualHeight, 96, 96, PixelFormats.Pbgra32); visual.Render(vfoc); var finalImage = BitmapFrame.Create(visual); // Encoding the RenderBitmapTarget as a PNG file. PngBitmapEncoder png = new PngBitmapEncoder(); png.Frames.Add(BitmapFrame.Create(finalImage)); using (Stream stm = File.Create(@”new.png”)) { png.Save(stm); }

将canvas保存到png C#wpf

所以我试图在WPF C#中拍摄我的canvas快照,以便我可以将其保存为png。 目前图像保存不正确,因为它包括左边距和上边距。 这就是我所拥有的: 为canvas大小创建一个矩形。 如果canvas.Margin.Left和Top设置为0,则保存的图像大小正确但仍然会发生偏移,从而切割底边和右边。 设置Margin.Left和Top仍会导致偏移发生,但整个图像被保存但尺寸错误(margin.Left + ActualWidth)而不仅仅是ActualWidth Rect rect = new Rect(canvas.Margin.Left, canvas.Margin.Top, canvas.ActualWidth, canvas.ActualHeight); double dpi = 96d; RenderTargetBitmap rtb = new RenderTargetBitmap((int)rect.Right, (int)rect.Bottom, dpi, dpi, System.Windows.Media.PixelFormats.Default); rtb.Render(canvas); BitmapEncoder pngEncoder = new PngBitmapEncoder(); pngEncoder.Frames.Add(BitmapFrame.Create(rtb)); try { System.IO.MemoryStream ms = new System.IO.MemoryStream(); pngEncoder.Save(ms); ms.Close(); System.IO.File.WriteAllBytes(filename, ms.ToArray()); } catch (Exception err) { MessageBox.Show(err.ToString(), “Error”, […]

RenderTargetBitmap和Viewport3D – 质量问题

我想将一个3D场景从Viewport3D导出到位图。 显而易见的方法是使用RenderTargetBitmap – 但是当我这样做时,导出的位图的质量明显低于屏幕上的图像。 环顾四周,似乎RenderTargetBitmap没有利用硬件渲染。 这意味着渲染在第0层完成。 这意味着没有mip-mapping等,因此降低了导出图像的质量。 有谁知道如何以屏幕质量导出Viewport3D的位图? 澄清 虽然下面给出的示例没有显示这一点,但我最终需要将Viewport3D的位图导出到文件中。 据我所知,唯一的方法是将图像转换为从BitmapSource派生的东西。 下面的Cplotts显示使用RenderTargetBitmap提高导出质量可以改善图像,但由于渲染仍然在软件中完成,因此速度极慢。 有没有办法使用硬件渲染将渲染的3D场景导出到文件? 当然应该可以吗? 你可以看到这个xaml的问题: RenderTargetBitmap! 这段代码: private void Button_Click(object sender, RoutedEventArgs e) { RenderTargetBitmap bmp = new RenderTargetBitmap((int)viewport3D.ActualWidth, (int)viewport3D.ActualHeight, 96, 96, PixelFormats.Default); bmp.Render(viewport3D); rtbImage.Source = bmp; viewport3D.Visibility = Visibility.Collapsed; rtbImage.Visibility = Visibility.Visible; }

如何将XPS文件转换为高质量的图像(而不是模糊的低分辨率)?

我正在尝试使用WPF转换XPS。 想法是这些图像可以加载silverlight 4,为此我使用以下代码: // XPS Document XpsDocument xpsDoc = new XpsDocument(xpsFileName, System.IO.FileAccess.Read); FixedDocumentSequence docSeq = xpsDoc.GetFixedDocumentSequence(); // The number of pages PageCount = docSeq.References[0].GetDocument(false).Pages.Count; DocumentPage sizePage = docSeq.DocumentPaginator.GetPage(0); PageHeight = sizePage.Size.Height; PageWidth = sizePage.Size.Width; // Scale dimensions from 96 dpi to 600 dpi. double scale = 300/ 96; // Convert a XPS page to a […]

将RenderTargetBitmap转换为BitmapImage

我有一个RenderTargetBitmap ,我需要将其转换为BitmapImage 。 请检查以下代码。 RenderTargetBitmap bitMap = getRenderTargetBitmap(); Image image = new Image();// This is a Image image.Source = bitMap; 在上面的代码中我使用了Image.Now我需要使用BitmapImage。 我怎样才能做到这一点? RenderTargetBitmap bitMap = getRenderTargetBitmap(); BitmapImage image = new BitmapImage();// This is a BitmapImage // how to set bitMap as source of BitmapImage ?

如何在UWP Windows 10应用程序中将InkCanvas渲染为图像?

RenderTargetBitmap类使用简单的Canvas + InkManager(在Windows 8.1中)将墨迹笔划渲染到图像。 UWP推出了InkCanvas和一个新的Inking API。 但是,似乎RenderTargetBitmap无法使用它。 当我尝试使用RenderAsync方法捕获墨迹笔划时,没有墨迹笔划只会呈现其他对象,如Rectangle等。 这是一个错误还是这个新的API不是以这种方式使用的? 如果没有,那么如何从InkCanvas渲染图像? 谢谢!