2

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..

Aleksey
  • 115
  • 7

2 Answers2

1

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.

Aleksey
  • 115
  • 7
0

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!

  • Well, I rewrote my code to display small image (640x480). Result is same. As it looks like both of my "frames" (top and bottom) are the same one, its not like in old times when new one is slowly overwriting old one.. I think my problem is caused by bad code in thread, that updates frame. But I dont know what I should do to correct it. – Aleksey Sep 01 '16 at 08:48