Why my Image updates from top to bottom row by row

89 Views Asked by At

I have a WPF application with Image control in it. I'm using WriteableBitmap to update Image.source, but I can't understand why I see this strange behaviour: my image breaks in two parts and top part is slowly moving to bottom in cycle at pretty slow rate. enter image description here I dont understand why this is happens. Top and bottom parts are actually the same frame, because I can see real-time updates in both of them.

My code:

    public MainWindow()
    {
        InitializeComponent();
        InitVideo();
        image.Source = frame;
    }

    private void InitVideo
    {
        /// .... some other init stuff ... 
        frame = new WriteableBitmap(width, height, 96, 96, PixelFormats.Rgb24, null);
        rgbch = new byte[stride * height];
        dataProc = new System.Threading.Thread(ReadData);
        dataProc.Start();
    }

    // in separate thread
    private void ReadData()
    {
        while (rxVideo)
        {
            for (int i = 0; i < rgbch.Length; i += stride)
            {
                pipe.Read(rgbch, i, stride);
            }

            Application.Current.Dispatcher.Invoke(() =>
           {
               frame.Lock();
               frame.WritePixels(new Int32Rect(0, 0, width, height), rgbch, stride, 0);
               frame.Unlock();
           });
     }

I tried to use frame.dispatcher.invoke -> same result. Tried Marshal.Copy -> same result..

2

There are 2 best solutions below

0
Aleksey On BEST ANSWER

I've found source of a problem.

It was cause by my code inside thread

        for (int i = 0; i < rgbch.Length; i += stride)
        {
            pipe.Read(rgbch, i, stride);
        }

rgbch was set as a source for writablebitmap backbuffer, so when I wrote new data in it, update worked slow, so I got that strange top-bottom update. I just did pipe.read(rgbch, 0, rgbch.Length) and it all worked faster without any borders in image.

1
Hossein Abedi On

It's almost surly not relevant to your code. It can be because of:

  • Very large image size(maybe 100s of MB)
  • Network low bandwidth
  • Weak graphic card

You should seek the reason of displaying image row by row in years ago. In those years the internet bandwidth was really low and this was a technique that let's user to see the image before loading completely. I know you know this. I just wrote it to make the answer complete!