Need C# function to convert grayscale TIFF to black & white (monochrome/1BPP) TIFF

前端 未结 7 1682
清歌不尽
清歌不尽 2021-01-14 09:18

I need a C# function that will take a Byte[] of an 8 bit grayscale TIFF, and return a Byte[] of a 1 bit (black & white) TIFF.

I\'m fairly new to working with TIF

相关标签:
7条回答
  • 2021-01-14 10:03

    Something like this might work, I haven't tested it. (Should be easy to C# it.)

        Dim bmpGrayscale As Bitmap = Bitmap.FromFile("Grayscale.tif")
        Dim bmpMonochrome As New Bitmap(bmpGrayscale.Width, bmpgrayscale.Height, Imaging.PixelFormat.Format1bppIndexed)
        Using gfxMonochrome As Graphics = Graphics.FromImage(bmpMonochrome)
            gfxMonochrome.Clear(Color.White)
        End Using
        For y As Integer = 0 To bmpGrayscale.Height - 1
            For x As Integer = 0 To bmpGrayscale.Width - 1
                If bmpGrayscale.GetPixel(x, y) <> Color.White Then
                    bmpMonochrome.SetPixel(x, y, Color.Black)
                End If
            Next
        Next
        bmpMonochrome.Save("Monochrome.tif")
    

    This might be a better way still:

    Using bmpGrayscale As Bitmap = Bitmap.FromFile("Grayscale.tif")
        Using bmpMonochrome As New Bitmap(bmpGrayscale.Width, bmpgrayscale.Height, Imaging.PixelFormat.Format1bppIndexed)
            Using gfxMonochrome As Graphics = Graphics.FromImage(bmpMonochrome)
                gfxMonochrome.CompositingQuality = Drawing2D.CompositingQuality.HighQuality
                gfxMonochrome.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
                gfxMonochrome.DrawImage(bmpGrayscale, new Rectangle(0, 0, bmpMonochrome.Width, bmpMonochrome.Height)
            End Using
            bmpMonochrome.Save("Monochrome.tif")
        End Using
    End Using
    

    I believe the term you are looking for is "resampling".

    0 讨论(0)
提交回复
热议问题