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
繼承

範例

下列範例示範如何在 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 一個多載,以自動更新和顯示後端緩衝區中的內容。

若要進一步控制更新,以及對後端緩衝區的多執行緒存取,請使用下列工作流程。

  1. Lock呼叫 方法以保留後端緩衝區以進行更新。

  2. 藉由存取 屬性來取得後端緩衝區的 BackBuffer 指標。

  3. 將變更寫入背景緩衝區。 鎖定時 WriteableBitmap ,其他執行緒可能會將變更寫入背景緩衝區。

  4. AddDirtyRect呼叫 方法,以指出已變更的區域。

  5. Unlock呼叫 方法以釋放背景緩衝區,並允許簡報到畫面。

當更新傳送至轉譯執行緒時,轉譯執行緒會將變更的矩形從背景緩衝區複製到前端緩衝區。 轉譯系統會控制此交換,以避免死結和重繪成品,例如「卸載」。

建構函式

WriteableBitmap(BitmapSource)

使用指定的 WriteableBitmap,初始化 BitmapSource 類別的新執行個體。

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

使用指定的參數,初始化 WriteableBitmap 類別的新執行個體。

屬性

BackBuffer

取得指向背景緩衝區的指標。

BackBufferStride

取得值,這個值指出單列像素資料中的位元組數。

CanFreeze

取得值,指出是否可以將物件設為不可修改。

(繼承來源 Freezable)
DependencyObjectType

DependencyObjectType取得包裝這個實例之 CLR 型別的 。

(繼承來源 DependencyObject)
Dispatcher

取得與這個 Dispatcher 關聯的 DispatcherObject

(繼承來源 DispatcherObject)
DpiX

取得影像 (DPI) 的水準點。

(繼承來源 BitmapSource)
DpiY

取得影像 (DPI) 的垂直點。

(繼承來源 BitmapSource)
Format

取得點陣圖資料的原生 PixelFormat

(繼承來源 BitmapSource)
HasAnimatedProperties

取得值,這個值表示是否有一個或多個 AnimationClock 物件與這個物件的任何一個相依性屬性相關聯。

(繼承來源 Animatable)
Height

取得裝置獨立單位中來源點陣圖的高度 (,每單位 1/96 英吋) 1/96 英吋。

(繼承來源 BitmapSource)
IsDownloading

取得值,這個值表示目前是否正在下載 BitmapSource 內容。

(繼承來源 BitmapSource)
IsFrozen

取得值,該值表示物件目前是否可修改。

(繼承來源 Freezable)
IsSealed

取得值,這個值表示此執行個體目前是否已密封 (唯讀)。

(繼承來源 DependencyObject)
Metadata

取得與這個點陣圖影像相關聯的中繼資料 (Metadata)。

(繼承來源 BitmapSource)
Palette

取得點陣圖的色板 (如果有指定的話)。

(繼承來源 BitmapSource)
PixelHeight

取得點陣圖的高度 (以像素為單位)。

(繼承來源 BitmapSource)
PixelWidth

取得點陣圖的寬度 (以像素為單位)。

(繼承來源 BitmapSource)
Width

取得點陣圖的寬度,以與裝置無關的單位 (每單位 1/96 英吋) 。

(繼承來源 BitmapSource)

方法

AddDirtyRect(Int32Rect)

指定所變更之點陣圖的區域。

ApplyAnimationClock(DependencyProperty, AnimationClock)

AnimationClock 套用至指定的 DependencyProperty。 如果屬性已有動畫效果,即使用 SnapshotAndReplace 遞移式行為。

(繼承來源 Animatable)
ApplyAnimationClock(DependencyProperty, AnimationClock, HandoffBehavior)

AnimationClock 套用至指定的 DependencyProperty。 如果已建立屬性的動畫,則會使用指定的 HandoffBehavior

(繼承來源 Animatable)
BeginAnimation(DependencyProperty, AnimationTimeline)

將動畫套用至指定的 DependencyProperty。 呈現下一個畫面格後,就會啟動動畫。 如果指定的屬性已有動畫效果,即使用 SnapshotAndReplace 遞移式行為。

(繼承來源 Animatable)
BeginAnimation(DependencyProperty, AnimationTimeline, HandoffBehavior)

將動畫套用至指定的 DependencyProperty。 呈現下一個畫面格後,就會啟動動畫。 如果已建立指定之屬性的動畫,則會使用指定的 HandoffBehavior

(繼承來源 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)

強制轉型所指定相依性屬性的值。 完成方式是叫用存在於呼叫 DependencyObject 之相依性屬性的屬性中繼資料中所指定的任何 CoerceValueCallback 函式。

(繼承來源 DependencyObject)
CopyPixels(Array, Int32, Int32)

將點陣圖像素資料複製到具有所指定步距 的像素陣列 (從指定的位移 (Offset) 開始)。

(繼承來源 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)

確定已為剛剛設定的 DependencyObjectType 資料成員,建立適當的內容指標。

(繼承來源 Freezable)
OnFreezablePropertyChanged(DependencyObject, DependencyObject, DependencyProperty)

此成員支援Windows Presentation Foundation (WPF) 基礎結構,而且不適合直接從程式碼使用。

(繼承來源 Freezable)
OnPropertyChanged(DependencyPropertyChangedEventArgs)

覆寫 OnPropertyChanged(DependencyPropertyChangedEventArgs)DependencyObject 實作也可以叫用任何 Changed 處理常式,以回應類型 Freezable 的變更相依性屬性。

(繼承來源 Freezable)
ReadLocalValue(DependencyProperty)

傳回相依性屬性的區域值 (如果存在)。

(繼承來源 DependencyObject)
ReadPreamble()

確定 Freezable 是從有效的執行緒進行存取。 如果 API 會讀取非相依性屬性的資料成員,則 Freezable 的繼承者必須在該 API 的開頭呼叫這個方法。

(繼承來源 Freezable)
SetCurrentValue(DependencyProperty, Object)

設定相依性屬性的值,而不需要變更其值來源。

(繼承來源 DependencyObject)
SetValue(DependencyProperty, Object)

設定相依性屬性的區域值 (由相依性屬性的識別碼所指定)。

(繼承來源 DependencyObject)
SetValue(DependencyPropertyKey, Object)

設定唯讀相依性屬性的區域數值 (由相依性屬性的 DependencyPropertyKey 識別項所指定)。

(繼承來源 DependencyObject)
ShouldSerializeProperty(DependencyProperty)

傳回值,這個值表示序列化程序是否應該序列化所提供相依性屬性的值。

(繼承來源 DependencyObject)
ToString()

根據目前的文化特性,建立這個物件的字串表示。

(繼承來源 ImageSource)
ToString(IFormatProvider)

根據傳入的 IFormatProvider,建立這個物件的字串表示。 如果提供者為 null,則會使用 CurrentCulture

(繼承來源 ImageSource)
TryLock(Duration)

嘗試鎖定該點陣圖,並等待不超過指定長度的時間。

Unlock()

釋放背景緩衝區以供顯示使用。

VerifyAccess()

請強制執行可以存取這個 DispatcherObject 的呼叫執行緒。

(繼承來源 DispatcherObject)
WritePixels(Int32Rect, Array, Int32, Int32)

更新指定之點陣圖區域中的像素。

WritePixels(Int32Rect, Array, Int32, Int32, Int32)

更新指定之點陣圖區域中的像素。

WritePixels(Int32Rect, IntPtr, Int32, Int32)

更新指定之點陣圖區域中的像素。

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

更新指定之點陣圖區域中的像素。

WritePostscript()

引發 FreezableChanged 事件,並叫用其 OnChanged() 方法。 在任何 API 修改未以相依性屬性儲存的類別成員之後,衍生自 Freezable 的類別應該在 API 的結尾呼叫這個方法。

(繼承來源 Freezable)
WritePreamble()

確認 Freezable 未凍結,而且是從有效的執行緒內容進行存取。 在任何 API 將資料寫入至非相依性屬性的資料成員之前,Freezable 繼承者應該在 API 的開頭呼叫這個方法。

(繼承來源 Freezable)

事件

Changed

發生於 Freezable 或所含的物件遭到修改時。

(繼承來源 Freezable)
DecodeFailed

當影像因影像標頭損毀而無法載入時發生。

(繼承來源 BitmapSource)
DownloadCompleted

點陣圖內容下載完成時發生。

(繼承來源 BitmapSource)
DownloadFailed

無法下載點陣圖內容時發生。

(繼承來源 BitmapSource)
DownloadProgress

點陣圖內容的下載進度變更時發生。

(繼承來源 BitmapSource)

明確介面實作

IFormattable.ToString(String, IFormatProvider)

使用指定的格式,格式化目前執行個體的值。

(繼承來源 ImageSource)

適用於

另請參閱