本文介绍了如何在c#中获取Tiff图像迭代并获取Fomart32bppRgb,Format48bppRgb的Pixel值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private byte[] GetRawBytesFromBmp(Bitmap bmp)
{
            byte[] raw;
            int row, column;
            int width = bmp.Width;
            int columnWidth = width / 4;
            int height = bmp.Height;
            if (height % 2 != 0 && width % 2 != 0)
            {
                width--;
            }

            int bmpStride = 0;
            int rawStride = 0;
            int BytesPerPixel = 0;
            BitmapData data = null;
            try
            {
                //Lock the bitmap so we can access the raw pixel data
                // Lock the bitmap's bits.
                data = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, bmp.PixelFormat);
                bmpStride = data.Stride;
                if (bmp.PixelFormat == PixelFormat.Format24bppRgb || bmp.PixelFormat == PixelFormat.Format8bppIndexed || bmp.PixelFormat==PixelFormat.Format32bppRgb)
                {

                    BytesPerPixel = Image.GetPixelFormatSize(bmp.PixelFormat) / 8;

                    rawStride = BytesPerPixel * width;
                }
                else if (bmp.PixelFormat == PixelFormat.Format48bppRgb)
                {
                    BytesPerPixel = Image.GetPixelFormatSize(bmp.PixelFormat) / 8;
                    rawStride = BytesPerPixel * width;
                }
                else
                {

                    return null;
                }
                // Declare an array to hold the bytes of the bitmap.
                raw = new byte[rawStride * height];
                unsafe
                {
                    // Get the address of the first line.
                    byte* bmpPtr = (byte*)data.Scan0.ToPointer();

                    fixed (byte* rawPtr = raw)
                        if (bmp.PixelFormat == PixelFormat.Format24bppRgb)
                        {
                            //the pixels row by row
                            for (row = 0; row < height; ++row)
                            {
                                for (column = 0; column < rawStride; column += BytesPerPixel)
                                {
                                    // rawPtr[row * rawStride + column] = bmpPtr[row * bmpStride + column];  //b
                                    //   rawPtr[row * rawStride + column + 1] = bmpPtr[row * bmpStride + column + 1]; //g
                                    // rawPtr[row * rawStride + column + 2] = bmpPtr[row * bmpStride + column + 2]; //r

                                    rawPtr[row * rawStride + column] = bmpPtr[row * bmpStride + column + 2];   //b
                                    rawPtr[row * rawStride + column + 1] = bmpPtr[row * bmpStride + column + 1]; //g
                                    rawPtr[row * rawStride + column + 2] = bmpPtr[row * bmpStride + column]; ; //r
                                }
                            }
                            // SwapRedBlue(raw);
                        }
                        else if (bmp.PixelFormat == PixelFormat.Format8bppIndexed)
                        {
                            for (row = 0; row < height; ++row)
                            {
                                for (column = 0; column < rawStride; column += BytesPerPixel)
                                {
                                    rawPtr[row * rawStride + column] = bmpPtr[row * bmpStride + column];  //r
                                }
                            }
                        }
                        else if (bmp.PixelFormat == PixelFormat.Format48bppRgb || bmp.PixelFormat == PixelFormat.Format32bppRgb)
                        {

                            //int PixelSize = 4;

                            for (row = 0; row < height; ++row)
                            {
                                // byte* row = (byte*)bmpdata.Scan0 + (y * bmpdata.Stride);

                                for (column = 0; column < rawStride; column += BytesPerPixel)
                                {
                                    //rawPtr[row * rawStride + column] = rawPtr[row * bmpStride + column];//0;   //Blue  0-255
                                    //rawPtr[row * rawStride + column + 1] = rawPtr[row * bmpStride + column + 1];// 255; //Green 0-255
                                   // rawPtr[row * rawStride + column + 2] = rawPtr[row * bmpStride + column + 2];//0;   //Red   0-255
                                   // rawPtr[row * rawStride + column + 3] = rawPtr[row * bmpStride + column + 3];//50;  //Alpha 0-255

                                    rawPtr[row * rawStride + column] = rawPtr[row * bmpStride + column];
                                    rawPtr[row * rawStride + column] = bmpPtr[row * bmpStride + column + 2];   //b
                                    rawPtr[row * rawStride + column + 1] = bmpPtr[row * bmpStride + column + 1]; //g
                                    rawPtr[row * rawStride + column + 2] = bmpPtr[row * bmpStride + column]; ; //r
                                }
                            }
                            /*
                            for (int y = 0; y < height; y++)
                            {
                                for (int x = 0; x < width * BytesPerPixel; x += BytesPerPixel)
                                {
                                    int i = y * width * BytesPerPixel + x;
                                    // Blue and Green components
                                    // always 0x00 since we are just
                                    // interested in red
                                    rawPtr[i] = 0x00;
                                    rawPtr[i + 1] = 0x00;
                                    rawPtr[i + 2] = 0x00;
                                    rawPtr[i + 3] = 0x00;

                                    if (x < columnWidth * BytesPerPixel)
                                    {
                                        //Left most column, full red
                                        rawPtr[i + 4] = 0xFF;
                                        rawPtr[i + 5] = 0xFF;
                                    }
                                    else if (x < columnWidth * BytesPerPixel * 2)
                                    {
                                        //Next left, half red
                                        rawPtr[i + 4] = 0x00;
                                        rawPtr[i + 5] = 0xFF;
                                    }
                                    else if (x < columnWidth * BytesPerPixel * 3)
                                    {
                                        //Next left, other half red
                                        rawPtr[i + 4] = 0xFF;
                                        rawPtr[i + 5] = 0x00;
                                    }
                                    else
                                    {
                                        //Final column, no red
                                        rawPtr[i + 4] = 0x00;
                                        rawPtr[i + 5] = 0x00;
                                    }
                                }
                            }

                        }




                }
            }
            catch (Exception e)
            {
                return null;
            }
            finally
            {
                bmp.UnlockBits(data);
            }
            return raw;
}

推荐答案

这篇关于如何在c#中获取Tiff图像迭代并获取Fomart32bppRgb,Format48bppRgb的Pixel值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 14:32