
Copy screen to byte[] array
Posted Thursday, 3 November, 2011 - 11:30 by trenmost inHi! I'm using c#, and i would like to copy my screen content to a byte array:
byte[] image = new byte[resX * resY]; GL.ReadPixels(0, 0, resX, resY, PixelFormat.Bgr, PixelType.Byte, image);
I tried it with this method, but when the program gets at the GL.ReadPixels method, i get an error message of: "Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
what am i doing wrong?
:)


Comments
Re: Copy screen to byte[] array
PixelFormat.Bgr requires 3 bytes per pixel, so your byte array should have a size of resX * resY * 3.
Consider using PixelFormat.Bgra and resX * resY * 4 for better performance.
Re: Copy screen to byte[] array
actually i only need the Red component of the image. so if i use PixelFormat.Red, then is the resx*resy sized buffer enough?
Re: Copy screen to byte[] array
Yes, that would be enough (but it might still be faster to use Bgra and only read every fourth element - you'd have to measure, provided performance is a concern).
Re: Copy screen to byte[] array
yeah but it would be worse memory wise. im not even drawing the image to the screen actually, i just use openGL's fast shape-to-pixel conversion, and then get back the data :)
note: i tried using the PixelFormat.Red, but sometimes it gives this memory error even now! :( but sometimes it works...
Re: Copy screen to byte[] array
I have been using this method for years without issue. Is this the exact code you are using?
Re: Copy screen to byte[] array
yes. in this function im drawing a custom class (Shape), which holds points and some extra data as follows:
this works perfectly when sometimes, but sometimes it doesnt..
by distortion i mean: lets call the first row of the image 0, the second row: 1, third row: 2 etc.
the image looks like if every row was shifted with i, where i is the index of the row (0,1,2...)
oh and i have to allocate more space for the image array, because of the distortion, or else i get this corrupted memory error!
Re: Copy screen to byte[] array
OpenGL aligns rows to 4-byte boundaries by default. Which means there are some empty padding bytes at the end of a row, if resX * byte size of the pixelformat isn't a multiple of 4. So you have to set the array size like this:
Re: Copy screen to byte[] array
wow...that'll be the problem! i'll implement this, but thanks a lot!!!! :)
Re: Copy screen to byte[] array
It was the problem! but instead of using that GetStride, i expanded the resolution to the next number which is a multiple of 4 by using this code:
(i could do this because im not drawing this image to the screen, im just using its data).
Thank you smiley80! :)