【问题标题】:Simple way to implement an efficient framebuffer in a modern language?用现代语言实现高效帧缓冲区的简单方法?
【发布时间】:2012-01-03 06:23:33
【问题描述】:

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

【问题讨论】:

  • 什么样的操作?读取/写入像素可以在数组中轻松完成。就这些了吗?

标签: c# java graphics d framebuffer


【解决方案1】:

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

MSDN 有一个示例 here,我从以下地址粘贴:

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

    }

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      如果您需要的是一个二维数组,在 C# 中您可以创建一个multidimensional array,让您可以直接访问每个成员。为了提高效率,尽量避免频繁的装箱和拆箱,不要频繁地分配和取消分配大内存块,如果你做得对,那么在 C# 或 Java 中没有理由比在其他语言中的效率低得多。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-06-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多