ilyazyilmaz's picture

ReadPixels

When thiscode run ,

             IntPtr abc=new IntPtr(0);
            GL.ReadPixels(10, 10, 1, 1, PixelFormat.Rgba, PixelType.Float, abc);

windows give this error:
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

I looked forum , this problem is occur too much.
How Can I solve this problem???


Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Shiny's picture

You're reading the pixel data and storing it at address 0-- probably not what you want.

So either provide a valid address from, say, a struct or a locked bitmap's data, or maybe pass a struct by reference.
The latter option is probably best in your case as you seem to be reading a single pixel.

Here I read a pixel into a plain int and convert it to a Color:

public Color GetPixel(Point location)
{
    int result = 0;
 
    GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, 0);
    GL.ReadBuffer(ReadBufferMode.Front);
    GL.ReadPixels(location.X, location.Y, 1, 1,
                  PixelFormat.Bgra, PixelType.UnsignedByte,
                  ref result);
 
    return Color.FromArgb(result);
}
ilyazyilmaz's picture

Thanks for your answer ,
I tries your code works .
But I need to get depth of pixel.

  GL.ReadPixels<float>(e.X, viewPort[3]-e.Y, 1, 1, PixelFormat.DepthComponent, PixelType.Float, Pixel);

I need to this function But I couldnt find in OpenTK

GL.gluUnProject();

AndyKorth's picture

We don't include any of the now-deprecated glu functions. But there are alternatives, such as:

http://www.opentk.com/node/1276#comment-13034

We might even have Unproject added to the math functions in the github branch. I don't recall for sure.. but I think it's come up in the last few months.