StreamDeckSharp: Problem with fast draws.
Hi Christian,
i have a problem related to your project, but surely not caused by your project.
I try to compose pre loaded Images with different transformations (i.e. rotations, scales, translations) into one Bitmap using a Graphics Class inside a Timer-thread.
Problem is, that i can’t access the images in a thread, but have to invoke the UI-Thread via a delegate to do so. But the UI-Thread is only checked every 500 ms, which is way too slow for me.
public void mydraw()
{
if (Thread.CurrentThread != myUIthred) //Tell the UI thread to invoke mydraw if its not himself.
{
BeginInvoke(myaction);
return;
}
tempg.Clear(Color.Black); // tempg is created elsewhere: tempg = Graphics.FromImage(result);
tempg.TranslateTransform(1024, 1024);
tempg.RotateTransform((float)angle);
tempg.TranslateTransform(-1024, -1024);
tempg.DrawImage(back, 0, 0); // back, middle, ring and over are preloaded images
tempg.DrawImage(middle, 0, 0);
tempg.DrawImage(ring, 0, 0);
//tempg.Flush();
tempg.TranslateTransform(1024, 1024);
tempg.RotateTransform((float)-angle);
tempg.TranslateTransform(-1024, -1024);
tempg.DrawImage(over, 0, 0);
//tempg.Flush();
tempg.Save();
deck.SetKeyBitmap(9, KeyBitmap.Create.FromBitmap((Bitmap)result));
angle = angle +2; // for testing - angles are read from simulator in a another thread
}
I guess, while showing the videoframes on the streamdeck, you encountered the same problem.
Do you have a reciepe or tip for me, how i can achive drawing in a higher frame rate while using Bitmaps? Maybe you could push me into the right direction.
I hope it is ok for you that i ask this question via a git issue.
Gern auch auf deutsch 😃
Viele Grüße aus Bremen
Andreas
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 16 (3 by maintainers)
Ran into the sam Problem. easy solution.
The KeyBitmap.FromBitmap must run in the same thread where the Bitmap was instatiated. I move all bitmap operations to a seprate thread and precache the Keybitmaps there, pushing them then just out to the device when needed. The keybitmap itself is threadsafe. Works a treat.
Only still have the “half” image issue, that sometimes images will look halftoned for the upper or lower half of the bitmap, while the bitmap itself is 100% ok, and the keybitmap is ok as well.
@Cupiii could you provide a link to the repository it’s pretty hard to help only knowing small parts of what you are doing and what you want to achieve.
I’m not sure why you would need a locking mechanism in “mydraw”. Drawing shouldn’t happen in parallel, a background timer with 10-40 FPS should do the trick. There are a lot more things I noticed in the short section you provided, like why do you call
tempg.Save();without a state restore this very likely doesn’t do what you think it does.Initialization should probably happen in another method an the path shouldn’t be hardcoded. Even during early development it should at least be a constant because it will be way easier to change later.
Also according to your comment the images are 2048x2048, and every drawing tick you scale transform and draw them. You probably should cache the downscaled images and only rotate them. You could go even further and trade of memory for speed. Because you always add 3 to your angle there are only 360/3 = 120 states which you could all cache if you want.
But as I said earlier. If you provide a link to your repo I may find the time help you clean things up and get it running.