1

I did some profiling on my project and traced a paint function to using 80% of my project's processing time. Since it's so significant I feel like any improvements I do would greatly impact my performance. This function takes roughly 25 ms to finish which is about 40 fps and I hope to get it up to 60.

private void pic2OnPaint(object sender, PaintEventArgs e)
{
    try
    {
        Rectangle rect = Rect();

        e.Graphics.DrawImage(LatestScreenshot, new Point(-rect.Location.X, -rect.Location.Y));

        if (PaintLayers.Count > 0 | ChosenTool != Option.None) // So it does not attempt to draw a blank bitmap.
        {
            e.Graphics.DrawImage(PaintSurface, new Point(-rect.Location.X, -rect.Location.Y));
        }

        if (!Holding & !PanelDragHolding & !HandleDragHolding) // So it does not attempt to draw a blank bitmap.
        {
            e.Graphics.DrawImage(invisibleLayer, new Point(-rect.Location.X, -rect.Location.Y));
        }       
    }
    catch { }
}

I would greatly appreciate any tips.

This gets called every frame. LatestScreenshot bitmap does not change. Paintsurface gets changed each frame. I put it in a try catch just because the only time it errors is once I initialize the project so LatestScreenshot is null for the first time it gets called. I do not get any errors other than that one.

Hello World
  • 168
  • 6
  • 2
    We don´t know your surroundings. Is the code called often (e.g. in a loop)? How big is it? Your question is quite unclear, there´s no general rule to "make a code fast". Anyway I can´t see much you could do here, but we can´t know what you actually *need* and which calls may or may not be obsolete, slow or whatever. – MakePeaceGreatAgain Sep 30 '17 at 18:42
  • 7
    You should never use `try catch` with empty `catch` block. You hide possible exceptions and remove one of the ways of debugging your application. – FCin Sep 30 '17 at 18:43
  • How did you determine that the code takes 25ms to execute? How large is that image? – Ňɏssa Pøngjǣrdenlarp Sep 30 '17 at 18:45
  • The image is the size of my screen on a low end computer. – Hello World Sep 30 '17 at 18:48
  • Firstly get rid of the try catch. Do not create a Rectangle and Point object every call. If possible use one object and reuse it over and over again. – Bin4ry Sep 30 '17 at 18:53
  • Try to draw everithing to a bitmap and then draw that to the screen. Also, instead of try ..catch simply check if the latestScreenshot is not null before drawing it. – Zohar Peled Sep 30 '17 at 19:09
  • If we have this sample of your code for review only, there is no room for any improvements. If so, it is time to change approach. Please take a look [here](https://stackoverflow.com/questions/11020710/is-graphics-drawimage-too-slow-for-bigger-images). It should navigate you in some other directions. – Vladimir.RL Sep 30 '17 at 19:21
  • You might consider moving to lower-level drawing APIs like ActiveX or Direct2D. – Dour High Arch Sep 30 '17 at 20:12
  • 2
    You should not redraw the whole image, but only the part that fits inside the `e.ClipRectangle`.. – Alexander Petrov Sep 30 '17 at 20:45
  • Thank you Alexander, I forgot that invalidate could do that. – Hello World Oct 01 '17 at 01:48

0 Answers0