0

In my application, whenever the user moves the mouse around, I have to re-create a new Bitmap with very high resolution ( It may reaches 3000 pixels x 3000 pixels at its biggest size). The size of the bitmap depends on where the mouse is. For example: the size is smallest when the mouse is on the top-left corner of the screen and is biggest when the mouse is on the bottom-right corner.

enter image description here

The problem is when the mouse is very far away from top-left corner, the size of the bitmap is very big. I checked and realize the time of re-creating and drawing bitmap by DrawingContext in OnRender is fine. However there may be a bottleneck in the framework, it takes long time to transfer the drawing data in DrawingContext to the screen. And this influences WPF mouse input system, so the next mouse move event is fired very late and my application becomes laggy. Here is another question on this site that is related to my problem: The reason behind slow performance in WPF

Do you have any idea to solve my problem?

PS: Whenever the user moves the mouse, I create a new bitmap and draw something on it, then call InvalidateVisual() and draw the bitmap into DrawingContext.

Community
  • 1
  • 1
  • Use a WriteableBitmap instead and call WritePixels or update its BackBuffer. Do not do anything else than that, i.e. no InvalidateVisual or OnRender. – Clemens Jul 28 '17 at 11:47
  • If you can, try to limit the size of the displayed bitmap to the size of the viewport, and only redraw what will actually be on screen. You will also get performance boosts from this if you can reuse the same bitmap over and over again by clearing it or drawing over the existing content. Allocating a new bitmap is expensive. – Bradley Uffner Jul 28 '17 at 13:54
  • @Clemens I would like to take advantage of some Drawing API in WPF such as DrawPolygon, FillPolygon,... If I use the WriteableBitmap and WritePixels, I have to implement these Drawing stuff myself. It takes some time to test fully and I am not sure about the performance. It is maybe slow because it is almost done 100% in software ( I have to scan each pixel and set RGB value to it one by one). Do you have any idea about this? Thanks. – Huyết Công Tử Aug 01 '17 at 04:23
  • @BradleyUffner Thank you for your advise. However, in my application, when the user uses Zoom-out button, the bitmap is fully seen and it is always in the viewport. – Huyết Công Tử Aug 01 '17 at 04:28
  • 1
    There is the WriteableBitmapEx library. – Clemens Aug 01 '17 at 06:14
  • @Clemens I tried WriteableBitmapEx library, but there is a problem. When I scale the bitmap area that is drawn by DrawPolygon and filled by FillPolygon, the border pixels ( created by DrawPolygon) and the content pixels (created by FillPolygon) don't match 100%. There are some pixels gaps inside the shape. Maybe the two functions are implemented with different polygon filling algorithms. – Huyết Công Tử Aug 01 '17 at 08:23

0 Answers0