WriteableBitmap 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
提供可写入和更新的集合 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
- 继承
-
WriteableBitmap
示例
以下示例演示如何在鼠标移动时将 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 一个重载以自动更新和显示后台缓冲区中的内容。
若要更好地控制更新,以及对后台缓冲区的多线程访问,请使用以下工作流。
Lock调用该方法以保留用于更新的后备缓冲区。
通过访问属性获取指向后缓冲区的 BackBuffer 指针。
将更改写入后台缓冲区。 锁定后缓冲区时 WriteableBitmap ,其他线程可能会将更改写入后台缓冲区。
AddDirtyRect调用该方法以指示已更改的区域。
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) |
方法
活动
| 名称 | 说明 |
|---|---|
| Changed |
修改它包含的对象时 Freezable 发生。 (继承自 Freezable) |
| DecodeFailed |
由于映像标头损坏,映像无法加载时发生。 (继承自 BitmapSource) |
| DownloadCompleted |
完全下载位图内容时发生。 (继承自 BitmapSource) |
| DownloadFailed |
当位图内容无法下载时发生。 (继承自 BitmapSource) |
| DownloadProgress |
当位图内容的下载进度发生更改时发生。 (继承自 BitmapSource) |
显式接口实现
| 名称 | 说明 |
|---|---|
| IFormattable.ToString(String, IFormatProvider) |
使用指定格式设置当前实例的值的格式。 (继承自 ImageSource) |