图像Ushort数组到位图格式8bppindexed

时间:2015-06-22 15:24:41

标签: c# arrays image-processing bitmap bitmapimage

我输入了一个Ushort图像数据数组。此处收集其他输入,例如“宽度,高度”。 ushort数组还带有我想要使用的Min和Max值,它们存储在'io_current'中。

我想返回一个Format8ppIndexed位图,我有这个代码,但我做错了什么?:

    private Bitmap CreateBitmap(ushort[,] pixels16)
    {
        int width = pixels16.GetLength(1);
        int height = pixels16.GetLength(0);
        Bitmap bmp = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
        BitmapData bmd = bmp.LockBits(new Rectangle(0, 0, width, height),
        System.Drawing.Imaging.ImageLockMode.ReadOnly, bmp.PixelFormat);

        // This 'unsafe' part of the code populates the bitmap bmp with data stored in pixel16.
        // It does so using pointers, and therefore the need for 'unsafe'. 
        unsafe
        {
            //int pixelSize = 4;
            int i, j; //, j1; //, i1;
            byte b;
            ushort sVal;
            double lPixval;
            //The array has max and min constraints
            int distance = io_current.MaximumValue - io_current.MinimumValue;

            for (i = 0; i < bmd.Height; ++i)
            {
                byte* row = (byte*)bmd.Scan0 + (i * bmd.Stride);
                //i1 = i * bmd.Height;

                for (j = 0; j < bmd.Width; ++j)
                {
                    sVal = (ushort)(pixels16[i, j]);

                    lPixval = ((sVal - io_current.MinimumValue) * 255) / distance; // Convert to a 255 value range
                    //lPixval = ((sVal - io_current.MinimumValue) / distance) * 255;
                    //lPixval = 255 - lPixval; //invert the value
                    if (lPixval > 255) lPixval = 255;
                    if (lPixval < 0) lPixval = 0;
                    b = (byte)(lPixval);
                    //j1 = j * pixelSize; //Pixelsize is one
                    row[j] = b; // Just one in 8bpp

                    //Not necessary for format8bppindexed
                    //row[j1] = b; // Red
                    //row[j1 + 1] = b; // Green
                    //row[j1 + 2] = b; // Blue
                    //row[j1 + 3] = 255; //No Alpha channel in 24bit
                }
            }
        }
        bmp.UnlockBits(bmd);
        return bmp;
    }

我正在使用黑屏或单色屏幕。基本上没有返回可用的数据。显然来自评论。我试图从24位位图代码转换它,并认为这很容易。

0 个答案:

没有答案