Search code examples
c#vb.netscreenshottiff

Save a picture in Tiff format without compression


I just found out TIFF has several formats. For some reason when using

Using bm As New Bitmap(rect.Width, rect.Width)
bm.Save("C:\testfolder\screenshot.png", Imaging.ImageFormat.Tiff)

the format becomes LZW compressed, which I can't use in a third-party library.

After some searching I found that .NET does provide a library to save as TIFF with different compression options, I wanted to try them all but I don't know how to implement it. Or I should say I didn't figure out how to use it:

https://learn.microsoft.com/en-us/dotnet/api/system.windows.media.imaging.tiffcompressoption?view=netframework-4.8#examples

The purpose is I want to get a region screenshot and save it as TIFF.

Dim stream As New FileStream("new.tif", FileMode.Create)
Dim encoder As New TiffBitmapEncoder()
encoder.Compression = TiffCompressOption.Zip
encoder.Frames.Add(BitmapFrame.Create(image))   <--what is this "image"?
encoder.Save(stream)

What is that image entity? A stream? How to get a region of captured screenshot to be saved as a TIFF?

If

Using bm As New Bitmap(rect.Width, rect.Width) 

I don't mind to save it first then only reread the bmp stream. The only problem is I have no idea what is that "image" given in the example stands for.

I read some info from here but I still haven't figured it out: https://learn.microsoft.com/en-us/dotnet/api/system.windows.media.imaging.bitmapframe.create?view=netframework-4.8

Kindly offer me an example how to use the code if you figured it out.


Solution

  • You can go the older System.Drawing way instead of System.Windows.Media way.

    Adapted only slightly from the documentation for the Encoder.Compression Field:

    Imports System.Drawing.Imaging
    '' .....
    
    Private Shared Function GetEncoderInfo(ByVal mimeType As String) As ImageCodecInfo
        Dim j As Integer
        Dim encoders() As ImageCodecInfo
        encoders = ImageCodecInfo.GetImageEncoders()
    
        j = 0
        While j < encoders.Length
            If encoders(j).MimeType = mimeType Then
                Return encoders(j)
            End If
            j += 1
        End While
        Return Nothing
    
    End Function
    
    Sub SaveAsTiff()
        Dim sampleFile = "C:\temp\ToTiff.png"
    
        Using bmp = Image.FromFile(sampleFile)
            Dim myImageCodecInfo = GetEncoderInfo("image/tiff")
    
            Dim myEncoder As Imaging.Encoder = Imaging.Encoder.Compression
            Dim myEncoderParameters = New EncoderParameters(1)
            Dim myEncoderParameter = New EncoderParameter(myEncoder, EncoderValue.CompressionNone)
            myEncoderParameters.Param(0) = myEncoderParameter
    
            bmp.Save("C:\temp\ToTiff.tif", myImageCodecInfo, myEncoderParameters)
    
        End Using
    
    End Sub
    

    However, it does not allow for ZIP compression. But that may not matter to you: Which TIFF image compression is better, LZW or ZIP?

    Also, for screenshots, you might want to consider using the PNG format, especially if the images will be used on the web.