首先,由于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进行绘制的更多相关文章

  1. 深入WPF中的图像画刷(ImageBrush)之1——ImageBrush使用举例

    原文:深入WPF中的图像画刷(ImageBrush)之1--ImageBrush使用举例 昨天我在<简述WPF中的画刷(Brush)  >中简要介绍了WPF中的画刷的使用.现在接着深入研究 ...

  2. 扩展ArcGIS API for Silverlight/WPF 中的TextSymbol支持角度标注

    原文 http://blog.csdn.net/esricd/article/details/7587136 在ArcGIS API for Silverlight/WPF中原版的TextSymbol ...

  3. 在WPF中自定义你的绘制(五)

    原文:在WPF中自定义你的绘制(五) 在WPF中自定义你的绘制(五)                                                                   ...

  4. 在WPF中自定义你的绘制(三)

    原文:在WPF中自定义你的绘制(三) 在WPF中自定义你的绘制(三)                                                                  ...

  5. 在WPF中自定义你的绘制(四)

    原文:在WPF中自定义你的绘制(四)                                   在WPF中自定义你的绘制(四)                                 ...

  6. 在WPF中自定义你的绘制(一)

    原文:在WPF中自定义你的绘制(一)   在WPF中自定义你的绘制(一)                                                                 ...

  7. 在WPF中自定义你的绘制(二)

    原文:在WPF中自定义你的绘制(二)   在WPF中自定义你的绘制(二)                                                                 ...

  8. WPF中自定义绘制内容

    先说结论:实现了在自定义大小的窗口中,加载图片,并在图片上绘制一个矩形框:且在窗口大小改变的情况,保持绘制的矩形框与图片的先对位置不变. 在WinForm中,我们可以很方便地绘制自己需要的内容,在WP ...

  9. WPF中使用amCharts绘制股票K线图

    原文:WPF中使用amCharts绘制股票K线图 本想自己用GDI绘图, 通过数据直接绘制一张蜡柱图, 但觉得这样子的功能比较少, 所以到网上搜索一些能画出K线图的控件. 发现DynamicDataD ...

随机推荐

  1. IMP 导入数据报错 OCI-21500 OCI-22275

    IMP导入数据报错如下: OCI-21500: internal error code, arguments: [kgepop: no error frame to pop to], [], [], ...

  2. [Javascript] Gradient Fills on the HTML5 Canvas

    window.onload = function() { var canvas = document.getElementById("canvas"), context = can ...

  3. [AngularJS + Webpack] require directives

    direictives/index.js: module.exports = function(ngModule) { //register all the directives here requi ...

  4. apache配置directoryindex

    为了让程序自动执行目录下的某个文件,可以配置虚拟主机中的directoryindex 如: <VirtualHost *:80>    DocumentRoot "D:/var/ ...

  5. Spring + JDK Timer Scheduler Example--reference

    http://www.mkyong.com/spring/spring-jdk-timer-scheduler-example/ In this example, you will use Sprin ...

  6. [转]VS2010 (C#)winform程序打包发布图解

    1.新建一个Windows窗体应用程序,例如项目名为monitor,功能略.新建的时候不要忘了创建解决方案. 2.在monitor解决方案上“右击”—— “添加”——“新建项目”,选择“其他类型项目” ...

  7. centos 6.3安装mono和monoDevelop过程

    Mono官方网站:http://www.mono-project.com MonoDevelop官方网站:http://monodevelop.com/ 注:整个安装过程最好在同一个终端下完成! 1. ...

  8. 安装tensorflow

    官网:http://tensorflow.org/安装步骤:1.sudo apt-get install python-pip python-dev python-virtualenv 3    co ...

  9. javascript的一点误解

    var a=[]; for(var i = 0; i < 10; i++) { a[i] = function() { return i; } } console.log(a[9]()); co ...

  10. Openfire3.8.2在eclipse中Debug方式启动最简单的方式

    一.前言 最近打算研究一下Openfire,于是打算最好能够以Debug方式启动Openfire的Server,到网上一搜,还果真早到官网的一篇文章来: http://community.ignite ...