patterncsharpMinor
Encoding a screenshot to JPEG and saving it to a memorystream
Viewed 0 times
screenshotencodingandsavingmemorystreamjpeg
Problem
I have done some tests of my code, which I got help to improve here before. The code simply takes a screenshot of a selected process window, encodes it to JPEG (if I want), saves it to a
My test simply runs the code 1000 times, and posts the time it took in ms.
It's very simple to look at it this way. I think that 10 seconds is 0% delay, but I'm not sure. From the results, it should be that for some reason (it should be 60fps as my screen displays that, not 100).
So here are the results:
The resolution isn't exactly correct as I am not counting the window borders, etc, so it is a bit larger. 800x600 is super fast, even with JPEG encoding, just a little slower than BMP.
But then, even the little jump to 1152x864 makes a huge performance hit, about twice as slow (with BMP, there was about 17k ms, so the encoding did quite the hit, but it's still very slow).
1600x1200 was more than 3x times slower. I didn't try BMP there, but I would guess it would land on 30k ms.
It basically hooks to a window and takes a screenshot of that window.
I didn't even think that capturing took make such a big performan
memorystream and returns it (necessary for sending it).My test simply runs the code 1000 times, and posts the time it took in ms.
It's very simple to look at it this way. I think that 10 seconds is 0% delay, but I'm not sure. From the results, it should be that for some reason (it should be 60fps as my screen displays that, not 100).
So here are the results:
800x600 = 11160 ms
1152x864 = 20218 ms
1600x1200 = 36399 msThe resolution isn't exactly correct as I am not counting the window borders, etc, so it is a bit larger. 800x600 is super fast, even with JPEG encoding, just a little slower than BMP.
But then, even the little jump to 1152x864 makes a huge performance hit, about twice as slow (with BMP, there was about 17k ms, so the encoding did quite the hit, but it's still very slow).
1600x1200 was more than 3x times slower. I didn't try BMP there, but I would guess it would land on 30k ms.
private static MemoryStream PrintWindow(IntPtr hwnd, EncoderParameters JpegParam)
{
NativeMethods.Rect rc;
NativeMethods.GetWindowRect(hwnd, out rc);
using (Bitmap bmp = new Bitmap(rc.Width, rc.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb))
{
using (Graphics gfxBmp = Graphics.FromImage(bmp))
{
IntPtr hdcBitmap = gfxBmp.GetHdc();
try
{
NativeMethods.PrintWindow(hwnd, hdcBitmap, 0);
}
finally
{
gfxBmp.ReleaseHdc(hdcBitmap);
}
}
MemoryStream ms = new MemoryStream();
bmp.Save(ms, GetEncoderInfo(ImageFormat.Jpeg), JpegParam);
return ms;
}
}It basically hooks to a window and takes a screenshot of that window.
I didn't even think that capturing took make such a big performan
Solution
Well, first of all, what you call "the little jump" is not that little. You can easily devide
As for your code - you can find the common way to save screenshots in .Net on SO (just dont forget to clean up: dispose bitmap, etc. ). You dont need to use interop for such tasks. I doubt it can be optimized any further.
Edit: you should also set
1152x864 by 800x600 for yourself and see, that it, surprisingly enough, equals 2. Which means, that your algorithm most likely has a complexity of O(n), which makes sense.As for your code - you can find the common way to save screenshots in .Net on SO (just dont forget to clean up: dispose bitmap, etc. ). You dont need to use interop for such tasks. I doubt it can be optimized any further.
Edit: you should also set
MemoryStream buffer size manually, to fit the size of resulting data. If you don't, it might cause MemoryStream to resize constantly and lead to performance loss.Context
StackExchange Code Review Q#29548, answer score: 3
Revisions (0)
No revisions yet.