通过


WriteableBitmap 类

定义

提供可写入和更新的集合 BitmapSource

public ref class WriteableBitmap sealed : System::Windows::Media::Imaging::BitmapSource
public sealed class WriteableBitmap : System.Windows.Media.Imaging.BitmapSource
type WriteableBitmap = class
    inherit BitmapSource
Public NotInheritable Class WriteableBitmap
Inherits BitmapSource
继承

示例

以下示例演示如何在鼠标移动时将 a WriteableBitmap 用作绘制像素的 Image 源。

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Input;

namespace WriteableBitmapDemo
{
    class Program
    {
        static WriteableBitmap writeableBitmap;
        static Window w;
        static Image i;

        [STAThread]
        static void Main(string[] args)
        {
            i = new Image();
            RenderOptions.SetBitmapScalingMode(i, BitmapScalingMode.NearestNeighbor);
            RenderOptions.SetEdgeMode(i, EdgeMode.Aliased);
           
            w = new Window();
            w.Content = i;
            w.Show();

            writeableBitmap = new WriteableBitmap(
                (int)w.ActualWidth, 
                (int)w.ActualHeight, 
                96, 
                96, 
                PixelFormats.Bgr32, 
                null);

            i.Source = writeableBitmap;

            i.Stretch = Stretch.None;
            i.HorizontalAlignment = HorizontalAlignment.Left;
            i.VerticalAlignment = VerticalAlignment.Top;

            i.MouseMove += new MouseEventHandler(i_MouseMove);
            i.MouseLeftButtonDown += 
                new MouseButtonEventHandler(i_MouseLeftButtonDown);
            i.MouseRightButtonDown += 
                new MouseButtonEventHandler(i_MouseRightButtonDown);

            w.MouseWheel += new MouseWheelEventHandler(w_MouseWheel);

            Application app = new Application();
            app.Run();
        }

        // The DrawPixel method updates the WriteableBitmap by using
        // unsafe code to write a pixel into the back buffer.
        static void DrawPixel(MouseEventArgs e)
        {
            int column = (int)e.GetPosition(i).X;
            int row = (int)e.GetPosition(i).Y;

            try{
                // Reserve the back buffer for updates.
                writeableBitmap.Lock();

                unsafe
                {
                    // Get a pointer to the back buffer.
                    IntPtr pBackBuffer = writeableBitmap.BackBuffer;

                    // Find the address of the pixel to draw.
                    pBackBuffer += row * writeableBitmap.BackBufferStride;
                    pBackBuffer += column * 4;

                    // Compute the pixel's color.
                    int color_data = 255 << 16; // R
                    color_data |= 128 << 8;   // G
                    color_data |= 255 << 0;   // B

                    // Assign the color data to the pixel.
                    *((int*) pBackBuffer) = color_data;
                }
    
                // Specify the area of the bitmap that changed.
                writeableBitmap.AddDirtyRect(new Int32Rect(column, row, 1, 1));
            }
            finally{
                // Release the back buffer and make it available for display.
                writeableBitmap.Unlock();
            }
        }

        static void ErasePixel(MouseEventArgs e)
        {
            byte[] ColorData = { 0, 0, 0, 0 }; // B G R

            Int32Rect rect = new Int32Rect(
                    (int)(e.GetPosition(i).X), 
                    (int)(e.GetPosition(i).Y), 
                    1, 
                    1);

            writeableBitmap.WritePixels( rect, ColorData, 4, 0);
        }

        static void i_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            ErasePixel(e);
        }

        static void i_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            DrawPixel(e);
        }

        static void i_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                DrawPixel(e);
            }
            else if (e.RightButton == MouseButtonState.Pressed)
            {
                ErasePixel(e);
            }
        }

        static void w_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            System.Windows.Media.Matrix m = i.RenderTransform.Value;

            if (e.Delta > 0)
            {
                m.ScaleAt(
                    1.5, 
                    1.5, 
                    e.GetPosition(w).X, 
                    e.GetPosition(w).Y);
            }
            else
            {
                m.ScaleAt(
                    1.0 / 1.5, 
                    1.0 / 1.5, 
                    e.GetPosition(w).X, 
                    e.GetPosition(w).Y);
            }

            i.RenderTransform = new MatrixTransform(m);
        }
    }
}

注解

使用 WriteableBitmap 类按帧更新和呈现位图。 这对于生成算法内容(如分形图像)和数据可视化(如音乐可视化工具)非常有用。

WriteableBitmap 类使用两个缓冲区。 后台缓冲区在系统内存中分配,并累积当前未显示的内容。 前端缓冲区在系统内存中分配,并包含当前显示的内容。 呈现系统将前缓冲区复制到视频内存以供显示。

两个线程使用这些缓冲区。 用户界面(UI)线程将生成 UI,但不会将其呈现到屏幕。 UI 线程响应用户输入、计时器和其他事件。 应用程序可以有多个 UI 线程。 呈现线程撰写并呈现 UI 线程中的更改。 每个应用程序只有一个呈现线程。

UI 线程将内容写入后台缓冲区。 呈现线程从前缓冲区读取内容,并将其复制到视频内存中。 使用已更改的矩形区域跟踪对后缓冲区的更改。

调用其中 WritePixels 一个重载以自动更新和显示后台缓冲区中的内容。

若要更好地控制更新,以及对后台缓冲区的多线程访问,请使用以下工作流。

  1. Lock调用该方法以保留用于更新的后备缓冲区。

  2. 通过访问属性获取指向后缓冲区的 BackBuffer 指针。

  3. 将更改写入后台缓冲区。 锁定后缓冲区时 WriteableBitmap ,其他线程可能会将更改写入后台缓冲区。

  4. AddDirtyRect调用该方法以指示已更改的区域。

  5. Unlock调用该方法以释放后台缓冲区并允许向屏幕呈现。

将更新发送到呈现线程时,呈现线程会将已更改的矩形从后缓冲区复制到前缓冲区。 呈现系统控制此交换,以避免死锁和重绘项目,如“撕裂”。

构造函数

名称 说明
WriteableBitmap(BitmapSource)

使用给定BitmapSource的初始化类的新实例WriteableBitmap

WriteableBitmap(Int32, Int32, Double, Double, PixelFormat, BitmapPalette)

使用指定的参数初始化类的新实例 WriteableBitmap

属性

名称 说明
BackBuffer

获取指向后缓冲区的指针。

BackBufferStride

获取一个值,该值指示单个像素数据行中的字节数。

CanFreeze

获取一个值,该值指示是否可以使对象不可修改。

(继承自 Freezable)
DependencyObjectType

获取包装 DependencyObjectType 此实例的 CLR 类型。

(继承自 DependencyObject)
Dispatcher

获取与此DispatcherDispatcherObject关联的值。

(继承自 DispatcherObject)
DpiX

获取图像的水平点/英寸(dpi)。

(继承自 BitmapSource)
DpiY

获取图像的垂直点/英寸(dpi)。

(继承自 BitmapSource)
Format

获取位图数据的本机 PixelFormat

(继承自 BitmapSource)
HasAnimatedProperties

获取一个值,该值指示一个或多个 AnimationClock 对象是否与此对象的依赖属性中的任何一个相关联。

(继承自 Animatable)
Height

获取与设备无关的单位(每单位 1/96 英寸)的源位图的高度。

(继承自 BitmapSource)
IsDownloading

获取一个值,该值指示内容当前是否 BitmapSource 正在下载。

(继承自 BitmapSource)
IsFrozen

获取一个值,该值指示对象当前是否可修改。

(继承自 Freezable)
IsSealed

获取一个值,该值指示此实例当前是否密封(只读)。

(继承自 DependencyObject)
Metadata

获取与此位图图像关联的元数据。

(继承自 BitmapSource)
Palette

获取位图的调色板(如果指定)。

(继承自 BitmapSource)
PixelHeight

获取位图的高度(以像素为单位)。

(继承自 BitmapSource)
PixelWidth

获取位图的宽度(以像素为单位)。

(继承自 BitmapSource)
Width

获取独立于设备的位图的宽度(每单位 1/96 英寸)。

(继承自 BitmapSource)

方法

名称 说明
AddDirtyRect(Int32Rect)

指定已更改的位图区域。

ApplyAnimationClock(DependencyProperty, AnimationClock, HandoffBehavior)

将 a AnimationClock 应用于指定的 DependencyProperty。 如果该属性已进行动画处理,则使用指定的 HandoffBehavior 属性。

(继承自 Animatable)
ApplyAnimationClock(DependencyProperty, AnimationClock)

将 a AnimationClock 应用于指定的 DependencyProperty。 如果该属性已进行动画处理, SnapshotAndReplace 则使用切换行为。

(继承自 Animatable)
BeginAnimation(DependencyProperty, AnimationTimeline, HandoffBehavior)

将动画应用于指定的 DependencyProperty。 当呈现下一帧时,将启动动画。 如果指定的属性已进行动画处理,则使用指定的 HandoffBehavior 属性。

(继承自 Animatable)
BeginAnimation(DependencyProperty, AnimationTimeline)

将动画应用于指定的 DependencyProperty。 当呈现下一帧时,将启动动画。 如果指定的属性已进行动画处理, SnapshotAndReplace 则使用交接行为。

(继承自 Animatable)
CheckAccess()

确定调用线程是否有权访问此 DispatcherObject权限。

(继承自 DispatcherObject)
CheckIfSiteOfOrigin()

检查位图源内容是否来自源的已知站点。 此方法用于确保像素复制操作安全。

(继承自 BitmapSource)
ClearValue(DependencyProperty)

清除属性的本地值。 要清除的属性由 DependencyProperty 标识符指定。

(继承自 DependencyObject)
ClearValue(DependencyPropertyKey)

清除只读属性的本地值。 要清除的属性由一个 DependencyPropertyKey.

(继承自 DependencyObject)
Clone()

创建此 WriteableBitmap对象的可修改克隆,从而创建此对象的值的深层副本。 复制依赖项属性时,此方法复制资源引用和数据绑定(但它们可能不再解析),但不能解析动画或其当前值。

CloneCore(Freezable)

使此实例成为指定 BitmapSource副本的深层副本。 复制依赖项属性时,此方法复制资源引用和数据绑定(但它们可能不再解析),但不能解析动画或其当前值。

(继承自 BitmapSource)
CloneCurrentValue()

创建此 ByteAnimationUsingKeyFrames 对象的可修改克隆,从而生成此对象的当前值的深层副本。 不会复制资源引用、数据绑定和动画,而是复制其当前值。

CloneCurrentValueCore(Freezable)

使此实例成为使用当前属性值指定的 BitmapSource 可修改的深层副本。 不会复制资源引用、数据绑定和动画,而是复制其当前值。

(继承自 BitmapSource)
CoerceValue(DependencyProperty)

强制指定依赖属性的值。 这是通过调用中调用依赖属性CoerceValueCallback的属性元数据中指定的任何DependencyObject函数来实现的。

(继承自 DependencyObject)
CopyPixels(Array, Int32, Int32)

将位图像素数据复制到具有指定步幅的像素数组中,从指定的偏移量开始。

(继承自 BitmapSource)
CopyPixels(Int32Rect, Array, Int32, Int32)

将指定矩形中的位图像素数据复制到具有指定偏移量开始的指定步幅的像素数组中。

(继承自 BitmapSource)
CopyPixels(Int32Rect, IntPtr, Int32, Int32)

复制指定矩形中的位图像素数据。

(继承自 BitmapSource)
CreateInstance()

初始化 Freezable 类的新实例。

(继承自 Freezable)
CreateInstanceCore()

在派生类中实现时,创建派生类的新实例 Freezable

(继承自 Freezable)
Equals(Object)

确定提供的 DependencyObject 是否等效于当前 DependencyObject

(继承自 DependencyObject)
Freeze()

使当前对象不可修改,并将其 IsFrozen 属性设置为 true

(继承自 Freezable)
FreezeCore(Boolean)

使派生类的 BitmapSource 实例不可变。

(继承自 BitmapSource)
GetAnimationBaseValue(DependencyProperty)

返回指定的 DependencyProperty非动画值。

(继承自 Animatable)
GetAsFrozen()

使用基(非动画化)属性值创建冻结的副本 Freezable。 由于副本已冻结,因此引用复制任何冻结的子对象。

(继承自 Freezable)
GetAsFrozenCore(Freezable)

使此实例成为指定 BitmapSource 对象的克隆。

(继承自 BitmapSource)
GetCurrentValueAsFrozen()

创建使用当前属性值的 Freezable 冻结副本。 由于副本已冻结,因此引用复制任何冻结的子对象。

(继承自 Freezable)
GetCurrentValueAsFrozenCore(Freezable)

将此实例指定为冻结的 BitmapSource克隆。 不会复制资源引用、数据绑定和动画,而是复制其当前值。

(继承自 BitmapSource)
GetHashCode()

获取此 DependencyObject代码的哈希代码。

(继承自 DependencyObject)
GetLocalValueEnumerator()

创建一个专用枚举器,用于确定哪些依赖项属性具有本地 DependencyObject设置的值。

(继承自 DependencyObject)
GetType()

获取当前实例的 Type

(继承自 Object)
GetValue(DependencyProperty)

返回此实例 DependencyObject上的依赖属性的当前有效值。

(继承自 DependencyObject)
InvalidateProperty(DependencyProperty)

重新评估指定依赖属性的有效值。

(继承自 DependencyObject)
Lock()

保留用于更新的后台缓冲区。

MemberwiseClone()

创建当前 Object的浅表副本。

(继承自 Object)
OnChanged()

修改当前 Freezable 对象时调用。

(继承自 Freezable)
OnFreezablePropertyChanged(DependencyObject, DependencyObject, DependencyProperty)

此成员支持 Windows Presentation Foundation (WPF) 基础结构,不应直接从代码使用。

(继承自 Freezable)
OnFreezablePropertyChanged(DependencyObject, DependencyObject)

确保为 DependencyObjectType 刚刚设置的数据成员建立适当的上下文指针。

(继承自 Freezable)
OnPropertyChanged(DependencyPropertyChangedEventArgs)

重写要DependencyObject调用的任何Changed处理程序的OnPropertyChanged(DependencyPropertyChangedEventArgs)实现,以响应类型的Freezable更改依赖项属性。

(继承自 Freezable)
ReadLocalValue(DependencyProperty)

返回依赖属性的本地值(如果存在)。

(继承自 DependencyObject)
ReadPreamble()

确保 Freezable 从有效线程访问该对象。 继承者 Freezable 必须在读取非依赖属性的数据成员的任何 API 的开头调用此方法。

(继承自 Freezable)
SetCurrentValue(DependencyProperty, Object)

设置依赖项属性的值,而不更改其值源。

(继承自 DependencyObject)
SetValue(DependencyProperty, Object)

设置依赖属性的本地值,由依赖属性标识符指定。

(继承自 DependencyObject)
SetValue(DependencyPropertyKey, Object)

设置只读依赖属性的本地值,由 DependencyPropertyKey 依赖属性的标识符指定。

(继承自 DependencyObject)
ShouldSerializeProperty(DependencyProperty)

返回一个值,该值指示序列化进程是否应序列化所提供的依赖属性的值。

(继承自 DependencyObject)
ToString()

基于当前区域性创建此对象的字符串表示形式。

(继承自 ImageSource)
ToString(IFormatProvider)

基于 IFormatProvider 传入创建此对象的字符串表示形式。 如果提供程序是 nullCurrentCulture 则使用提供程序。

(继承自 ImageSource)
TryLock(Duration)

尝试锁定位图,等待的时间长度不超过指定的时间长度。

Unlock()

释放后退缓冲区,使其可用于显示。

VerifyAccess()

强制调用线程有权访问此 DispatcherObject权限。

(继承自 DispatcherObject)
WritePixels(Int32Rect, Array, Int32, Int32, Int32)

更新位图指定区域中的像素。

WritePixels(Int32Rect, Array, Int32, Int32)

更新位图指定区域中的像素。

WritePixels(Int32Rect, IntPtr, Int32, Int32, Int32, Int32)

更新位图指定区域中的像素。

WritePixels(Int32Rect, IntPtr, Int32, Int32)

更新位图指定区域中的像素。

WritePostscript()

Changed为该事件Freezable引发并调用其OnChanged()方法。 派生自 Freezable 的类应在修改未存储为依赖属性的类成员的任何 API 末尾调用此方法。

(继承自 Freezable)
WritePreamble()

验证 Freezable 是否未冻结,以及是否正在从有效的线程上下文访问它。 Freezable 继承者应在写入非依赖属性的数据成员的任何 API 的开头调用此方法。

(继承自 Freezable)

活动

名称 说明
Changed

修改它包含的对象时 Freezable 发生。

(继承自 Freezable)
DecodeFailed

由于映像标头损坏,映像无法加载时发生。

(继承自 BitmapSource)
DownloadCompleted

完全下载位图内容时发生。

(继承自 BitmapSource)
DownloadFailed

当位图内容无法下载时发生。

(继承自 BitmapSource)
DownloadProgress

当位图内容的下载进度发生更改时发生。

(继承自 BitmapSource)

显式接口实现

名称 说明
IFormattable.ToString(String, IFormatProvider)

使用指定格式设置当前实例的值的格式。

(继承自 ImageSource)

适用于

另请参阅