- 首先,抱歉这么晚才回复...
- 其次,实际上我不知道为什么你会在不同的操作系统版本上得到不同的结果。
无论如何,这是一个低级(且丑陋)的解决方法。它专为 JPEG 图像而设计:
Public Shared Function GetJpegBpp(FileName As String) As Integer
Dim len As Integer
Dim fp As FileStream = Nothing
Dim marker(1) As Byte
Dim data(15) As Byte
Dim components As Byte = 0
GetJpegBpp = -2
Try
fp = New FileStream(FileName, FileMode.Open, FileAccess.Read)
GetJpegBpp = -1
If fp.Read(marker, 0, 2) < 2 OrElse marker(0) <> &HFF OrElse marker(1) <> &HD8 Then Exit Function
Do
If fp.Read(marker, 0, 2) < 2 OrElse marker(0) <> &HFF OrElse (marker(1) > 1 And marker(1) < &HC0) Then Exit Function
If (marker(1) < &HD0 Or marker(1) > &HD9) AndAlso marker(1) > 1 Then
If fp.Read(data, 0, 2) < 2 Then Exit Function
len = (CInt(data(0)) << 8) Or data(1)
len -= 2
If len < 0 Then Exit Function
If (marker(1) >= &HC0) And (marker(1) <= &HC3) Then
If len < 9 OrElse fp.Read(data, 0, 6) < 6 Then Exit Function
components = data(5)
If components = 0 OrElse components = 2 OrElse components > 4 OrElse (components * 3 + 6) <> len Then Exit Function
len -= 6
ElseIf marker(1) = &HDA Then
If len < (4 + 2 * components) Or (fp.ReadByte() <> components) Then Exit Function
len -= 1
End If
fp.Position += len
End If
Loop Until marker(1) = &HDA Or marker(1) = &HD9
If components = 0 OrElse marker(1) = &HD9 OrElse (fp.Length - fp.Position) < 3 Then Exit Function
Catch
Exit Function
Finally
If Not fp Is Nothing Then fp.Close()
End Try
GetJpegBpp = components * 8
End Function
您需要替换此行
iscmyk = ((DirectCast(img.Flags, Imaging.ImageFlags) And Imaging.ImageFlags.ColorSpaceCmyk) = Imaging.ImageFlags.ColorSpaceCmyk)
有了这个:
iscmyk = (GetJpegBpp(ofd.FileName) = 32)
最后,我还没有用 CMYK JPEG 图像测试过这段代码,但我想它应该可以工作......