首页 > 其他分享 >WPF使用WriteableBitmap更新图像

WPF使用WriteableBitmap更新图像

时间:2023-01-11 15:00:18浏览次数:35  
标签:dates System new WriteableBitmap length 图像 using wbBitmap WPF

 

<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

相关文章

  • 数字图像处理笔记
    一,绪论1.1,什么是数字图像处理1.2,数字图像处理的起源1.3,数字图像处理技术应用实例1.4,数字图像处理的基本步骤1.5,图像处理系统的组成二,数字图像基础2.1,视觉感......
  • 02 图像色彩空间转换
    02图像色彩空间转换opencv知识点:色彩空间转换函数-cvtColor()图像保存-imwrite()图像显示-imshow()本课解决的问题:如何对图片进行色彩空间转换?如何保存图像?......
  • JavaScript 图像压缩
    JavaScript可以使用类似于canvas和webworkers来实现图像压缩。使用canvas,可以将图像绘制到canvas上,然后使用canvas提供的toBlob()或toDataURL()方法将其转......
  • 04-VGG16 图像分类
       图1VGG的网络结构  图2VGG16的网络 VGG网络结构的理解,参考:https://blog.csdn.net/Keep_Trying_Go/article/details/123943751Cifar10的vgg16网络pyto......
  • 02-Resnet18 图像分类
     图1Resnet的残差块   图2Resnet18网络架构Cifar10数据集的Resnet10的框架实现(Pytorch):1importtorch2fromtorchimportnn34#ResNet18_Bas......
  • 02-Lenet5 图像分类网络
      图1Lenet5手写字符分类网络架构Cifar10数据集的Lenet5的框架实现(Pytorch):1importtorch2fromtorchimportnn,optim3importtorch.nn.functional......
  • matlab的gui图像处理操作界面,实现坐标轴重置界面
    1.首先建立gui界面,具体过程在这里不做赘述。   2.然后将坐标轴所要显示的曲线放到对应的位置。按下开始按键,即可在相应位置显示曲线   functionpush_beg......
  • WPF-TreeView行选中效果
    privatevoidtrvw_MouseDown(objectsender,MouseButtonEventArgse){if(e.SourceisTreeViewItemtreeViewItem){......
  • m基于QPSK调制解调的无线图像传输matlab仿真,包括扩频解扩均衡等模块
    1.算法描述       软件无线电在无线通信领域被称为是自模拟通信过渡到数字通信之后的又一次革命,在军用和民用方面都有着广阔的应用。它是一种新的无线通信技术,基于......
  • m基于QPSK调制解调的无线图像传输matlab仿真,包括扩频解扩均衡等模块
    1.算法描述软件无线电在无线通信领域被称为是自模拟通信过渡到数字通信之后的又一次革命,在军用和民用方面都有着广阔的应用。它是一种新的无线通信技术,基于通用的可编程的......