简体   繁体   English

DirectCast无法正常工作

[英]DirectCast not working

'ofd is open file dialog 'ofd是打开文件对话框

Dim img As Bitmap
Dim iscmyk As Boolean
Dim i As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


    ofd.Filter = "Jpg Image(*.jpg)|*.jpg"
    If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
        img = Bitmap.FromFile(ofd.FileName)
        iscmyk = ((DirectCast(img.Flags, Imaging.ImageFlags) And Imaging.ImageFlags.ColorSpaceCmyk) = Imaging.ImageFlags.ColorSpaceCmyk)
    End If
    img = New Bitmap(img, New Size(120, 190))
    MsgBox("cmyk = " & iscmyk)
    PictureBox1.Image = img
End Sub

i need to check if the image is cmyk or rgb if its cmyk then iscmyk returns true if its not cmyk then iscmyk returns false in my windows 7 pc its returns false for each & every image but in XP it returns perfect answer 我需要检查图像是cmyk还是rgb如果它的cmyk然后iscmyk返回true如果它不是cmyk然后iscmyk在我的Windows 7 pc中返回false它为每个图像返回false但在XP中它返回完美的答案

why its not working in my other win7 pcs??? 为什么它不能在我的其他win7个电脑上工作???

  • First, sorry for responding so late... 首先,抱歉这么晚回复......
  • Second, actually I don't know why you get different results on different OS versions. 其次,实际上我不知道为什么你会在不同的OS版本上得到不同的结果。

Anyway, here is a low-level (and ugly) workaround. 无论如何,这是一个低级(和丑陋)的解决方法。 It's designed for JPEG images: 它专为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

You need to replace this line 你需要更换这一行

iscmyk = ((DirectCast(img.Flags, Imaging.ImageFlags) And Imaging.ImageFlags.ColorSpaceCmyk) = Imaging.ImageFlags.ColorSpaceCmyk)

with this: 有了这个:

iscmyk = (GetJpegBpp(ofd.FileName) = 32)

Finally, I haven't tested this code with CMYK JPEG images, but I guess it should work... 最后,我还没有用CMYK JPEG图像测试这段代码,但我想它应该可行...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM