locked
accessing pixels from directx application RRS feed

  • Question

  • I have written a program that is designed to take a screen shot of a directx application so that I can access its pixels. This is all done programatically and is done by simulating a print screen key press. Then I create an image and bitmap object to retrieve the image from the clipboard. I can then access the pixels from the bitmap.

    The problem is that this is a very slow process. What I would like to know is if there is any way to speed this up, or an alternative route to accessing the pixels that might be a bit faster but would provide the same functionality. Thanks

    Bryan
    Wednesday, September 12, 2007 6:30 PM

All replies

  •  

    check out the lockbits function from bitmap, following is from MSDN. I used it a while ago, it's fast.

     

    Code Snippet

    private void LockUnlockBitsExample(PaintEventArgs e)
    {

        // Create a new bitmap.
        Bitmap bmp = new Bitmap("c:\\fakePhoto.jpg");

        // Lock the bitmap's bits. 
        Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
        System.Drawing.Imaging.BitmapData bmpData =
            bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
            bmp.PixelFormat);
             
        // Get the address of the first line.
       IntPtr ptr = bmpData.Scan0;

        // Declare an array to hold the bytes of the bitmap.
        // This code is specific to a bitmap with 24 bits per pixels.
        int bytes = bmp.Width * bmp.Height * 3;
        byte[] rgbValues = new byte[bytes];

        // Copy the RGB values into the array.
        System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);

        // Set every red value to 255. 
        for (int counter = 2; counter < rgbValues.Length; counter+=3)
            rgbValues[counter] = 255;
     
        // Copy the RGB values back to the bitmap
        System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);

        // Unlock the bits.
        bmp.UnlockBits(bmpData);

        // Draw the modified image.
        e.Graphics.DrawImage(bmp, 0, 150);

    }

     

     

     

    Wednesday, September 12, 2007 7:14 PM