2013-12-20 30 views
2

我试过两种不同的方法来实现这一点,第一种是Android风格的方法,第二种是OpenGL风格的方法。从我的活动中,我创建了一个包含OpenGL(1.1)代码的视图。如何在Xamarin for Android中使用OpenTK作为位图获取OpenGL渲染?

第一种方法(机器人):

Bitmap b = gameView.GetDrawingCache (true); // this is always null 

而第二个方法(OpenGL的):

public Bitmap GrabScreenshot() 
{ 
     int size = Width * Height * 4; 
     byte[] bytes = new byte[size]; 
     GL.ReadPixels<byte>(0, 0, Width, Height, All.Rgba, All.UnsignedByte, bytes); 
     Bitmap bmp = BitmapFactory.DecodeByteArray (bytes, 0, size); 
     return bmp; 
} 

回答

2

我没有测试此代码。我以为你可以用它作为指导。

如何试图像这样(源自:OpenTK Forums):

public Bitmap GrabScreenshot() 
    { 

     Bitmap bmp = new Bitmap(Width, Height); 
     System.Drawing.Imaging.BitmapData data = 
      bmp.LockBits(otkViewport.ClientRectangle, System.Drawing.Imaging.ImageLockMode.WriteOnly, 
         System.Drawing.Imaging.PixelFormat.Format24bppRgb); 

     GL.Finish(); 
     GL.ReadPixels(0, 0, this.otkViewport.Width, this.otkViewport.Height, PixelFormat.Bgr, PixelType.UnsignedByte, data.Scan0); 
     bmp.UnlockBits(data); 
     bmp.RotateFlip(RotateFlipType.RotateNoneFlipY); 
     return bmp; 
    } 

我相信可能会出现由于字节的格式问题。在这个例子中,他们明确规定开始数据的阵列

data.Scan0 

但是,你只要发送一个字节数组。

+0

感谢您的答复。我在OpenTK网站上看到了这一点,我只是不确定如何将这些代码翻译成在Xamarin中可用的东西。 – Chris

1

这里说上Xamarin.Android工作的版本:

private static Bitmap GraphicsContextToBitmap(int width, int height) 
    { 
     GL.Flush(); 
     GL.PixelStore (PixelStoreParameter.PackAlignment, 1); 

     var bitmap = Bitmap.CreateBitmap(width, height, Bitmap.Config.Argb8888); 

     var data = bitmap.LockPixels();  
     GL.ReadPixels(0, 0, width, height, PixelFormat.Rgba, PixelType.UnsignedByte, data); 
     GL.Finish(); 
     bitmap.UnlockPixels(); 

     return bitmap; 
    }