首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >DirectCast不工作

DirectCast不工作
EN

Stack Overflow用户
提问于 2012-10-17 13:44:21
回答 1查看 560关注 0票数 0

‘'ofd是打开的文件对话框

代码语言:javascript
复制
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

我需要检查图像是cmyk还是rgb,如果是cmyk,则iscmyk返回true如果不是cmyk,则iscmyk返回false在我的Windows7 pc中,它返回false,每个图像都返回false,但在XP中它返回完美答案

为什么它不能在我的其他win7电脑上工作?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-10-30 13:45:34

我不知道为什么你会在不同的操作系统版本上得到不同的结果。

无论如何,这里有一个低级的(丑陋的)变通方法。它是为JPEG图像设计的:

代码语言:javascript
复制
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

您需要替换此行

代码语言:javascript
复制
iscmyk = ((DirectCast(img.Flags, Imaging.ImageFlags) And Imaging.ImageFlags.ColorSpaceCmyk) = Imaging.ImageFlags.ColorSpaceCmyk)

有了这个:

代码语言:javascript
复制
iscmyk = (GetJpegBpp(ofd.FileName) = 32)

最后,我还没有用CMYK JPEG图像测试这段代码,但我猜它应该可以工作……

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12927830

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档