Suche…


Parameter

Parameter Einzelheiten
PixelHeight (System.Int32) Die Höhe des Bildes in Einheiten von Bildpixeln
PixelWidth (System.Int32) Die Breite des Bildes in Einheiten von Bildpixeln
PixelFormat (System.Windows.Media.PixelFormat) Die Breite des Bildes in Einheiten von Bildpixeln
Pixel Alles, was IList <T> implementiert - einschließlich des C # -Byte-Arrays
DpiX Gibt die horizontale Dpi an - optional
DpiY Gibt die vertikale Dpi an - optional

Bemerkungen

  • Stellen Sie sicher, dass Sie auf die System.Windows.Interactivity-Assembly verweisen, damit der XAML-Parser die xmlns: i- Deklaration erkennt.
  • Beachten Sie, dass die Anweisung xmlns: b mit dem Namespace übereinstimmt, in dem sich die Verhaltensimplementierung befindet
  • Das Beispiel setzt Kenntnisse in Bezug auf Bindungsausdrücke und XAML voraus.
  • Dieses Verhalten unterstützt das Zuweisen von Pixeln zu einem Bild in Form eines Bytearrays - auch wenn der Typ der Abhängigkeitseigenschaft als IList angegeben ist . Dies funktioniert, da das C # -Byte- Array die IList implementiert
  • Schnittstelle.
  • Das Verhalten erzielt eine sehr hohe Leistung und kann für das Videostreaming verwendet werden
  • Weisen Sie stattdessen nicht der Source Dependency Property-Bindung des Bilds der Pixels Dependency Property zu
  • Die Eigenschaften Pixel, PixelWidth, PixelHeight und PixelFormat müssen für die Darstellung der Pixel zugewiesen werden
  • Reihenfolge der Abhängigkeit Die Zuordnung von Immobilien spielt keine Rolle

Verhaltensimplementierung

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interactivity;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace MyBehaviorAssembly
{
    public class PixelSupportBehavior : Behavior<Image>
    {

    public static readonly DependencyProperty PixelsProperty = DependencyProperty.Register(
        "Pixels", typeof (IList<byte>), typeof (PixelSupportBehavior), new PropertyMetadata(default(IList<byte>),OnPixelsChanged));

    private static void OnPixelsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var b = (PixelSupportBehavior) d;
        var pixels = (IList<byte>) e.NewValue;

        b.RenderPixels(pixels);
    }
 
    public IList<byte> Pixels
    {
        get { return (IList<byte>) GetValue(PixelsProperty); }
        set { SetValue(PixelsProperty, value); }
    }

    public static readonly DependencyProperty PixelFormatProperty = DependencyProperty.Register(
        "PixelFormat", typeof (PixelFormat), typeof (PixelSupportBehavior), new PropertyMetadata(PixelFormats.Default,OnPixelFormatChanged));

   
    private static void OnPixelFormatChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var b = (PixelSupportBehavior) d;
        var pixelFormat = (PixelFormat) e.NewValue;

        if(pixelFormat==PixelFormats.Default)
            return;

        b._pixelFormat = pixelFormat;

        b.InitializeBufferIfAttached();
    }

    public PixelFormat PixelFormat
    {
        get { return (PixelFormat) GetValue(PixelFormatProperty); }
        set { SetValue(PixelFormatProperty, value); }
    }

    public static readonly DependencyProperty PixelWidthProperty = DependencyProperty.Register(
        "PixelWidth", typeof (int), typeof (PixelSupportBehavior), new PropertyMetadata(default(int),OnPixelWidthChanged));

    private static void OnPixelWidthChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var b = (PixelSupportBehavior)d;
        var value = (int)e.NewValue;

        if(value<=0)
            return;

        b._pixelWidth = value;

        b.InitializeBufferIfAttached();
    }

    public int PixelWidth
    {
        get { return (int) GetValue(PixelWidthProperty); }
        set { SetValue(PixelWidthProperty, value); }
    }


    public static readonly DependencyProperty PixelHeightProperty = DependencyProperty.Register(
        "PixelHeight", typeof (int), typeof (PixelSupportBehavior), new PropertyMetadata(default(int),OnPixelHeightChanged));

    private static void OnPixelHeightChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var b = (PixelSupportBehavior)d;
        var value = (int)e.NewValue;

        if (value <= 0)
            return;

        b._pixelHeight = value;

        b.InitializeBufferIfAttached();
    }

    public int PixelHeight
    {
        get { return (int) GetValue(PixelHeightProperty); }
        set { SetValue(PixelHeightProperty, value); }
    }

    public static readonly DependencyProperty DpiXProperty = DependencyProperty.Register(
        "DpiX", typeof (int), typeof (PixelSupportBehavior), new PropertyMetadata(96,OnDpiXChanged));

    private static void OnDpiXChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var b = (PixelSupportBehavior)d;
        var value = (int)e.NewValue;

        if (value <= 0)
            return;

        b._dpiX = value;

        b.InitializeBufferIfAttached();
    }

    public int DpiX
    {
        get { return (int) GetValue(DpiXProperty); }
        set { SetValue(DpiXProperty, value); }
    }

    public static readonly DependencyProperty DpiYProperty = DependencyProperty.Register(
        "DpiY", typeof (int), typeof (PixelSupportBehavior), new PropertyMetadata(96,OnDpiYChanged));

    private static void OnDpiYChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var b = (PixelSupportBehavior)d;
        var value = (int)e.NewValue;

        if (value <= 0)
            return;

        b._dpiY = value;

        b.InitializeBufferIfAttached();
    }

    public int DpiY
    {
        get { return (int) GetValue(DpiYProperty); }
        set { SetValue(DpiYProperty, value); }
    }

    private IntPtr _backBuffer = IntPtr.Zero;
    private int _bytesPerPixel;
    private const int BitsPerByte = 8;
    private int _pixelWidth;
    private int _pixelHeight;
    private int _dpiX;
    private int _dpiY;
    private Int32Rect _imageRectangle;
    private readonly GCHandle _defaultGcHandle = new GCHandle();
    private PixelFormat _pixelFormat;

    private int _byteArraySize;
    private WriteableBitmap _bitMap;

    private bool _attached;

    protected override void OnAttached()
    {
        _attached = true;
        InitializeBufferIfAttached();
    }

    private void InitializeBufferIfAttached()
    {
        if(_attached==false)
            return;

        ReevaluateBitsPerPixel();
        
        RecomputeByteArraySize();

        ReinitializeImageSource();
    }

    private void ReevaluateBitsPerPixel()
    {
        var f = _pixelFormat;

        if (f == PixelFormats.Default)
        {
            _bytesPerPixel = 0;
            return;
        };

        _bytesPerPixel = f.BitsPerPixel/BitsPerByte;
    }

    private void ReinitializeImageSource()
    {
        var f = _pixelFormat;
        var h = _pixelHeight;
        var w = _pixelWidth;

        if (w<=0|| h<=0 || f== PixelFormats.Default)
            return;

        _imageRectangle = new Int32Rect(0, 0, w, h);
        _bitMap = new WriteableBitmap(w, h, _dpiX, _dpiY, f, null);
        _backBuffer = _bitMap.BackBuffer;
        AssociatedObject.Source = _bitMap;
    }

    private void RenderPixels(IList<byte> pixels)
    {
        if (pixels == null)
        {
            return;
        }

        var buffer = _backBuffer;
        if (buffer == IntPtr.Zero)
            return;

        var size = _byteArraySize;

        var gcHandle = _defaultGcHandle;
        var allocated = false;
        var bitMap = _bitMap;
        var rect = _imageRectangle;
        var w = _pixelWidth;
        var locked = false;
        try
        {
            gcHandle = GCHandle.Alloc(pixels, GCHandleType.Pinned);
            allocated = true;

            bitMap.Lock();
            locked = true;
            var ptr = gcHandle.AddrOfPinnedObject();
            _bitMap.WritePixels(rect, ptr, size,w);                            
        }
        finally
        {
            if(locked)
                bitMap.Unlock();

            if (allocated)
                gcHandle.Free();
        }
    }

    private void RecomputeByteArraySize()
    {
        var h = _pixelHeight;
        var w = _pixelWidth;
        var bpp = _bytesPerPixel;

        if (w<=0|| h<=0 || bpp<=0)
            return;

        _byteArraySize = (w * h * bpp);
    }

    public PixelSupportBehavior()
    {
        _pixelFormat = PixelFormats.Default;
    }
}
}

XAML-Nutzung

<UserControl x:Class="Example.View"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:b="clr-namespace:MyBehaviorAssembly;assembly=MyBehaviorAssembly"
         xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
         mc:Ignorable="d" 
         d:DesignHeight="200" d:DesignWidth="200"                    
         >

                <Image Stretch="Uniform">             

                <i:Interaction.Behaviors>

                    <b:PixelSupportBehavior 
                            PixelHeight="{Binding PixelHeight}"
                            PixelWidth="{Binding PixelWidth}"
                            PixelFormat="{Binding PixelFormat}"
                            Pixels="{Binding Pixels}"
                            />

                </i:Interaction.Behaviors>

            </Image>

</UserControl>


Modified text is an extract of the original Stack Overflow Documentation
Lizenziert unter CC BY-SA 3.0
Nicht angeschlossen an Stack Overflow