Ejemplo n.º 1
0
        /// <summary>
        /// Updates the image in the picture box from the Pi system.
        /// Image must be available in the Pi system when this method is executing.
        /// </summary>
        public void UpdateImage()
        {
            if (PiImage != null)
            {
                long          w = 0;
                long          h = 0;
                long          d = 0;
                ImageDataType dt;
                IntPtr        data = PiImage.GetData(out w, out h, out d, out dt);

                if (w > int.MaxValue)
                {
                    throw new InvalidOperationException("Image width is too large to be shown.");
                }
                if (h > int.MaxValue)
                {
                    throw new InvalidOperationException("Image height is too large to be shown.");
                }
                if (d > int.MaxValue)
                {
                    throw new InvalidOperationException("Image depth is too large to be shown.");
                }

                CopyFromPointerToTemp(data, (int)w, (int)h, (int)d, dt);
            }
            else
            {
                CopyFromPointerToTemp(IntPtr.Zero, 0, 0, 0, ImageDataType.UInt8);
            }
            UpdateScreen();
        }
Ejemplo n.º 2
0
        private void CopyFromPiTask(CancellationToken cancellationToken)
        {
            try
            {
                if (PiImage != null)
                {
                    // NOTE: This can be used for testing the task system responsiveness
                    //for (int n = 0; n < 100; n++)
                    //{
                    //    cancellationToken.ThrowIfCancellationRequested();
                    //    Thread.Sleep(1);
                    //}

                    long          w = 0;
                    long          h = 0;
                    long          d = 0;
                    ImageDataType dt;
                    IntPtr        data = PiImage.GetData(out w, out h, out d, out dt);

                    cancellationToken.ThrowIfCancellationRequested();

                    TemporaryBitmapData.CopyFromPointerToTemp(data, w, h, d, dt, Slice, cancellationToken);
                }
                else
                {
                    TemporaryBitmapData.CopyFromPointerToTemp(IntPtr.Zero, 0, 0, 0, ImageDataType.UInt8, Slice, cancellationToken);
                }

                cancellationToken.ThrowIfCancellationRequested();

                // Swap the bitmaps in the UI thread
                sync.Post(new SendOrPostCallback(o =>
                {
                    // NOTE: Here we are in the UI thread but a new loading task might have started before we get here.
                    // Proceed only if a new loading task has not been started, i.e. the token source is the same than
                    // what it was when this task was started.
                    if (canc != null && canc.Token == cancellationToken)
                    {
                        var temp            = ActiveBitmapData;
                        ActiveBitmapData    = TemporaryBitmapData;
                        TemporaryBitmapData = temp;
                        UpdateScreen();
                        if (ImageLoaded != null)
                        {
                            ImageLoaded(this, EventArgs.Empty);
                        }
                    }
                }), new object());
            }
            catch (TaskCanceledException)
            {
                throw;
            }
        }