将浮点像素数据从文件中读回 C#

Read float pixel data from a file back to C#

本文关键字:文件 数据 像素 像素数      更新时间:2023-10-16

在C++中,我将浮点图像写入文件:

FILE* fp = fopen("image.fft", "wb");
float* pixels = getPixel();
fwrite((unsigned char*)pixels, sizeof(pixels), width*height, fp);

为了分析图像,我们需要将浮点图像读入 C#。我坚持如何将浮点图像"image.fft"读取到 C# 中。我知道浮动图像的大小宽度和高度。

你可以使用这个bimap构造函数 http://msdn.microsoft.com/en-us/library/zy1a2d14.aspx,只需使用GCHandle从文件中字节数组来获取IntPtr或类似的东西:

 Bitmap BytesToBitmap (byte[] bmpBytes, Size imageSize)
{
    Bitmap bmp = new Bitmap (imageSize.Width, imageSize.Height);
    BitmapData bData  = bmp.LockBits (new Rectangle (0,0, bmp.Size.Width,bmp.Size.Length),
        ImageLockMode.WriteOnly,
        PixelFormat.Format32bppRgb);
    // Copy the bytes to the bitmap object
    Marshal.Copy (bmpBytes, 0, bData.Scan0, bmpBytes.Length);
    bmp.UnlockBits(bData);
    return bmp;
}

使用位图类来获取和设置像素欲了解更多信息,请关注此