用现代语言实现高效帧缓冲区的简单方法?

发布于 2024-12-24 19:09:32 字数 133 浏览 0 评论 0原文

我正在寻找一种在 C#、D 或 Java 中实现帧缓冲区的简单方法。允许我使用二维颜色数组并更新单个像素或区域的东西(API 或库)。此外,更新时不会产生大量开销。我知道这可以通过 OpenGL 来完成,但 API 对于我正在做的事情来说似乎太复杂了。

I'm looking for a simple way to implement a framebuffer in C#, D or Java. Something (an API or library) that would allow me to work with a 2d array of colors and update individual pixels or areas. Also, something that doesn't incur large overhead on update. I know that this can be done with OpenGL, but the API seems far too complicated for what I'm doing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

败给现实 2024-12-31 19:09:32

尝试在 .NET 中使用普通的旧 System.Drawing.Bitmap 吗?
您可以使用 Bitmap.Lockbits() 来访问位图后面的字节数组并更新它。这比位图上的普通像素操作要高效得多。

MSDN 有一个示例此处,我粘贴自:

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.
        int bytes  = Math.Abs(bmpData.Stride) * bmp.Height;
        byte[] rgbValues = new byte[bytes];

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

        // Set every third value to 255. A 24bpp bitmap will look red.  
        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);

    }

Try using a plain old System.Drawing.Bitmap in .NET?
You could use Bitmap.Lockbits() to get access to the byte array behind the bitmap and update it. This is much more efficient than normal pixel operations on the bitmap.

MSDN has an example here that I've pasted from:

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.
        int bytes  = Math.Abs(bmpData.Stride) * bmp.Height;
        byte[] rgbValues = new byte[bytes];

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

        // Set every third value to 255. A 24bpp bitmap will look red.  
        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);

    }
阪姬 2024-12-31 19:09:32

数组在迭代整个屏幕的如此大量的像素数据时将花费大量时间。最好找到不需要或需要很少迭代的东西。更像是 C 中的指针。

Arrays will take lots of time while iterating through such large amount pixel data for a complete screen. It will be better to find something that doesn't needs or need very less amount of iteration. Something more like pointers in C.

你如我软肋 2024-12-31 19:09:32

If what you need is a 2D array, In C# you can create a multidimensional array that gives you direct access to every member. To make this efficient, try to avoid frequent boxing and unboxing and don't allocate and deallocate large memory chunks frequently, and if you do it right there's no reason why this task would be much less efficient in C# or Java than in other languages.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文