【问题标题】:How to change 12-bits and 10-bits raw file to bitmap image using c#如何使用 c# 将 12 位和 10 位原始文件更改为位图图像
【发布时间】:2013-11-19 04:52:03
【问题描述】:

我有 10 位和 12 位 rawbayer (.raw) 文件。我想将其转换为位图图像,但我无法更改它。 8 位或 16 位原始文件可轻松更改,但 10 位或 12 位无法更改。

这是 8 位原始文件到位图的代码。

private void DisplayImage08(string fileName) //Raw file name
    {
        // Open a binary reader to read in the pixel data. 
        // We cannot use the usual image loading mechanisms since this is raw 
        // image data.         
        try
        {
            BinaryReader br = new BinaryReader(File.Open(fileName, FileMode.Open));
            byte pixByte;
            int i;
            int iTotalSize = (int)br.BaseStream.Length;

            // Get the dimensions of the image from the user
            ID = new ImageDimensions(iTotalSize); 
            width = Convert.ToInt32(ID.txtwidth);
            height = Convert.ToInt32(ID.txtheight);
            //panel1.Width = width;
            //panel1.Height = height;
            pictureBox1.Width = width;
            pictureBox1.Height = height;
            pix08 = new byte[iTotalSize];
                //pix08 = new byte[iTotalSize];

            for (i = 0; i < iTotalSize; ++i)
            {

                pixByte = (byte)(br.ReadByte());
                pix08[i] = pixByte;
            }
                br.Close();
               int bitsPerPixel = 8;
                stride = (width * bitsPerPixel + 7) / 8;

                // Single step creation of the image
                bmps = BitmapSource.Create(width, height, 96, 96, PixelFormats.Gray8, null,
                    pix08, stride);
                //Bitmap bmp = new Bitmap();

               // img.Source = bmps;
                Bitmap bt = BitmapFromSource(bmps); //Change Bitmap source to Bitmap
                pictureBox1.Image = bt; //Display on pictureBox



                pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;

        }
        catch (Exception e)
        {

        }
    }

但我想将 12 位或 10 位原始文件更改为位图。

所以请帮我解决这个问题,我该如何改变它?

谢谢

【问题讨论】:

  • 请不要只要求我们为您解决问题。向我们展示如何尝试自己解决问题,然后向我们确切地展示结果是什么,并告诉我们您为什么觉得它不起作用。请参阅“What Have You Tried?”了解您真正需要阅读的优秀文章。
  • 您似乎忽略了代码中有趣的部分。 BitmapSourceBitmapFromSource的实现是什么?

标签: c# .net bitmap io visual-c++-2010


【解决方案1】:

这绝不是一个完整的答案,但此代码将根据数据的打包方式将 12 位或 10 位图像转换为 8 位图像。

        // Input data read from file
        var pixels = ... 

        // Output
        var bytes = new byte[width * height];

        // Sort data
        for (var i = 0; i < bytes.Length; i++)
        {
            if (i % 2 == 0)
            {
                var index = i * 3 / 2;
                bytes[i] = pixels[index];
            }
            else
            {
                var index = (i * 3 + 1) / 2;
                bytes[i] = pixels[index];
            }
        }

【讨论】:

    猜你喜欢
    • 2012-06-29
    • 2019-08-05
    • 2018-03-17
    • 1970-01-01
    • 2011-04-24
    • 2014-10-29
    • 1970-01-01
    • 1970-01-01
    • 2022-01-27
    相关资源
    最近更新 更多