locked
Tiff image convert is not work in the following pixel in my code? RRS feed

  • Question

  • User-607628887 posted

    I have doubts following things
    1.Tiff image convert to Bitmap.
    Bitmap Pixel Format is the following
    Format32bppRgb
    Format32bppPArgb
    Format32bppArgb
    Format48bppRgb

    System.Drawing.Bitmap _selectedImage;
    //// Retrieve the image.
    _selectedImage = new Bitmap(linkstirng, true);
    byte[] direct = GetRawBytesFromBmp(_selectedImage);

    I have put my method code

    Format24bppRgb working fine.but the following format not works please give and idea.

    Format32bppRgb
    Format32bppPArgb
    Format32bppArgb
    Format48bppRgb

    ///
    /// Gives the Byte array for bmp data.
    ///
    ///
    ///
    private byte[] GetRawBytesFromBmp(Bitmap bmp)
    {
    /// Here byte of Array declare
    byte[] raw;
    int row, column;
    //Bitmap width
    int width = bmp.Width;

    //Bitmap Height
    int height = bmp.Height;
    if (height % 2 != 0 && width % 2 != 0)
    {
    width–;
    }
    /*——————————————————–
    BitmapData data = image.LockBits(new Rectangle(0, 0, columns, rows), ImageLockMode.ReadOnly, image.PixelFormat);
    IntPtr bmpData = data.Scan0;
    int stride = columns*3;
    int size = rows*stride;

    byte[] pixelData = new byte[size];

    for (int i = 0; i < rows; ++i)
    Marshal.Copy((new IntPtr(bmpData.ToInt64() + i*data.Stride), pixelData, i*stride, stride);
    ——————————————————————————————————*/

    int bmpStride = 0;
    int rawStride = 0;
    int BytesPerPixel = 0;
    BitmapData data = null;
    try
    {
    //Lock the bitmap so we can access the raw pixel data
    data = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, bmp.PixelFormat);
    bmpStride = data.Stride;
    if (bmp.PixelFormat == System.Drawing.Imaging.PixelFormat.Format24bppRgb || bmp.PixelFormat == System.Drawing.Imaging.PixelFormat.Format8bppIndexed || bmp.PixelFormat == System.Drawing.Imaging.PixelFormat.Format48bppRgb)
    {
    BytesPerPixel = System.Drawing.Image.GetPixelFormatSize(bmp.PixelFormat) / 8;
    rawStride = BytesPerPixel * width;
    }
    else if (bmp.PixelFormat == System.Drawing.Imaging.PixelFormat.Format32bppRgb)
    {

    BytesPerPixel = System.Drawing.Image.GetPixelFormatSize(bmp.PixelFormat) / 8;
    rawStride = BytesPerPixel * width;

    }
    else
    {

    return null;
    }

    raw = new byte[rawStride * height];
    unsafe
    {
    if (bmp.PixelFormat == System.Drawing.Imaging.PixelFormat.Format24bppRgb)
    {
    byte* bmpPtr = (byte*)data.Scan0.ToPointer();

    fixed (byte* rawPtr = raw)
    if (bmp.PixelFormat == System.Drawing.Imaging.PixelFormat.Format24bppRgb)
    {
    //the pixels row by row
    for (row = 0; row < height; ++row)
    {
    // iterate over width (columns)
    for (column = 0; column < rawStride; column += BytesPerPixel)
    {

    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
    }
    }

    }
    else if (bmp.PixelFormat == System.Drawing.Imaging.PixelFormat.Format8bppIndexed)
    {
    //the pixels row by row
    for (row = 0; row < height; ++row)
    { // iterate over width (columns)
    for (column = 0; column < rawStride; column += BytesPerPixel)
    {
    rawPtr[row * rawStride + column] = bmpPtr[row * bmpStride + column]; //r
    }
    }
    }
    else if (bmp.PixelFormat == System.Drawing.Imaging.PixelFormat.Format48bppRgb)
    {
    // the below code wont work please give any idea and above condition

    //the pixels row by row
    //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 0×00 since we are just
    // // interested in red
    // rawPtr[i] = 0×00;
    // rawPtr[i + 1] = 0×00;
    // rawPtr[i + 2] = 0×00;
    // rawPtr[i + 3] = 0×00;

    // 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] = 0×00;
    // rawPtr[i + 5] = 0xFF;
    // }
    // else if (x 0 && raw!=null)
    // {

    // }

    //}
    }
    }
    else if (bmp.PixelFormat == System.Drawing.Imaging.PixelFormat.Format32bppRgb)
    {

    //the pixels row by row
    //for (int y = 0; y < data.Height; ++y)
    //{
    // byte* ptrSrc = (byte*)data.Scan0 + (y * data.Stride);
    // int* pixelPtr = (int*)ptrSrc;

    // for (int x = 0; x < data.Width; ++x)
    // {
    // System.Drawing.Color col = System.Drawing.Color.FromArgb(*pixelPtr);

    // if (col == ColorToFind)
    // return new System.Drawing.Point(x, y);

    // ++pixelPtr; //Increate ptr by 4 bytes, because it is int
    // }
    //}

    ////int PixelSize = 4;

    fixed (byte* rawPt = raw)
    //the pixels row by row
    for (int y = 0; y < data.Height; y++)
    {
    byte* rawPtr = (byte*)data.Scan0 + (y * data.Stride);
    //the pixels row by row

    for (int x = 0; x < data.Width; x++)
    {
    rawPtr[x * BytesPerPixel] = 255;
    //rawPtr[x * BytesPerPixel] = 0;
    //rawPtr[x * BytesPerPixel] = 0;

    }
    }

    /*
    fixed (byte* rawPt = raw)
    for (int y = 0; y < data.Height; y++)
    {
    byte* rawPtr = (byte*)data.Scan0 + (y * data.Stride);

    for (int x = 0; x < data.Width; x++)
    {
    //rawPtr[x * BytesPerPixel] = 255;

    rawPt[x * BytesPerPixel] = rawPtr[y * data.Width+x+2];//b
    rawPt[x * BytesPerPixel+1] = rawPtr[y * data.Width + x + 1];//g
    rawPt[x * BytesPerPixel + 2] = rawPtr[y * data.Width + x];//r

    }
    }*/

    /*
    fixed (byte* rawPt = raw)
    for (int y = 0; y < data.Height; ++y)
    {
    byte* rawPtr = (byte*)data.Scan0 + (y * data.Stride);

    for (int x = 0; x < data.Width; x += BytesPerPixel)
    {
    //rawPtr[x * BytesPerPixel] = 255;

    rawPt[x * BytesPerPixel] = rawPtr[y * data.Width + x + 2];//b
    rawPt[x * BytesPerPixel + 1] = rawPtr[y * data.Width + x + 1];//g
    rawPt[x * BytesPerPixel + 2] = rawPtr[y * data.Width + x];//r

    }
    }*/

    //fixed (byte* rawPt = raw)

    //for (int y = 0; y < data.Height; ++y)
    //{
    // byte* rawPtr = (byte*)data.Scan0 + (y * data.Stride);

    // for (int x = 0; x < data.Width; x += BytesPerPixel)
    // {
    // //rawPtr[x * BytesPerPixel] = 255;

    // rawPt[y * data.Width+x] = rawPtr[y * data.Width + x + 2];//b
    // rawPt[y * data.Width+x + 1] = rawPtr[y * data.Width + x + 1];//g
    // rawPt[y * data.Width+x + 2] = rawPtr[y * data.Width + x];//r

    // }
    //}

    //for (int i = 0; i < rawStride; i++)
    // {
    // byte* pixel = (byte*)data.Scan0;
    // pixel = pixel + (i * 4);

    // byte b = pixel[0];
    // byte g = pixel[1];
    // byte r = pixel[2];

    // byte luma = (byte)(r * 0.3 + g * 0.59 + b * 0.11);
    // raw[i] = luma;
    // }

    //byte* bmpPtr = (byte*)data.Scan0.ToPointer();

    //fixed (byte* rawPtr = raw)
    // 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
    // }
    // }
    }
    }
    }
    catch (Exception e)
    {
    return null;
    }
    finally
    {
    bmp.UnlockBits(data);
    //bmp.Save(@"C:\JK.bmp", ImageFormat.Bmp);
    }

    return raw;

    }

    Thursday, June 12, 2014 9:54 AM

All replies