【问题标题】:How do I grab an OpenGL render as a bitmap using OpenTK in Xamarin for Android?如何在 Xamarin for Android 中使用 OpenTK 将 OpenGL 渲染作为位图获取?
【发布时间】:2013-12-20 22:08:24
【问题描述】:

我尝试了两种不同的方法来实现这一点,第一种是 Android 风格的方法,第二种是 OpenGL 风格的方法。从我的活动中,我创建了一个包含 OpenGL (1.1) 代码的视图。

第一种方法(android):

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;
}

【问题讨论】:

    标签: c# android xamarin opentk


    【解决方案1】:

    我没有测试过这段代码。我在想你也许可以用它作为指南。

    试试这样的东西怎么样(源自: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
    

    但是,您只需发送一个字节数组。

    【讨论】:

    • 感谢您的回复。我在 OpenTK 网站上看到了这个,我只是不确定如何将这段代码翻译成可以在 Xamarin 中运行的东西。
    【解决方案2】:

    这是适用于 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;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-20
      • 1970-01-01
      • 1970-01-01
      • 2017-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多