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
下列範例示範如何在 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使用 類別來更新和轉譯每個畫面格的點陣圖。 這適用于產生演算法內容,例如 Fractal 影像,以及資料視覺效果,例如音樂視覺化檢視。
類別 WriteableBitmap 使用兩個緩衝區。 後端緩衝區會配置在系統記憶體中,並累積目前未顯示的內容。 前端緩衝區會配置在系統記憶體中,並包含目前顯示的內容。 轉譯系統會將前端緩衝區複製到視訊記憶體以供顯示。
兩個執行緒會使用這些緩衝區。 使用者介面 (UI) 執行緒會產生 UI,但不會將其呈現到畫面。 UI 執行緒會回應使用者輸入、計時器和其他事件。 應用程式可以有多個 UI 執行緒。 轉 譯執行緒 會撰寫並轉譯 UI 執行緒中的變更。 每個應用程式只有一個轉譯執行緒。
UI 執行緒會將內容寫入背景緩衝區。 轉譯執行緒會從前端緩衝區讀取內容,並將它複製到視訊記憶體。 後端緩衝區的變更會使用已變更的矩形區域來追蹤。
呼叫其中 WritePixels 一個多載,以自動更新和顯示後端緩衝區中的內容。
若要進一步控制更新,以及對後端緩衝區的多執行緒存取,請使用下列工作流程。
Lock呼叫 方法以保留後端緩衝區以進行更新。
藉由存取 屬性來取得後端緩衝區的 BackBuffer 指標。
將變更寫入背景緩衝區。 鎖定時 WriteableBitmap ,其他執行緒可能會將變更寫入背景緩衝區。
AddDirtyRect呼叫 方法,以指出已變更的區域。
Unlock呼叫 方法以釋放背景緩衝區,並允許簡報到畫面。
當更新傳送至轉譯執行緒時,轉譯執行緒會將變更的矩形從背景緩衝區複製到前端緩衝區。 轉譯系統會控制此交換,以避免死結和重繪成品,例如「卸載」。
Writeable |
使用指定的 WriteableBitmap,初始化 BitmapSource 類別的新執行個體。 |
Writeable |
使用指定的參數,初始化 WriteableBitmap 類別的新執行個體。 |
Back |
取得指向背景緩衝區的指標。 |
Back |
取得值,這個值指出單列像素資料中的位元組數。 |
Can |
取得值,指出是否可以將物件設為不可修改。 (繼承來源 Freezable) |
Dependency |
DependencyObjectType取得包裝這個實例之 CLR 型別的 。 (繼承來源 DependencyObject) |
Dispatcher |
取得與這個 Dispatcher 關聯的 DispatcherObject。 (繼承來源 DispatcherObject) |
DpiX |
取得影像 (DPI) 的水準點。 (繼承來源 BitmapSource) |
DpiY |
取得影像 (DPI) 的垂直點。 (繼承來源 BitmapSource) |
Format |
取得點陣圖資料的原生 PixelFormat。 (繼承來源 BitmapSource) |
Has |
取得值,這個值表示是否有一個或多個 AnimationClock 物件與這個物件的任何一個相依性屬性相關聯。 (繼承來源 Animatable) |
Height |
取得裝置獨立單位中來源點陣圖的高度 (,每單位 1/96 英吋) 1/96 英吋。 (繼承來源 BitmapSource) |
Is |
取得值,這個值表示目前是否正在下載 BitmapSource 內容。 (繼承來源 BitmapSource) |
Is |
取得值,該值表示物件目前是否可修改。 (繼承來源 Freezable) |
Is |
取得值,這個值表示此執行個體目前是否已密封 (唯讀)。 (繼承來源 DependencyObject) |
Metadata |
取得與這個點陣圖影像相關聯的中繼資料 (Metadata)。 (繼承來源 BitmapSource) |
Palette |
取得點陣圖的色板 (如果有指定的話)。 (繼承來源 BitmapSource) |
Pixel |
取得點陣圖的高度 (以像素為單位)。 (繼承來源 BitmapSource) |
Pixel |
取得點陣圖的寬度 (以像素為單位)。 (繼承來源 BitmapSource) |
Width |
取得點陣圖的寬度,以與裝置無關的單位 (每單位 1/96 英吋) 。 (繼承來源 BitmapSource) |
Changed |
發生於 Freezable 或所含的物件遭到修改時。 (繼承來源 Freezable) |
Decode |
當影像因影像標頭損毀而無法載入時發生。 (繼承來源 BitmapSource) |
Download |
點陣圖內容下載完成時發生。 (繼承來源 BitmapSource) |
Download |
無法下載點陣圖內容時發生。 (繼承來源 BitmapSource) |
Download |
點陣圖內容的下載進度變更時發生。 (繼承來源 BitmapSource) |
IFormattable. |
使用指定的格式,格式化目前執行個體的值。 (繼承來源 ImageSource) |
產品 | 版本 |
---|---|
.NET Framework | 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
Windows Desktop | 3.0, 3.1, 5, 6, 7, 8, 9 |