简体   繁体   English

用文件流创建位图

[英]create bitmap with filestream

I need to create a bitmap with a filestream.我需要使用文件流创建位图。 So far I have this Code:到目前为止,我有这个代码:

 using (FileStream bmp = File.Create(@"C:\test.bmp"))
        {
            BinaryWriter writer = new BinaryWriter(bmp);
            int i = 0;

          //  writer.Write((char*)&fileheader, sizeof(fileheader));
          //  writer.Write((char*)&infoheader, sizeof(infoheader));

            for (int rows = 0; rows < 160; rows++)
            {
                for (int cols = 0; cols < 112; cols++)
                {
                    writer.Write(CamData[i]);
                    i++;
                }
            }

            bmp.Close();
        }

but I still need the header Informations for the bitmap.但我仍然需要位图的标题信息。 My Problem is, that I dont know how to implement they in C#.我的问题是,我不知道如何在 C# 中实现它们。 I know the resolution (320 x 240 ) and my pixeldata are 16 bit grayscale values given in a ushort array.我知道分辨率 (320 x 240 ) 和我的像素数据是 ushort 数组中给出的 16 位灰度值。

thanks谢谢

Seems the System.Drawing classes don't like handling 16 bit grayscale, probably because the underlying GDI+ object sees its colour components as values from 0 to 255, whereas 16 bit grayscale actually means you can have 65535 shades of gray.似乎 System.Drawing 类不喜欢处理 16 位灰度,可能是因为底层 GDI+ 对象将其颜色分量视为从 0 到 255 的值,而 16 位灰度实际上意味着您可以拥有 65535 种灰度。

This means you have two options: either you switch to PresentationCore, and create your image with that, or you downsample the values to byte size and make an 8-bit grayscale image.这意味着您有两个选择:要么切换到 PresentationCore,然后使用它创建图像,要么将值下采样到字节大小并制作 8 位灰度图像。

The first option is explained in this answer .本答案中解释了第一个选项。

The second option includes three steps:第二个选项包括三个步骤:

  • Downsample the data to 1 byte per pixel将数据下采样到每像素 1 个字节
  • Generate a grayscale colour palette (since 8-bit grayscale is technically paletted)生成灰度调色板(因为 8 位灰度在技术上是调色板)
  • Create an 8-bit indexed image out of your downsampled data and the palette.从您的下采样数据和调色板中创建一个 8 位索引图像。

The code:编码:

Byte[] camDataBytes = new Byte[CamData.Length];
for(Int32 i = 0; i < camData.Length; i++)
    camDataBytes[i] = (Byte)(CamData[i] / 256);

Color[] palette = new Color[256];
for(Int32 i = 0; i < 256; i++)
    palette[i] = Color.FromArgb(i,i,i);

using(Bitmap b = BuildImage(camDataBytes, 320, 240, 320, PixelFormat.Format8bppIndexed, palette, null))
    b.Save(@"C:\test.bmp", ImageFormat.Bmp); 

The BuildImage function to create an image out of a byte array can be found here .可以在此处找到从字节数组中创建图像的BuildImage函数。 Assuming the image data is compact 320x240, the stride of the final byte array should be exactly the width, and thus 320.假设图像数据是紧凑的 320x240,最终字节数组的步幅应该正好是宽度,因此是 320。

There is a constructor to create a Bitmap from a byte array.有一个构造函数可以从字节数组创建Bitmap Then, save it to stream in bmp format, using Bitmap 's member functions.然后,使用Bitmap的成员函数将其保存为 bmp 格式的流。 See here and here .请参阅此处此处

Try this:尝试这个:

/// From stream to bitmap...
FileStream fs = new FileStream("test.bmp", FileMode.Open);
Bitmap bmp = new Bitmap(fs);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM