WPF中如何使用图像API进行绘制
首先,由于WPF中不象GDI+中有Graphics对象,因此你无法使用Graphics进行绘图了,取而代之的是:DrawingContext;类似地,GDI+中的OnPaint已被OnRender取代。
其次,UIElement有一个OnRendar方法,它的定义是:
protected virtual void OnRender (DrawingContext drawingContext)
但我们不能直接调用OnRender方法,也不能直接创建DrawingContext实例,但可以利用 DrawingGroup.Open 和DrawingVisual.RenderOpen。
这里举两个例子:
(1)自定义绘制Canvas:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows;
using System.Globalization; namespace BrawDraw.Com.Test
{
class CanvasCustomPaint : Canvas
{
protected override void OnRender(DrawingContext dc)
{
base.OnRender(dc);
//画矩形
dc.DrawRectangle(Brushes.Red, new Pen(Brushes.Blue, ),
new Rect(new Point(, ), new Size(, )));
//画文字
dc.DrawText(new FormattedText("Hello, World!", CultureInfo.CurrentCulture,
FlowDirection.LeftToRight, new Typeface("Arial"), , Brushes.Orange),
new Point(,));
}
}
}
(2)保存图片到文件:
protected void SavePhoto(string fileName)
{
DrawingVisual drawingVisual = new DrawingVisual();
DrawingContext drawingContext = drawingVisual.RenderOpen();
// 画矩形
Rect rect = new Rect(new Point(, ), new Size(, ));
drawingContext.DrawRectangle(Brushes.LightBlue, (Pen)null, rect);
// 画文字
drawingContext.DrawText(
new FormattedText("Hello, world",
CultureInfo.GetCultureInfo("en-us"),
FlowDirection.LeftToRight,
new Typeface("Verdana"),
, Brushes.Black),
new Point(, )); drawingContext.Close(); // 利用RenderTargetBitmap对象,以保存图片
RenderTargetBitmap renderBitmap = new RenderTargetBitmap((int)this.Width, (int)this.Height, , , PixelFormats.Pbgra32);
renderBitmap.Render(drawingVisual); // 利用JpegBitmapEncoder,对图像进行编码,以便进行保存
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
// 保存文件
FileStream fileStream = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite);
encoder.Save(fileStream);
// 关闭文件流
fileStream.Close();
}
最后附上这里的一段话(http://blogs.msdn.com/timothyc/archive/2006/06/16/634638.aspx),除加重点文字以桔色示凸出外, 以原样提供:
Adding seemingly simple tweaks (e.g., clipping, bitmap effects) to our scene causes us to fall back to software, and software rending in WPF is slower than GDI+ software rendering.
First, the WPF software rendering code is derived from the GDI+ codebase. There are certain limits to what can be accomplished in hardware, and we have to work around what the hardware vendors give us. As graphics hardware evolves, those limits are likely to become better over time. If at least some portion of your scene is rendered in hardware, the cost of rendering is already going to be faster than it was in GDI+. Finally, we shipped a tool at the PDC called ‘Perforator’ to help identify where software rendering occurs.
(注意红色文字部分)
WPF中如何使用图像API进行绘制的更多相关文章
- 深入WPF中的图像画刷(ImageBrush)之1——ImageBrush使用举例
原文:深入WPF中的图像画刷(ImageBrush)之1--ImageBrush使用举例 昨天我在<简述WPF中的画刷(Brush) >中简要介绍了WPF中的画刷的使用.现在接着深入研究 ...
- 扩展ArcGIS API for Silverlight/WPF 中的TextSymbol支持角度标注
原文 http://blog.csdn.net/esricd/article/details/7587136 在ArcGIS API for Silverlight/WPF中原版的TextSymbol ...
- 在WPF中自定义你的绘制(五)
原文:在WPF中自定义你的绘制(五) 在WPF中自定义你的绘制(五) ...
- 在WPF中自定义你的绘制(三)
原文:在WPF中自定义你的绘制(三) 在WPF中自定义你的绘制(三) ...
- 在WPF中自定义你的绘制(四)
原文:在WPF中自定义你的绘制(四) 在WPF中自定义你的绘制(四) ...
- 在WPF中自定义你的绘制(一)
原文:在WPF中自定义你的绘制(一) 在WPF中自定义你的绘制(一) ...
- 在WPF中自定义你的绘制(二)
原文:在WPF中自定义你的绘制(二) 在WPF中自定义你的绘制(二) ...
- WPF中自定义绘制内容
先说结论:实现了在自定义大小的窗口中,加载图片,并在图片上绘制一个矩形框:且在窗口大小改变的情况,保持绘制的矩形框与图片的先对位置不变. 在WinForm中,我们可以很方便地绘制自己需要的内容,在WP ...
- WPF中使用amCharts绘制股票K线图
原文:WPF中使用amCharts绘制股票K线图 本想自己用GDI绘图, 通过数据直接绘制一张蜡柱图, 但觉得这样子的功能比较少, 所以到网上搜索一些能画出K线图的控件. 发现DynamicDataD ...
随机推荐
- Redis学习手册(事务)
一.概述: 和众多其它数据库一样,Redis作为NoSQL数据库也同样提供了事务机制.在Redis中,MULTI/EXEC/DISCARD/WATCH这四个命令是我们实现事务的基石.相信对有关系型数据 ...
- 数据持久层(三)ODB介绍
ODB: C++ Object-Relational Mapping (ORM) ODB is an open-source, cross-platform, and cross-database o ...
- 合并两个vectcor——2013-08-26
vector<int> v1; vector<int> v2; for(int i=0; i<5; i++) { v1.push_back(i); if(i%2==1) ...
- Linux Ubuntu上架设FTP
操作系统:ubuntu (GNU/Linux) 为了在机子上架设ftp服务器,我们需要安装ftp服务器软件.Linux下具有代表性的ftp服务器软件有Wu-FTP,ProFTP和Vsftp.Wu-FT ...
- Java8特性---关于Null
为了防止无良网站的爬虫抓取文章,特此标识,转载请注明文章出处.LaplaceDemon/SJQ. http://www.cnblogs.com/shijiaqi1066/p/5713941.html ...
- ecshop在PHP 5.4以上版本各种错误问题处理
在php5.4版本之后有很多的函数与功能进行丢弃与升级功能了,现在国内很多CMS都还未按php5.4标准来做了,下面我整理了一些在ecshop在PHP 5.4以上版本各种错误问题处理. 1.PHP 5 ...
- c中计时的几种方法
C计时的几种方法说明及例程 1. 使用clock() 函数 头文件:<time.h> clock()函数,返回“自程序启动到调用该函数,CPU时钟的计时单元数(clock tick)” 每 ...
- LIBPNG使用小结(二)
之前写的LIBPNG库学习小结介绍了怎么样自定义LIBPNG库的write.read.flush函数,而不使用LIBPNG库提供的默认函数. 上一篇讲述的都是在单线程的情况下,今天将程序升级,放在多线 ...
- 0基础学习ios开发笔记第一天
Ios操作 界面操作 快捷键 command + c 复制 command+v 粘贴 command +a 全选 command +s 保存 command +z 撤销 command +x 剪切 ...
- C++ 引用(&)
#include <iostream> void sort(int &a, int &b){ if (a>=b) { return; } if (a<b) { ...