免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2721 | 回复: 0
打印 上一主题 下一主题

WPF中,如何使用图像API进行绘制而不是XAML? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-07-24 08:45 |只看该作者 |倒序浏览
首先,由于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, 1),
new Rect(new Point(20, 20), new Size(100, 100)));
//画文字
            dc.DrawText(new FormattedText("Hello, World!", CultureInfo.CurrentCulture,
FlowDirection.LeftToRight, new Typeface("Arial"), 40, Brushes.Orange),
new Point(50,60));
}
}
}
(



2)保存图片到文件:


        protected void SavePhoto(string fileName)
{
DrawingVisual drawingVisual = new DrawingVisual();
DrawingContext drawingContext = drawingVisual.RenderOpen();
// 画矩形
            Rect rect = new Rect(new Point(160, 100), new Size(320, 80));
drawingContext.DrawRectangle(Brushes.LightBlue, (Pen)null, rect);
// 画文字
            drawingContext.DrawText(
new FormattedText("Hello, world",
CultureInfo.GetCultureInfo("en-us"),
FlowDirection.LeftToRight,
new Typeface("Verdana"),
36, Brushes.Black),
new Point(100, 60));
drawingContext.Close();
// 利用RenderTargetBitmap对象,以保存图片
            RenderTargetBitmap renderBitmap = new RenderTargetBitmap((int)this.Width, (int)this.Height, 96, 96, 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.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP