簡體   English   中英

如何在Xamarin for Android中使用OpenTK將OpenGL渲染捕獲為位圖?

[英]How do I grab an OpenGL render as a bitmap using OpenTK in Xamarin for Android?

我嘗試了兩種不同的方法來實現此目的,第一種是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;
}

我尚未測試此代碼。 我認為您可能可以將其用作指南。

如何嘗試這樣的事情(來自: OpenTK論壇 ):

    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

但是,您只是發送一個字節數組。

這里是在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;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM