WPF中,如何使用图像API进行绘制而不是XAML?

123 篇文章 2 订阅
116 篇文章 9 订阅

--------------------------------------------------------------------------------
转载时请保留以下信息:
大可山 [MSN:a3news(AT)hotmail.com]
http://www.zpxp.com 萝卜鼠在线图形图像处理
--------------------------------------------------------------------------------

首先,由于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.

(注意桔色文字部分)

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
WPF ,可以使用 XAML 和代码来绘制动态图像。下面是一个简单的示例,演示如何使用 XAML 和代码绘制一个移动的圆形: XAML 代码: ```xml <Window x:Class="WpfApp1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Canvas x:Name="canvas"> <Ellipse x:Name="circle" Fill="Red" Width="50" Height="50" Canvas.Left="0" Canvas.Top="0"/> </Canvas> </Window> ``` C# 代码: ```csharp public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); // 创建一个动画 DoubleAnimation animation = new DoubleAnimation(); animation.From = 0; animation.To = canvas.ActualWidth - circle.Width; animation.Duration = TimeSpan.FromSeconds(5); animation.RepeatBehavior = RepeatBehavior.Forever; // 将动画应用到圆形的 Canvas.Left 属性上 circle.BeginAnimation(Canvas.LeftProperty, animation); } } ``` 这个示例,我们在 Canvas 添加了一个 Ellipse 元素,它表示一个圆形。我们将圆形的颜色设置为红色,宽度和高度均为 50,初始位置为 Canvas 的左上角。 然后,在 C# 代码,我们创建了一个 DoubleAnimation 对象,表示一个从 0 到 Canvas 的宽度减去圆形宽度的动画。我们将动画的持续时间设置为 5 秒,并将其重复执行。 最后,我们调用圆形的 BeginAnimation 方法,将动画应用到圆形的 Canvas.Left 属性上,使得圆形在 Canvas 移动。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值