WPF使用WriteableBitmap更新图像

<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); } }); } } }
更新大图可以把图像分开使用多任务更新

using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace WpfApp2 { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { private WriteableBitmap _wbBitmap1; private WriteableBitmap _wbBitmap2; private WriteableBitmap _wbBitmap3; private WriteableBitmap _wbBitmap4; int width = 8000; int height = 8000; int length = 0; short[] dates; short[] date1; short[] date2; short[] date3; short[] date4; public MainWindow() { InitializeComponent(); length = width * height; _wbBitmap1 = new WriteableBitmap(width / 4, height / 4, 96, 96, PixelFormats.Gray16, null); Img1.Source = _wbBitmap1; _wbBitmap2 = new WriteableBitmap(width / 4, height / 4, 96, 96, PixelFormats.Gray16, null); Img2.Source = _wbBitmap2; _wbBitmap3 = new WriteableBitmap(width / 4, height / 4, 96, 96, PixelFormats.Gray16, null); Img3.Source = _wbBitmap3; _wbBitmap4 = new WriteableBitmap(width / 4, height / 4, 96, 96, PixelFormats.Gray16, null); Img4.Source = _wbBitmap4; dates = new short[length]; date1 = new short[width / 4 * height / 4]; date2 = new short[width / 4 * height / 4]; date3 = new short[width / 4 * height / 4]; date4 = new short[width / 4 * height / 4]; } public void ShowImage1(short[] rawData) { unsafe { _wbBitmap1.Lock(); Marshal.Copy(rawData, 0, _wbBitmap1.BackBuffer, width / 4 * height / 4); _wbBitmap1.AddDirtyRect(new System.Windows.Int32Rect(0, 0, width / 4, height / 4)); _wbBitmap1.Unlock(); } } public void ShowImage2(short[] rawData) { unsafe { _wbBitmap2.Lock(); Marshal.Copy(rawData, 0, _wbBitmap2.BackBuffer, width / 4 * height / 4); _wbBitmap2.AddDirtyRect(new System.Windows.Int32Rect(0, 0, width / 4, height / 4)); _wbBitmap2.Unlock(); } } public void ShowImage3(short[] rawData) { unsafe { _wbBitmap3.Lock(); Marshal.Copy(rawData, 0, _wbBitmap3.BackBuffer, width / 4 * height / 4); _wbBitmap3.AddDirtyRect(new System.Windows.Int32Rect(0, 0, width / 4, height / 4)); _wbBitmap3.Unlock(); } } public void ShowImage4(short[] rawData) { unsafe { _wbBitmap4.Lock(); Marshal.Copy(rawData, 0, _wbBitmap4.BackBuffer, width / 4 * height / 4); _wbBitmap4.AddDirtyRect(new System.Windows.Int32Rect(0, 0, width / 4, height / 4)); _wbBitmap4.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);// 逆转数组 Thread.Sleep(100); } }); Task task1 = Task.Run(() => { while (true) { Array.Copy(dates, date1, width / 4 * height / 4); Img1.Dispatcher.BeginInvoke(new Action(() => { ShowImage1(date1); })); Thread.Sleep(100); } }); Task task2 = Task.Run(() => { while (true) { Array.Copy(dates, width / 4 * height / 4, date2,0, width / 4 * height / 4); Img2.Dispatcher.BeginInvoke(new Action(() => { ShowImage2(date2); })); Thread.Sleep(100); } }); Task task3 = Task.Run(() => { while (true) { Array.Copy(dates, width / 4 * height / 4, date3, 0, width / 4 * height / 4); Img3.Dispatcher.BeginInvoke(new Action(() => { ShowImage3(date3); })); Thread.Sleep(100); } }); Task task4 = Task.Run(() => { while (true) { Array.Copy(dates, width / 4 * height / 4, date4, 0, width / 4 * height / 4); Img4.Dispatcher.BeginInvoke(new Action(() => { ShowImage4(date4); })); Thread.Sleep(100); } }); } } }

<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> <Grid Grid.Row="1"> <Grid.RowDefinitions> <RowDefinition Height="*"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"/> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <Image Name="Img1" Grid.Row="0" HorizontalAlignment="Right" VerticalAlignment="Bottom" /> <Image Name="Img2" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Bottom"/> <Image Name="Img3" Grid.Row="1" HorizontalAlignment="Right" VerticalAlignment="Top"/> <Image Name="Img4" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Top"/> </Grid> </Grid> </Window>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
· AI 智能体引爆开源社区「GitHub 热点速览」