コード例 #1
0
            /// <summary>
            /// Called when we've to draw the image.
            /// </summary>
            /// <param name="graphics">The graphics.</param>
            private void OnDraw(Graphics2D graphics)
            {
                if (IsDisposed) { throw new ObjectDisposedException("ImageDataFlowHelper"); }

                if (this.SyncBuffer == null) { return; }

                // Change bitmap contents if needed
                if (this.SyncBufferChanged)
                {
                    lock (this.SyncBufferLock)
                    {
                        // Recreate bitmap on need
                        if ((this.BitmapSize != this.SyncBufferSize) ||
                           (this.Bitmap == null))
                        {
                            CommonTools.SafeDispose(ref this.Bitmap);
                            this.Bitmap = new WriteableBitmapResource(
                                new Size2(this.SyncBufferSize.Width, this.SyncBufferSize.Height),
                                BitmapFormat.Bgra, AlphaMode.Ignore);
                            this.BitmapSize = this.SyncBufferSize;
                        }

                        // Write data from SyncBuffer to bitmap
                        this.Bitmap.SetBitmapContent(
                            graphics,
                            this.SyncBuffer.Pointer,
                            this.SyncBuffer.Pitch);
                    }
                }

                // Draw the bitmap on the screen
                if ((this.Bitmap != null) &&
                    (this.BitmapSize.Width > 0) &&
                    (this.BitmapSize.Height > 0))
                {
                    graphics.Clear(Color4.Transparent);

                    // Draw the current contents of the stream
                    RectangleF viewBounds = new RectangleF(
                        0f, 0f,
                        graphics.ScreenSize.Width, graphics.ScreenSize.Height);
                    graphics.DrawBitmap(this.Bitmap, viewBounds);

                    // Draw the stream's name
                    RectangleF titleBounds = new RectangleF(
                        viewBounds.Width / 2f - 100f,
                        10f,
                        200f, 30f);
                    graphics.FillRoundedRectangle(titleBounds, 5f, 5f, this.TextBackground);
                    graphics.DrawText(
                        this.StreamName, this.TextFormat,
                        titleBounds,
                        this.TextForeground);
                }
            }
コード例 #2
0
            /// <summary>
            /// Initializes a new instance of the <see cref="ImageDataFlowHelper"/> class.
            /// </summary>
            /// <param name="width">The width of the image.</param>
            /// <param name="height">The height of the image.</param>
            public ImageDataFlowHelper(string streamName)
            {
                this.StreamName = streamName;
                this.TextFormat = new TextFormatResource("Arial", 18f, FontWeight.Bold);
                this.TextFormat.TextAlignment = TextAlignment.Center;
                this.TextFormat.ParagraphAlignment = ParagraphAlignment.Center;
                this.TextBackground = new SolidBrushResource(Color4.LightGray.ChangeAlphaTo(0.7f));
                this.TextForeground = new SolidBrushResource(Color4.DarkBlue);

                // Initialize scene(s)
                this.Scene = new Scene();
                this.Scene.ManipulateSceneAsync((manipulator) =>
                {
                    manipulator.AddDrawingLayer(OnDraw);
                }).FireAndForget();

                // Initialize members for Direct2D rendering
                this.Bitmap = null;
                this.BitmapSize = new Size2(0, 0);

                // Initialize members for synchronization
                this.SyncBufferSize = new Size2(0, 0);
                this.SyncBuffer = null;
                this.SyncBufferLock = new object();
                this.SyncBufferChanged = false;
            }