如何创建16位bmp图像?

时间:2016-11-04 15:19:40

标签: c# bmp

我尝试使用以下代码创建一个16位bmp / jpg文件:

    public static void CreateBitmap_Rgb48(int width, int height, double dpiX, double dpiY, string fn)
    {
        int bytesperpixel = 6; // BytesPerChannel = 2,ChannelCount = 3 (bgr)
        int channelCount = 3;
        int stride = width * bytesperpixel;
        byte[] imgdata = new byte[width * height * bytesperpixel];

        int rectDim = 40;
        ushort[] intPixelData = new ushort[width * height * channelCount];
        for (int row = 0; row < height; row++)
        {
            for (int col = 0; col < width * channelCount; col += channelCount)
            {
                if (((col / channelCount / rectDim) % 2) != ((row / rectDim) % 2))
                {
                    intPixelData[row * width * channelCount + col + 0] = 0x0000;
                    intPixelData[row * width * channelCount + col + 1] = 0x0000;
                    intPixelData[row * width * channelCount + col + 2] = 0xffff;
                }
                else
                {
                    intPixelData[row * width * channelCount + col + 0] = 0x0000;
                    intPixelData[row * width * channelCount + col + 1] = 0xffff;
                    intPixelData[row * width * channelCount + col + 2] = 0x0000;
                }
            }
        }
        Buffer.BlockCopy(intPixelData, 0, imgdata, 0, imgdata.Length);

        // compose the BitmapImage
        var image = BitmapSource.Create(width, height, dpiX, dpiY, PixelFormats.Rgb48, null, imgdata, stride);
        BmpBitmapEncoder encoder = new BmpBitmapEncoder();
        encoder.Frames.Add(BitmapFrame.Create(image));
        using (var fileStream = new FileStream(fn, FileMode.Create))
        {
            encoder.Save(fileStream);
        }
    }

同样,对于jpeg,我用过:             JpegBitmapEncoder encoder = new JpegBitmapEncoder();

但文件生成的大小是错误的,BmpBitmapDecoder显示bmp的默认格式和jpg的rgb24格式。他们不是rgb48。怎么了?

0 个答案:

没有答案