c# - 如何将原始数据显示为图像 (Visual Studio c#)

标签 c# image visual-studio drawing

我将接收一些存储在字节数组中的原始数据,其中每 2 个字节是一个像素值(16 位/像素)。首先,数组将包含 100x100*2 字节(对于 100x100 像素的图像来说足够了)。我想在窗体窗口中显示此数据。最后,我想用新数据刷新图像,使它看起来像视频流。不需要严格的帧率。如何才能做到这一点? C# 中的任何代码示例?

编辑: 在对数十个类似问题提出一些建议和评论之后,我仍然无法解决这个问题。这是我正在尝试做的事情的总体思路,但图像未显示在窗体的图片框中。 我的实现具体有什么问题以及如何解决

// array of data I collected
byte[] dataArray = new byte[100 * 100 * 2]; 
//create a pointer to the data
IntPtr hglobal = Marshal.AllocHGlobal(100 * 100 * 2);

// copy my array to global
Marshal.Copy(dataArray, 0, hglobal, dataArray.Length);
// create a bitmap: 100x100 pixels, 2bytes/pixel, 16bitgrayscale
Bitmap newBitmap = new Bitmap(100, 100, 2 * 100, PixelFormat.Format16bppGrayScale, hglobal);

// display bitmap
pictureBox1.Image = newBitmap;

// free the memory
Marshal.FreeHGlobal(hglobal);

最佳答案

主要问题是不支持 PixelFormat.Format16bppGrayScale(至少在我的 Win 8.1 x64 系统上)。所以你必须在显示之前将图像转换为 rgb:

private void Form1_Load(object sender, EventArgs e)
{
    //Create pixel data to put in image, use 2 since it is 16bpp
    Random r = new Random();
    int width = 100;
    int height = 100;
    byte[] pixelValues = new byte[width * height * 2];
    for (int i = 0; i < pixelValues.Length; ++i)
    {
        // Just creating random pixel values for test
        pixelValues[i] = (byte)r.Next(0, 256);
    }

    var rgbData = Convert16BitGrayScaleToRgb48(pixelValues, width, height);
    var bmp = CreateBitmapFromBytes(rgbData, width, height);

    // display bitmap
    pictureBox1.Image = bmp;
}

private static byte[] Convert16BitGrayScaleToRgb48(byte[] inBuffer, int width, int height)
{
    int inBytesPerPixel = 2;
    int outBytesPerPixel = 6;

    byte[] outBuffer = new byte[width * height * outBytesPerPixel];
    int inStride = width * inBytesPerPixel;
    int outStride = width * outBytesPerPixel;

    // Step through the image by row
    for (int y = 0; y < height; y++)
    {
        // Step through the image by column
        for (int x = 0; x < width; x++)
        {
            // Get inbuffer index and outbuffer index
            int inIndex = (y * inStride) + (x * inBytesPerPixel);
            int outIndex = (y * outStride) + (x * outBytesPerPixel);

            byte hibyte = inBuffer[inIndex + 1];
            byte lobyte = inBuffer[inIndex];

            //R
            outBuffer[outIndex] = lobyte;
            outBuffer[outIndex + 1] = hibyte;

            //G
            outBuffer[outIndex + 2] = lobyte;
            outBuffer[outIndex + 3] = hibyte;

            //B
            outBuffer[outIndex + 4] = lobyte;
            outBuffer[outIndex + 5] = hibyte;
        }
    }
    return outBuffer;
}

private static Bitmap CreateBitmapFromBytes(byte[] pixelValues, int width, int height)
{
    //Create an image that will hold the image data
    Bitmap bmp = new Bitmap(width, height, PixelFormat.Format48bppRgb);

    //Get a reference to the images pixel data
    Rectangle dimension = new Rectangle(0, 0, bmp.Width, bmp.Height);
    BitmapData picData = bmp.LockBits(dimension, ImageLockMode.ReadWrite, bmp.PixelFormat);
    IntPtr pixelStartAddress = picData.Scan0;

    //Copy the pixel data into the bitmap structure
    System.Runtime.InteropServices.Marshal.Copy(pixelValues, 0, pixelStartAddress, pixelValues.Length);

    bmp.UnlockBits(picData);
    return bmp;
}

想法取自this thread .

关于c# - 如何将原始数据显示为图像 (Visual Studio c#),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32290631/

相关文章:

c# - 可以从编译为 wasm 的 C# 调用 C++ 吗?

c# - Android 的单声道,WebView 输入字段文件选择器不起作用

javascript - 订购图像游戏

html - 如何去除 float 标签?

c# - Visual Studio 应用程序中的解决方案中缺少某些 NuGet 包

c# - 从 ViewModel 了解数据绑定(bind)

c# - Try-Catch 表达流畅

html - Css 图像在响应图像顶部的绝对位置

c# - 如何在 C# 中向类、方法、属性等添加文档工具提示?

c++ - 是否可以从 CUDA 10.1 内核调用 cuBLAS 或 cuBLASLt 函数?