c#.net display image

时间:2018-06-18 09:41:26

标签: c# wpf

我试图显示2张图像,但结果只显示了1张图像。我的代码出了什么问题?或者有什么我应该知道做正确的工作或我错过了什么?感谢。

PS。我几乎是第一次编程,我对c#非常陌生。所以非常详细的解释将非常有帮助和赞赏。还请写下答案代码来解决这个问题。谢谢

这是我的代码。

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;
using System.Threading;

namespace WpfApp1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            byte[] bitmapArray = new byte[2046 * 2046 * 3];



            for (int i = 0; i < 12558348; i++)
            {
                bitmapArray[i] = 127;
             }

            PixelFormat pf = PixelFormats.Bgr24;
            BitmapSource bitmap = BitmapSource.Create(2046, 2046, 96, 96, pf, null, bitmapArray, 2046 * 3);
            BaslerImage.Source = bitmap;

            Nothing(bitmapArray, pf);

        }

        async void Nothing(byte[] bitmapArray, PixelFormat pf)
        {
            for (int i = 0; i < 12558348; i++)
            {

                bitmapArray[i] = 30;

            }

            PixelFormat pf2 = PixelFormats.Bgr24;
            BitmapSource bitmap2 = BitmapSource.Create(2046, 2046, 96, 96, pf, null, bitmapArray, 2046 * 3);
            BaslerImage.Source = bitmap2;

            await Task.Delay(10);
        }
    }
}

1 个答案:

答案 0 :(得分:-1)

我相信您的任务是通过更新Image.Source属性在系列上显示多个图像。请参考下面的代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;
using System.Threading;

namespace WpfApp1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            Nothing();
        }

        async void Nothing()
        {
            byte[] bitmapArray = new byte[2046 * 2046 * 3];

            byte a = 30;
            int b = 80;
            var k = a * 2 + b;

            for(int j =0; j < 10; j++)
            { 
                for (int i = 0; i < 12558348; i++)
                {
                    bitmapArray[i] = (byte)(63 + j*10);
                }
                PixelFormat pf = PixelFormats.Bgr24;
                BitmapSource bitmap = BitmapSource.Create(2046, 2046, 96, 96, pf, null, bitmapArray, 2046 * 3);
                BaslerImage.Source = bitmap;
                await Task.Delay(1000);
            }
        }
    }
}