- 浏览: 401178 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (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)
最新评论
In previous discussion, we have covered the following topics
Convert from System.Drawing.Icon to System.Media.ImageSource and vice versa
wpf - how to create and save any visual element into a file
wpf Save a image using DrawingImage() and workaround the WebBrowser drawing issue
wpf - Draw Windows Forms Controls
c# - Convert from System.Drawing.Image to System.WIndows.Media.ImageSource
This topic is adding more routines/examples to the GDI drawing, control and Image conversions; This topic is more about converting some controls (wpf/silverlight/winforms) to Bitmap (or sources to Bitmap)
the original post is located here:
How to convert a UI Control in WPF/Silverlight/WinForms into a Bitmap;
ometimes you just need to convert a FrameworkElement/Control into a bitmap even though the control doesn't necessarily exist in the UI Tree. Usually people find themselves in this situation because the 2D Graphics Drawing library for WPF/Silverlight is completely non-existent and you just need a cheap way to draw a circle or text.
WPF
If the element was created programmatically and doesn't exist in the UI tree, you need to force it to update its layout using Measure/Arrange.
// using System, System.Windows, System.Windows.Media, System.Windows.Media.Imaging public BitmapSource CreateBitmap(FrameworkElement element, bool isInUiTree) { if (!isInUiTree) { element.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity)); element.Arrange(new Rect(new Point(0, 0), frameworkElement.DesiredSize)); } int width = (int)Math.Ceiling(element.ActualWidth); int height = (int)Math.Ceiling(element.ActualHeight); width = width == 0 ? 1 : width; height = height == 0 ? 1 : height; RenderTargetBitmap rtbmp = new RenderTargetBitmap( width, height, 96, 96, PixelFormats.Default); rtbmp.Render(element); return rtbmp; }
The Render method of RenderTargetBitmap can take in any Visual, not just a FrameworkElement. Measure and Arrange exist on the UIElement class but the ActualWidth and ActualHeight properties only exist on FrameworkElement, so you need to know what the bounds are if it's a UIElement. If it's just a visual, then you there is no Measure or Arrange. It must already be in the UI Tree in order for it to work.
Silverlight
Silverlight is a bit easier. All you have to do is simply pass the UIElement into one of the constructors of WriteableBitmap. No Measure/Arrange silliness.
// using System, System.Windows, System.Windows.Media.Imaging public BitmapSource CreateBitmap(FrameworkElement element) { int width = (int)Math.Ceiling(element.ActualWidth); int height = (int)Math.Ceiling(element.ActualHeight); width = width == 0 ? 1 : width; height = height == 0 ? 1 : height; return new WriteableBitmap(element, null); }
WinForms
Why are you still using WinForms? Anyways...
// using System, System.Drawing, System.Windows.Forms, public Bitmap CreateBitmap(Control winformsControl) { int width = (int)Math.Ceiling(winformsControl.Width); int height = (int)Math.Ceiling(winformsControl.Height); width = width == 0 ? 1 : width; height = height == 0 ? 1 : height; Bitmap bmp = new Bitmap(width, height); winformsControls.DrawToBitmap(bmp, new Rectangle(0, 0, width, height)); return bmp; }
发表评论
-
wpf - example to enhance ComboBox for AutoComplete
2014-09-19 15:56 1979first let’s see an example ... -
WPF – Virtualization – VirutalizationStackPanel and ItemsPanelTemplate
2013-08-05 21:55 1422Topic: WPF – Virtualization – ... -
wpf - BehaviorBase and one use examples
2013-06-18 18:41 1318Behavior is something that we ... -
WPF - Setting foreground color of Entire window
2013-06-13 16:00 1925You might as well as I would s ... -
WPF - Enhanced TabControl - TabControlEx aka Prerendering TabControl
2013-06-13 13:12 5337As an opening word, let's che ... -
wpf - ControlTemplate and AddLogicChild/RemoveLogicalChild
2013-06-10 15:42 1191Recently I was trying to debug ... -
wpf - default implicit style
2013-05-10 10:24 796We know that if you left out ... -
wpf - Style setter on the attached property
2013-05-08 14:54 2858I believe that you are familia ... -
wpf - specify enum values in xaml
2013-05-08 11:31 3594Many a situation you find tha ... -
wpf - IG xamDataGrid bind to XmlDataProvider with Xml Island
2012-12-18 14:28 1290Sometimes you may bind to some ... -
wpf - translate winform button/mouse event to wpf events
2012-12-12 17:37 2166It 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 1461The System.WIndows.Controls.Gri ... -
c# - Convert from System.Drawing.Image to System.WIndows.Media.ImageSource
2012-09-25 14:27 7421In Previous discussion, we have ... -
wpf - Get Effective DependencyProperty value on a DependencyObject
2012-08-28 19:05 1048As discussed in my previous pos ... -
wpf - Default(Theme) style and its DefaultStyleKey
2012-08-28 17:54 1392As dicsused in the subsection o ... -
wpf - Dependency Property Value Precedence
2012-08-28 18:56 886A dependency property to an Dep ... -
wpf - WPF TemplateBinding vs RelativeSource TemplatedParent
2012-08-28 14:20 3719This is a post that summarizes ... -
wpf - ICutomTypeDescriptor , PropertyDescriptor and its use in PropertyGrid
2012-08-28 14:04 3589The type ICustomTypeDe ... -
wpf - input binding examples
2012-08-27 11:47 1229Input binding is the mechanism ...
相关推荐
WPF是.NET Framework的一部分,是一个用于构建桌面应用的UI框架,它提供了丰富的图形层、数据绑定、样式和模板等特性,使得开发者可以创建出美观且功能强大的应用程序。 流媒体技术则涉及将连续的音频或视频数据在...
在.NET框架中,Windows Presentation Foundation (WPF)和Windows Forms (WinForms) 是两种不同的UI技术,但它们可以互相交互。本篇文章将详细介绍如何利用WPF调用WinForms类库来实现屏幕截图功能,包括全屏截取、...
CodedUI使用WPF或WinForms的控件模型来识别和操作图像。这涉及到UI元素的定位,如通过坐标、属性或名称。对于复杂的图像识别,可以使用图像识别技术,比如模板匹配或机器学习算法,但这通常需要更高级的自定义实现...
这里需要用到GDI+,因为WPF的`BitmapSource`和WinForms的`Bitmap`之间需要转换: ```csharp Bitmap bitmap = System.Windows.Media.Imaging.BitmapSourceToBitmap(bitmapSource); bitmap.Save("screenshot.png",...
"截图WPF&Winform&C#"这个项目提供了一个全面的解决方案,它包括了两种不同的UI框架——Windows Presentation Foundation (WPF) 和 Windows Forms (WinForms) 下的截图功能。这两种框架都是.NET Framework的重要组成...
GDI+适用于传统的WinForms应用程序,而WPF更适合现代UI设计,支持更丰富的图形和动画功能。 1. **使用GDI+实现雷达扫描效果** - 创建一个`Bitmap`对象作为画布,用于绘制雷达扫描图案。 - 使用`Graphics`对象从`...
综上所述,这个压缩包中的源码应该涉及了C#语言、WinForms或WPF UI设计、GDI+图形操作以及文件保存等技术。通过学习和理解这些源码,开发者可以加深对C#屏幕截图功能实现的理解,并能在此基础上扩展自己的应用。
同时,可以考虑使用WPF(Windows Presentation Foundation)代替WinForms,因为WPF提供了更强大的图形处理能力和更好的UI设计工具。 总的来说,C#中的屏幕截取和裁剪涉及了Windows图形编程、事件处理和图像处理等多...
GDI+适用于WinForms应用程序,而WPF适用于更现代的UI设计。根据项目需求,可以选择合适的框架将SVG图形渲染为位图。 4. **转换过程**:基本步骤包括加载SVG文件,创建一个适当的绘图表面(如Bitmap对象),然后使用...
在本文中,我们将深入探讨如何在C#的WPF(Windows Presentation Foundation)应用程序中使用AForge.NET库来处理摄像头输入并实现图像捕获功能。AForge.NET是一个开源框架,提供了丰富的计算机视觉和图像处理功能,它...
在VS2008中,开发者可以使用WinForms或WPF来创建用户界面,其中WinForms更适用于简单截图应用,而WPF则提供更多高级的图形和UI特性。 例如,以下是一个简单的WinForms截图示例代码: ```csharp using System; ...
2. **图像显示**:在窗口或控件上显示图像,这可能涉及到控件的选择和设置,如在C# WinForms中使用PictureBox控件,或者在WPF中使用Image控件。 3. **图像操作**:可以对图像进行缩放、旋转、裁剪等操作,通常涉及...
WPF或WinForms可能是UI设计的首选框架。 9. 错误处理与日志记录:为了提高软件的稳定性和可维护性,项目中应该包含了错误处理机制,如try-catch语句,以及日志记录功能,以便于追踪和解决可能出现的问题。 通过这...
然后,可以使用`Mat.ToBitmap()`方法将其转换为.NET的`Bitmap`对象,以便在WinForms或WPF应用中显示。例如: ```csharp Mat frame = new Mat(); while (true) { if (capture.Read(frame)) { Bitmap bitmap = ...
Bitmap bitmap = icon.ToBitmap(); pictureBox.Image = bitmap; ``` 5. **保存和导出Icon**:可以将Icon对象保存到文件,支持多种图像格式,包括.ico: ```csharp icon.Save("new_icon.ico"); ``` 6. **...
2. **设计UI**:在WinForm中添加一个PictureBox控件,这将是显示PDF内容的地方。你可能还需要一些按钮来控制页面导航、缩放等操作。 3. **加载PDF文件**:在C#代码中,你可以使用PDFium提供的API来加载PDF文件。...
描述中提到的“线程间操作无效”是一个常见的问题,当在非UI线程尝试修改UI元素时,会导致运行时错误,因为Windows窗体(WinForms)和WPF等UI框架都是线程不安全的。 在Windows编程中,多线程与界面之间的通信是一...
// 如果是在WinForms中显示 pictureBox1.Image = qrCodeImage; // 如果是在WPF中显示 pictureBox.Source = BitmapSource.Create( qrCodeImage.Width, qrCodeImage.Height, 96, 96, PixelFormats.Bgr24, null, ...
WinForms更适合快速开发,而WPF则提供更丰富的图形渲染和UI设计能力。 2. **鼠标事件处理**:取色器的核心功能是监听鼠标点击事件,当用户在屏幕上点击时,获取该位置的像素颜色。这需要对Windows消息循环和鼠标...
总结来说,开发一款模仿QQ截图功能的C#软件,需要熟悉WinForms或WPF的UI设计,理解Windows图形绘制原理,掌握图像处理和文件操作,以及可能的网络编程。这款软件的源代码(如压缩包中的 SCREEN_CAPTURE 文件)对于...