- 浏览: 399715 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (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 1976first let’s see an example ... -
WPF – Virtualization – VirutalizationStackPanel and ItemsPanelTemplate
2013-08-05 21:55 1410Topic: WPF – Virtualization – ... -
wpf - BehaviorBase and one use examples
2013-06-18 18:41 1311Behavior is something that we ... -
WPF - Setting foreground color of Entire window
2013-06-13 16:00 1918You might as well as I would s ... -
WPF - Enhanced TabControl - TabControlEx aka Prerendering TabControl
2013-06-13 13:12 5330As an opening word, let's che ... -
wpf - ControlTemplate and AddLogicChild/RemoveLogicalChild
2013-06-10 15:42 1185Recently I was trying to debug ... -
wpf - default implicit style
2013-05-10 10:24 794We know that if you left out ... -
wpf - Style setter on the attached property
2013-05-08 14:54 2851I believe that you are familia ... -
wpf - specify enum values in xaml
2013-05-08 11:31 3585Many a situation you find tha ... -
wpf - IG xamDataGrid bind to XmlDataProvider with Xml Island
2012-12-18 14:28 1286Sometimes you may bind to some ... -
wpf - translate winform button/mouse event to wpf events
2012-12-12 17:37 2162It 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 1458The System.WIndows.Controls.Gri ... -
c# - Convert from System.Drawing.Image to System.WIndows.Media.ImageSource
2012-09-25 14:27 7417In Previous discussion, we have ... -
wpf - Get Effective DependencyProperty value on a DependencyObject
2012-08-28 19:05 1044As discussed in my previous pos ... -
wpf - Default(Theme) style and its DefaultStyleKey
2012-08-28 17:54 1387As dicsused in the subsection o ... -
wpf - Dependency Property Value Precedence
2012-08-28 18:56 882A dependency property to an Dep ... -
wpf - WPF TemplateBinding vs RelativeSource TemplatedParent
2012-08-28 14:20 3713This is a post that summarizes ... -
wpf - ICutomTypeDescriptor , PropertyDescriptor and its use in PropertyGrid
2012-08-28 14:04 3580The type ICustomTypeDe ... -
wpf - tips to convert UI controls in WPF/Silverlight/Winforms into a Bitmap
2012-08-27 17:44 975In previous discussion, we have ...
相关推荐
在Windows Presentation Foundation (WPF) 中,Image控件是用于显示图像的标准组件,通常用于加载JPEG、PNG等常见格式的图片。然而,SVG(Scalable Vector Graphics)是一种基于XML的矢量图形格式,它提供了高质量的...
WPF中的ImageSource详解,给WPF中自动生成代码的兄弟们看看
在Winform中,我们通常使用`System.Drawing.Bitmap`类来处理图像,而在WPF中,图像数据则被表示为`System.Windows.Media.ImageSource`。当你需要在WPF应用中显示一个在Winform中创建或处理过的`Bitmap`对象时,就...
在Windows Presentation Foundation (WPF)中,Image控件用于显示图像。然而,WPF的Image控件默认并不直接支持点击事件,这使得开发者在需要对图片进行交互时可能会感到困扰。本文将详细介绍如何定义和实现WPF Image...
在Windows Presentation Foundation(WPF)开发中,UI元素的更新必须在主线程中进行,因为它们不是线程安全的。当需要动态改变如`Image`控件的背景图片时,如果尝试在后台线程中执行,会抛出“Cross-thread ...
输入一个网址,通过WPF使用WebBrowser控件定时访问刷新网页
在Windows Presentation Foundation (WPF) 中,`Image` 控件是用于显示图像的基本元素。而`OpacityMask` 是一个强大的特性,它允许我们为UI元素指定一个透明度掩码,以此来控制元素的不同部分的透明度。在这个示例中...
在Windows Presentation Foundation (WPF)应用中,WebBrowser控件是一个强大的工具,它允许开发者将网页内容嵌入到桌面应用程序中。本示例探讨的是如何利用这个控件实现Web页面与WPF应用之间的交互,特别是从...
WPF 中 Image 控件 Source 属性的相对路径和绝对路径问题总结 在 WPF 中,Image 控件的 Source 属性可以指定为相对路径或绝对路径,但这两种路径有不同的使用场景和注意事项。本文将总结 WPF 中 Image 控件 Source ...
"WPF_Image_Pan_and_Zoom"这一主题聚焦于如何在WPF应用中实现图像的平移和缩放功能,这对于创建交互式的图像查看器或地图应用程序至关重要。以下将详细讲解实现这一功能的关键技术和步骤。 首先,我们需要了解WPF中...
在Windows Presentation Foundation (WPF) 中,SVG(Scalable Vector Graphics)是一种常见的矢量图形格式,它允许创建高质量、可缩放的图形。在WPF应用中直接使用SVG图像可能会遇到一些挑战,因为SVG不是WPF原生...
本篇文章将详细讲解如何在WPF中实现Image对象与Base64String之间的转换,以及相关的知识点。 首先,了解Base64编码。Base64是一种用于把任意二进制数据编码为ASCII字符串的方法,以便在不能直接传输二进制数据的...
### WPF解决透明窗体与WebBrowser不兼容问题 #### 背景介绍 在Windows Presentation Foundation (WPF) 应用程序开发过程中,开发者可能会遇到一个常见的问题:当WPF窗口设置为透明时(例如,通过使用 `WindowStyle=...
在探讨如何在WPF(Windows Presentation Foundation)应用中打开并显示图片到`Image`控件的过程中,我们将深入解析代码逻辑、关键类库的使用以及一些最佳实践,这将对WPF初学者尤其有益。 ### WPF与图片处理 WPF...
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...
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,...
using (FileStream fs = new FileStream("modified_image.jpg", FileMode.Create)) { wb.SaveJpeg(fs, wb.PixelWidth, wb.PixelHeight, 0, 100); } ``` 在提供的压缩包文件中,"WallmadeJexawoPejakairkas.sln"很...
在Windows Presentation Foundation (WPF) 中,WebBrowser控件是一个非常有用的组件,它允许开发者在应用程序中嵌入网页浏览功能。MDI (Multiple Document Interface) 是一种用户界面设计模式,常用于创建支持多个...
"Wpf_WebBrowser.7z"这个压缩包文件很可能包含了一个使用WPF技术实现的Web浏览器项目。下面我们将深入探讨与这个主题相关的知识点。 1. **WPF WebBrowser控件**: - WPF中的`WebBrowser`控件允许开发者在应用程序...
**WPF中的WebBrowser控件** 在Windows Presentation Foundation (WPF)中,WebBrowser控件是一个非常有用的组件,它允许开发者在WPF应用程序内嵌入一个网页浏览器的功能。这个控件使得用户可以在应用中直接浏览网页...