Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is my function but it runs too slowly. Can anyone help me speeding it up?

void LayerShow(Color[,] layer)// Shows a layer in the panel.
{
for (int y = 0; y < CanvasDimY; y++)
{
for (int x = 0; x < CanvasDimX; x++)
{
image.SetPixel(x, y, layer[x, y]);
}
}
PanelCanvas.BackgroundImage = image;
PanelCanvas.Refresh();
}

What I have tried:

I have looked online for alternatives to SetPixel, and there seem to be some, but I have not been able to integrate the examples in my code. There is always something that you need to do to make it work, that they forget to mention.
Posted
Updated 20-Jun-20 9:53am

1 solution

You can get significant speed improvements by using unsafe code; in fact the unsafe SetPixel alternative is the prime example of LockBits and the like.

BTW googling faster SetPixel will yield many articles explaining it all...

:)
 
Share this answer
 
Comments
Member 14868736 20-Jun-20 16:44pm    
As I wrote, I have already searched on all these key words. I cant make it work. I need a rewrite of my tiny code example.
Luc Pattyn 20-Jun-20 16:58pm    
And what is the problem? Does your code compile without errors? Does it run at all? Be specific if you want something. If necessary, show us relevant code.

Yes making it much faster will require some rewriting, there is no magic bullet...

The official documentation in MSDN gives you everything you may need, such as https://docs.microsoft.com/en-us/dotnet/api/system.drawing.bitmap.lockbits?view=netframework-4.0

And this site offers some articles that explain it more, and make good use of it. This may interest you: https://www.codeproject.com/Articles/38619/Finding-a-Bitmap-contained-inside-another-Bitmap

:)
Member 14868736 21-Jun-20 4:07am    
I told you exactly what the problem is, shown my code and asked for a faster way to do exactly what my code does. There is no other problem involved. No errors, then I would have mentioned it of course.
[no name] 21-Jun-20 10:43am    
And he told you exactly - gave you the appropriate links - how to do it ;)
Member 14868736 21-Jun-20 16:05pm    
Sorry but that link is as useless to me as your comment. I need at rewrite of my code or close to it, since Im very new at coding C#.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900