我刚发现TIFF有几种格式。出于某种原因使用时
Using bm As New Bitmap(rect.Width, rect.Width)
bm.Save("C:\testfolder\screenshot.png", Imaging.ImageFormat.Tiff)
格式变为LZW压缩,我不能在第三方库中使用。
经过一番搜索,我发现.NET确实提供了一个库来保存为具有不同压缩选项的TIFF,我想尝试所有这些,但我不知道如何实现它。或者我应该说我没弄明白如何使用它:
目的是我想获得一个区域截图并将其保存为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)
什么是image
实体?一条小溪?如何将捕获的屏幕截图区域保存为TIFF?
如果
Using bm As New Bitmap(rect.Width, rect.Width)
我不介意先保存它然后重新读取bmp流。唯一的问题是我不知道示例中给出的“图像”是什么。
我从这里读了一些信息,但我还是没想出来:https://docs.microsoft.com/en-us/dotnet/api/system.windows.media.imaging.bitmapframe.create?view=netframework-4.8
请告诉我一个如何使用代码的例子,如果你想出来的话。
您可以使用较旧的System.Drawing方式而不是System.Windows.Media方式。
仅适用于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
但是,它不允许ZIP压缩。但这对你来说可能并不重要:Which TIFF image compression is better, LZW or ZIP?
此外,对于屏幕截图,您可能需要考虑使用PNG格式,尤其是如果图像将在网络上使用。