- 浏览: 389633 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (309)
- xaml C# wpf (0)
- scala java inner clas (1)
- Tools UML Eclipse UML2 (1)
- Timer .NET Framework (1)
- perl (6)
- python function paramter (1)
- Python Docstring (1)
- Python how to compare types (1)
- Python (8)
- java (5)
- C# (76)
- C# WPF (0)
- p4 (0)
- WPF (46)
- .net (6)
- xaml (1)
- javascript (40)
- windows (10)
- scala (4)
- winform (1)
- c++ (48)
- tools (12)
- cmd (1)
- os (0)
- CI (0)
- shell (0)
- C (2)
- haskell (49)
- functional (1)
- tool (1)
- gnu (1)
- linux (1)
- kaskell (0)
- svn (0)
- wcf (3)
- android (1)
最新评论
WPF has a class that is called System.Windows.Media.DrawingVisual where you can pass a System.Windows.Controls.Visual to it and it is supposed draw/render the visual as it is on the screen to some place in the memory.
generaly you can have the following code to create a drawing visual .
public static DrawingVisual CreateDrawingVisual(FrameworkElement visual, double width, double height) { var drawingVisual = new DrawingVisual(); // open the Render of the DrawingVisual using (var dc = drawingVisual.RenderOpen()) { var vb = new VisualBrush(visual) { Stretch = Stretch.None }; var rectangle = new Rect { X = 0, Y = 0, Width = width, Height = height, }; // draw the white background dc.DrawRectangle(Brushes.White, null, rectangle); // draw the visual dc.DrawRectangle(vb, null, rectangle); } return drawingVisual; }
With the code above, you are create a drawing visual with the same size as the size of the element that you passed in. Also, it create some background for the drawing visual.
This all works well except that it does not work for the type WebBrowser. It does not fail, but you won't see anything if try to display the drawing visual.
There is a post on the web telling that "Save as Image using DrawingImage() in WPF". Basically the code that the author proposed is like this;
/* using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; using System.IO; */ // Creates thumbnail image form the webbrowser control in wpf // Or Thumbnail of web pages image created can be easily loaded in any // image control.. public static string GetThumbnailImage(WebBrowser CurrentBrowser) { Guid guid = Guid.NewGuid(); string ThumbnailPath = @"E:\" + guid.ToString() + ".png"; Image imgScreen = new Image(); imgScreen.Width = 120; imgScreen.Height = 100; imgScreen.Source = new DrawingImage(VisualTreeHelper.GetDrawing(CurrentBrowser)); FileStream stream = new FileStream(ThumbnailPath, FileMode.Create); DrawingVisual vis = new DrawingVisual(); DrawingContext cont = vis.RenderOpen(); cont.DrawImage(imgScreen.Source, new Rect(new Size(120d, 100d))); cont.Close(); RenderTargetBitmap rtb = new RenderTargetBitmap((int)imgScreen.Width, (int)imgScreen.Height, 96d, 96d, PixelFormats.Default); rtb.Render(vis); PngBitmapEncoder encoder = new PngBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(rtb)); encoder.Save(stream); stream.Close(); return ThumbnailPath; }
While If you adop the code above and you will probably do the following
public static DrawingVisual CreateDrawingImage(Visual visual, double width, double height) { var drawingVisual = new DrawingVisual(); // open the Render of the DrawingVisual using (var dc = drawingVisual.RenderOpen()) { var drawingImage = new DrawingImage(VisualTreeHelper.GetDrawing(visual)); var rectangle = new Rect { X = 0, Y = 0, Width = width, Height = height, }; // draw the white background dc.DrawRectangle(Brushes.White, null, rectangle); // draw the visual //NOTE: // instead of Creating one VisualBrush, it create and use one DrawingImage // and use that DrawingImage , because VisualBrush has issue with WebBrowser // dc.DrawImage(drawingImage, rectangle); } return drawingVisual; }
发表评论
-
wpf - example to enhance ComboBox for AutoComplete
2014-09-19 15:56 1941first let’s see an example ... -
WPF – Virtualization – VirutalizationStackPanel and ItemsPanelTemplate
2013-08-05 21:55 1361Topic: WPF – Virtualization – ... -
wpf - BehaviorBase and one use examples
2013-06-18 18:41 1279Behavior is something that we ... -
WPF - Setting foreground color of Entire window
2013-06-13 16:00 1880You might as well as I would s ... -
WPF - Enhanced TabControl - TabControlEx aka Prerendering TabControl
2013-06-13 13:12 5294As an opening word, let's che ... -
wpf - ControlTemplate and AddLogicChild/RemoveLogicalChild
2013-06-10 15:42 1145Recently I was trying to debug ... -
wpf - default implicit style
2013-05-10 10:24 770We know that if you left out ... -
wpf - Style setter on the attached property
2013-05-08 14:54 2821I believe that you are familia ... -
wpf - specify enum values in xaml
2013-05-08 11:31 3544Many a situation you find tha ... -
wpf - IG xamDataGrid bind to XmlDataProvider with Xml Island
2012-12-18 14:28 1253Sometimes you may bind to some ... -
wpf - translate winform button/mouse event to wpf events
2012-12-12 17:37 2129It is common that we sometimes ... -
wpf - Freezable and its meaning
2012-09-27 12:38 0This article is based on the di ... -
wpf - Customize the grid lines for original wpf Grid control
2012-09-27 12:01 1428The System.WIndows.Controls.Gri ... -
c# - Convert from System.Drawing.Image to System.WIndows.Media.ImageSource
2012-09-25 14:27 7369In Previous discussion, we have ... -
wpf - Get Effective DependencyProperty value on a DependencyObject
2012-08-28 19:05 1011As discussed in my previous pos ... -
wpf - Default(Theme) style and its DefaultStyleKey
2012-08-28 17:54 1361As dicsused in the subsection o ... -
wpf - Dependency Property Value Precedence
2012-08-28 18:56 835A dependency property to an Dep ... -
wpf - WPF TemplateBinding vs RelativeSource TemplatedParent
2012-08-28 14:20 3655This is a post that summarizes ... -
wpf - ICutomTypeDescriptor , PropertyDescriptor and its use in PropertyGrid
2012-08-28 14:04 3545The type ICustomTypeDe ... -
wpf - tips to convert UI controls in WPF/Silverlight/Winforms into a Bitmap
2012-08-27 17:44 936In previous discussion, we have ...
相关推荐
输入一个网址,通过WPF使用WebBrowser控件定时访问刷新网页
WPF中的ImageSource详解,给WPF中自动生成代码的兄弟们看看
WPF 控件 Image 点击事件 1 定义事件 2 为目标控件附加一个模拟的鼠标单击事件
wpf UI元素不能在线程中操控,现在问题解决了,很好用的呦!
wpf Image控件 加载SVG图片使用了SharpVectors.Converters.Wpf.DLL 进行二次重绘然后重新转换成ImageSource
Demo中描述了如何让 WPF 窗体中的 WebBrowser 打开指定页面,并且页面中通过脚本调用托管的代码,也就是调用C#的方法。
WPF中Image控件Source属性的相对路径和绝对路径问题的开发经验总结,亲测通过!
WPF Image OpacityMask示例
However, the knowledge of how to do this is missing from a large part of the development community―even amongst those who work with WPF and Silverlight on a daily basis. Too often there is a reliance...
wpf WebBrowser在mdi使用
异步使用WMI来增强ListView C# .NET And The WPF Listview - Using WMI in Async mode to power the Listview.pdf
Designers can now model UI using Expression Blend, save the design as a XAML file, and simply hand the file off to a developer to code against in Visual Studio using WPF. This book covers the ...
Using SignalR in WinForms and WPF.这是推送不使用IIS的示例,同时使用winform或是WPF实现推送与接收。对使用WinForms 来推送和接收有参考价值
我见过的最好的鼠标滚动放大缩小,和平移功能。不止用于Image,带背景图的空间都能使用
Eliminate unnecessary code by taking advantage of the MVVM pattern in Silverlight and WPF using this book and eBook - less code, fewer bugs Build an enterprise application using Silverlight and WPF,...
This book was conceived from a need to explain the MVVM pattern and how it helps structure WPF and Silverlight applications. I had worked on a number of projects where these technologies were used ...
WPF AllowsTransparency="True" 使用webbrowser控件,可扩展到使用别的控件。
This book was conceived from a need to explain the MVVM pattern and how it helps structure WPF and Silverlight applications. I had worked on a number of projects where these technologies were used but...
简单的浏览图片并放入image中。。新手合适~
Having helped alot of people on the MSDN Forum in the WPF section, I tend to see alot of people who fail to exploit the benefits of DataBinding in WPF, and not enough people are aware of the benefits ...