locked
Does .Net 3.5 or .Net 4.0 support Format16bppGrayScale TIFFS RRS feed

  • Question

  • I've read a lot of articles that say that Format16bppGrayScale is not supported in .NET 2.0 (and confirmed this), however there are "suggestions" that it will work in .Net 3.5 +

     

    I have a byte[] array that I want to save in 16bit Tiff format. 

     

    I've tried the SetPixel and LockBits examples out there (in .Net4.0) and still get the same errors as in .Net 2.0.

     

     

    Wednesday, November 17, 2010 8:11 PM

Answers

  • unfortunatly, my application is written in C# so I can't confirm the proposed answer, hoever I have also managed to figure it out in C#..


    The code takes a Byte[] Array and converts it to ushort, then creates the bitmap, and saves it..

     

     

     

    private BitmapSource ConvertImage(ushort[] data)

    {

     

    System.Windows.Media.

     

    PixelFormat pf = System.Windows.Media.PixelFormats.Gray16;

     

     

    // stride is width *2 if this wron you get a general "parameter is wrong" message but no indication which one or why

     

     

    int stride = width *2 ;

     

     

    BitmapPalette pallet = BitmapPalettes.Gray16;

     

     

    double dpi = 96;

    oBmpOut =

     

    BitmapSource.Create(width, height, dpi, dpi, pf, pallet, data, stride);

     

     

    return (oBmpOut);

     

    }

     

     

     

    private void TIFF_Save(BitmapSource bmpSource, string sFileName)

    {

     

     

    using (FileStream fs = new FileStream(sFileName, FileMode.Create))

    {

     

     

    TiffBitmapEncoder encoder = new TiffBitmapEncoder();

    encoder.Compression =

     

    TiffCompressOption.Lzw;

    encoder.Frames.Add(

     

    BitmapFrame.Create(bmpSource));

    encoder.Save(fs);

    }

    }

     

     

    private ushort[] ConvertByteToUShort(byte[] bytes)

    {

     

     

    ushort[] _data = new ushort[bytes.Length / 2];

     

     

    int index = 0;

     

     

    int ucounter = 0;

    height = _data.Length / width;

     

     

    while (index < bytes.Length)

    {

    _data[ucounter++] =

     

    BitConverter.ToUInt16(bytes, index);

    index += 2;

    }

     

     

    return (_data);

    }

     

     

     

     

    public void ConvertTiff(byte[] bytes, string dst)

    {

     

    data = ConvertByteToUShort(bytes);

    oBmpOut = ConvertImage(data);

    TIFF_Save(oBmpOut, dst);

    Log (

     

    "Done:"+dst);

    }

     


    Darryl Burke
    • Marked as answer by D Burke Monday, November 22, 2010 3:27 PM
    Monday, November 22, 2010 3:27 PM

All replies

  • You should be able to work with 16bit GrayScale in .NET 2.0, you just cant display it or get a graphics for it.  That also means you can't use Get and Set Pixel. 

    "I have a byte[] array that I want to save in 16bit Tiff format."  Post your code.

    "I've tried the SetPixel and LockBits examples out there (in .Net4.0) and still get the same errors as in .Net 2.0."  You shouldn't have problems with LockBits.  SetPixel and LockBits aren't .NET 4.0 methods.  WPF methods work with 16bit GrayScale images.

    • Proposed as answer by jake121r Friday, March 27, 2015 3:25 PM
    Wednesday, November 17, 2010 9:37 PM
  • I've tried using lock bits, but when I go to save the file, I get the generic GDI+ error..  (as everyone else does)..  I don't need to deal with the data visually ( I just want to save it to a file)..

     

     


    Darryl Burke
    • Proposed as answer by jake121r Friday, March 27, 2015 3:28 PM
    Thursday, November 18, 2010 2:39 PM
  •     Dim B(100 * 100 * 2 - 1) As Byte
        Dim BmpSrc As BitmapSource = BitmapSource.Create(100, 100, 96, 96, _
                                                         PixelFormats.Gray16, _
                                                         
    Nothing, _
                                                         B, _
                                                         200)

        
    Dim TifEnc As New TiffBitmapEncoder
        TifEnc.Frames.Add(BitmapFrame.Create(BmpSrc))
        
    Using FS As New FileStream("16Bit.tif", FileMode.Create)
          TifEnc.Save(FS)
        
    End Using

    • Proposed as answer by liurong luo Monday, November 22, 2010 4:23 AM
    Thursday, November 18, 2010 3:42 PM
  • unfortunatly, my application is written in C# so I can't confirm the proposed answer, hoever I have also managed to figure it out in C#..


    The code takes a Byte[] Array and converts it to ushort, then creates the bitmap, and saves it..

     

     

     

    private BitmapSource ConvertImage(ushort[] data)

    {

     

    System.Windows.Media.

     

    PixelFormat pf = System.Windows.Media.PixelFormats.Gray16;

     

     

    // stride is width *2 if this wron you get a general "parameter is wrong" message but no indication which one or why

     

     

    int stride = width *2 ;

     

     

    BitmapPalette pallet = BitmapPalettes.Gray16;

     

     

    double dpi = 96;

    oBmpOut =

     

    BitmapSource.Create(width, height, dpi, dpi, pf, pallet, data, stride);

     

     

    return (oBmpOut);

     

    }

     

     

     

    private void TIFF_Save(BitmapSource bmpSource, string sFileName)

    {

     

     

    using (FileStream fs = new FileStream(sFileName, FileMode.Create))

    {

     

     

    TiffBitmapEncoder encoder = new TiffBitmapEncoder();

    encoder.Compression =

     

    TiffCompressOption.Lzw;

    encoder.Frames.Add(

     

    BitmapFrame.Create(bmpSource));

    encoder.Save(fs);

    }

    }

     

     

    private ushort[] ConvertByteToUShort(byte[] bytes)

    {

     

     

    ushort[] _data = new ushort[bytes.Length / 2];

     

     

    int index = 0;

     

     

    int ucounter = 0;

    height = _data.Length / width;

     

     

    while (index < bytes.Length)

    {

    _data[ucounter++] =

     

    BitConverter.ToUInt16(bytes, index);

    index += 2;

    }

     

     

    return (_data);

    }

     

     

     

     

    public void ConvertTiff(byte[] bytes, string dst)

    {

     

    data = ConvertByteToUShort(bytes);

    oBmpOut = ConvertImage(data);

    TIFF_Save(oBmpOut, dst);

    Log (

     

    "Done:"+dst);

    }

     


    Darryl Burke
    • Marked as answer by D Burke Monday, November 22, 2010 3:27 PM
    Monday, November 22, 2010 3:27 PM
  • I just turn to git hub for answers and use the debug a lot lol . I knowsounds dumb ..
    Friday, March 27, 2015 7:37 PM