private void OnRenderProgress(RenderProgressStep progressStep) { if (_bitmap == null) { throw new InvalidOperationException("Shouldn't be reporting progress when nothing is rendering."); } UpdateBitmap(progressStep); ReportProgress(progressStep.PercentComplete); }
/// <summary> /// Updates the bitmap with the cached updates. /// </summary> /// <param name="renderProgressStep">The update containing which pixels to set.</param> private void UpdateBitmap(RenderProgressStep renderProgressStep) { if (_bitmap == null) { throw new InvalidOperationException("Should not be processing updates when the bitmap is null."); } // Lock the bitmap so that we can do all of the updates at once. _bitmap.Lock(); try { int bytesPerPixel = _bitmap.Format.BitsPerPixel / 8; // Get a pointer to the back buffer for the corresponding row. IntPtr backBuffer = _bitmap.BackBuffer + (renderProgressStep.Row * _bitmap.BackBufferStride); // Loop through each pixel in the render progress step and set the corresponding color in the bitmap. foreach (Color color in renderProgressStep.Pixels) { // Convert the pixel's color to rgb format. int rgb = color.ToRgb(); // Set the pixel in memory unsafe { *(int *)backBuffer = rgb; } // Increment the buffer pointer backBuffer += bytesPerPixel; } // Calculate the update rectangle for the bitmap. var rect = new Int32Rect(0, renderProgressStep.Row, _bitmap.PixelWidth, 1); // Tell the bitmap what changed. _bitmap.AddDirtyRect(rect); } finally { // Unlock the bitmap so that it can be displayed in the UI. _bitmap.Unlock(); } }