如何为图像(Visual Studio的C#)显示原始数据 [英] How to display raw data as an image (Visual Studio c#)

查看:607
本文介绍了如何为图像(Visual Studio的C#)显示原始数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将接收将被存储在一个字节数组,其中每个2字节是象素值(16比特/像素)的一些原始数据。首先,该数组将包含100×100×2个字节(够了100×100像素的图像)。我想在表格窗口中显示此数据。最后,我想刷新图像用新的数据,使它看起来像一个视频流。没有严格的帧速率是必需的。如何才能做到这一点?在C#中的任何代码的例子吗?



编辑:
几十类似的问题的一些建议和评论后,我还是不能让这个部落格。这里是什么,我试图做的总体思路,但图像没有在窗体上的图片框中显示。 什么是专门错我的实现,以及如何解决它



  //阵列数据I收集
的byte [] dataArray中=新的字节[100 * 100 * 2];
//创建一个指针数据
的IntPtr HGLOBAL = Marshal.AllocHGlobal(100 * 100 * 2);

//我的数组复制到全球
Marshal.Copy(dataArray中,0,HGLOBAL,dataArray.Length);
//创建位图:100×100像素,2字节/像素,16bitgrayscale
位图newBitmap =新位图(100,100,2 * 100,PixelFormat.Format16bppGrayScale,HGLOBAL);

//显示位图
pictureBox1.Image = newBitmap;

//释放内存
Marshal.FreeHGlobal(HGLOBAL);


解决方案

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

 私人无效Form1_Load的(对象发件人,EventArgs五)
{
//创建像素数据就摆在图像,使用2,因为它是16bpp的
随机R =新的随机();
INT宽度= 100;
INT高度= 100;
字节[] = pixelValues新的字节[宽*高* 2];
的for(int i = 0; I< pixelValues.Length ++ I)
{
//刚刚创建测试
pixelValues随机像素值[I] =(字节)r.Next(0,256);
}

变种rgbData = Convert16BitGrayScaleToRgb48(pixelValues,宽度,高度);
变种的bmp = CreateBitmapFromBytes(rgbData,宽度,高度);

//显示位图
pictureBox1.Image = BMP;
}

私人静态的byte [] Convert16BitGrayScaleToRgb48(字节[] inBuffer,诠释的宽度,高度INT)
{
INT inBytesPerPixel = 2;
INT outBytesPerPixel = 6;

字节[] = outBuffer新的字节[宽*高* outBytesPerPixel]。
INT inStride =宽* inBytesPerPixel;
INT outStride =宽* outBytesPerPixel;

//通过排
图像的步骤(; Y<高度; INT Y = 0是++)
{
//通过图像分步列
的for(int x = 0; X<宽度; X ++)
{
//获取inbuffer指数和outbuffer指数
INT inIndex =(Y * inStride)+( X * inBytesPerPixel);
INT outIndex =(Y * outStride)+(X * outBytesPerPixel);

字节HIBYTE = inBuffer [inIndex + 1];
字节lobyte = inBuffer [inIndex]

// - [R
outBuffer [outIndex] = lobyte;
outBuffer [outIndex + 1] = HIBYTE;

//摹
outBuffer [outIndex + 2] = lobyte;
outBuffer [outIndex + 3] = HIBYTE;

// B
outBuffer [outIndex + 4] = lobyte;
outBuffer [outIndex + 5 = HIBYTE;
}
}
返回outBuffer;
}

私有静态位图CreateBitmapFromBytes(字节[] pixelValues,诠释的宽度,高度INT)
{
//创建将保存图像数据$图像b $ b BMP位图=新位图(宽度,高度,PixelFormat.Format48bppRgb);

//获取图像的像素数据
矩形尺寸参考=新的Rectangle(0,0,bmp.Width,bmp.Height);
的BitmapData picData = bmp.LockBits(尺寸,ImageLockMode.ReadWrite,bmp.PixelFormat);
IntPtr的pixelStartAddress = picData.Scan0;

//像素数据复制到该位图结构
System.Runtime.InteropServices.Marshal.Copy(pixelValues,0,pixelStartAddress,pixelValues.Length);

bmp.UnlockBits(picData);
返回BMP;
}



想法是从的此线程


I will be receiving some raw data that will be stored in a byte array, where each 2 bytes is a pixel value (16 bits/px). To start with, the array will contain 100x100*2 bytes (enough for a 100x100 pixel image). I would like to display this data in the Form window. Eventually, I would like to refresh the image with the new data to make it look like a video stream. No strict frame rate is required. How can this be done? Any code examples in C#?

EDIT: After some suggestions and reviews of tens of similar questions I still can not get this going. Here's the general idea of what I am trying to do, but the image is not displayed in the picture box on the form. What is specifically wrong with my implementation and how to fix it?

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

解决方案

The main problem is that PixelFormat.Format16bppGrayScale is not supported (at least on my Win 8.1 x64 system). So you have to convert image to rgb before displaying:

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

Idea was taken from this thread.

这篇关于如何为图像(Visual Studio的C#)显示原始数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆