<Window x:Class="WpfApp2.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApp2" mc:Ignorable="d" Title="MainWindow" Height="400" Width="400"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="50"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <Button x:Name="Btn" Content="更新图像" Click="BtnClkUpdateImage"></Button> <Image Name="Img" Grid.Row="1"/> </Grid> </Window>
using System; using System.Runtime.InteropServices; using System.Threading; using System.Threading.Tasks; using System.Windows; using System.Windows.Media; using System.Windows.Media.Imaging; namespace WpfApp2 { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { private WriteableBitmap _wbBitmap; int width = 1000; int height = 1000; int length = 0; short[] dates; public MainWindow() { InitializeComponent(); length = width * height; _wbBitmap = new WriteableBitmap(width, height, 96, 96, PixelFormats.Gray16, null); Img.Source = _wbBitmap; dates = new short[length]; } public void ShowImage(short[] rawData) { unsafe { _wbBitmap.Lock(); Marshal.Copy(rawData, 0, _wbBitmap.BackBuffer, length); _wbBitmap.AddDirtyRect(new System.Windows.Int32Rect(0, 0, width, height)); _wbBitmap.Unlock(); } } private void BtnClkUpdateImage(object sender, RoutedEventArgs e) { //随机产生图像数据 Random ran; for (int i = 0; i < length; i++) { ran = new Random(); dates[i] = (short)ran.Next(65535); } Btn.IsEnabled = false; Task task = Task.Run(() => { while (true) { Array.Reverse(dates);// 逆转数组 Img.Dispatcher.Invoke(new Action(() => { ShowImage(dates); })); Thread.Sleep(1); } }); } } }
标签:dates,System,new,WriteableBitmap,length,图像,using,wbBitmap,WPF From: https://www.cnblogs.com/lizhiqiang0204/p/17043782.html