[Delphi (Object Pascal)] Delphi根据头信息获取常用图片文件格式信息 →→→→→进入此内容的聊天室

来自 , 2021-01-07, 写在 Delphi (Object Pascal), 查看 132 次.
URL http://www.code666.cn/view/968c9b4f
  1.  
  2. '枚举图片格式种类
  3. Public Enum ImageForm
  4.   [BMP] = 0
  5.   [JPEG] = 1
  6.   [GIF87] = 2
  7.   [GIF89] = 3
  8.   [PNG] = 4
  9.   [TGA Normal] = 5 'TGA未压缩
  10.    [TGA RLE] = 6     'TGA经过RLE压缩后的
  11.   [PCX] = 7
  12.   [TIFF] = 8
  13.   [ICO] = 9
  14.   [CUR] = 10
  15.   [IFF] = 11
  16.   [ANI] = 12
  17.   [Other] = 13
  18.   [FileError] = 14
  19. End Enum
  20.  
  21.  
  22.  
  23. '-----------------------------------------------------------------------
  24. '-----------------------------------------------------------------------
  25. '--   标题:获取图片的格式
  26. '--   作者:BEAR-BEN
  27. '--   制作日期:2007-8-5
  28. '--   支持的格式:BMP,JPEG,GIF,PNG,TGA,PCX,TIFF,
  29. '                             ICO,CUR,IFF,ANI 共11种格式
  30. '--   版本:1.0
  31. '--   使用者请保留版权,谢谢!
  32. '-----------------------------------------------------------------------
  33. '-----------------------------------------------------------------------
  34. Public Function GetImageFileForm(ImageFilePath As String) As ImageForm
  35. Dim FileHeader(5) As Byte, FileNumber As Integer
  36.  
  37.    GetImageFileForm = FileError
  38.  
  39.    If Dir(ImageFilePath) <> "" Then    '判断图片文件是否存在
  40.     FileNumber = FreeFile
  41.     Open ImageFilePath For Binary As #FileNumber
  42.       Get FileNumber, , FileHeader()   '二进制流读取图片前5个字符
  43.      Close FileNumber
  44.    
  45.      GetImageFileForm = Other
  46.    
  47.    
  48.     '文件头标识识别
  49.     If (FileHeader(0) = 66) And (FileHeader(1) = 77) Then
  50.       GetImageFileForm = BMP
  51.       Exit Function
  52.     End If
  53.     If (FileHeader(0) = 255) And (FileHeader(1) = 216) Then
  54.       GetImageFileForm = JPEG    
  55.       Exit Function
  56.     End If
  57.     If (FileHeader(0) = 71) And (FileHeader(1) = 73) And (FileHeader(2) = 70) And (FileHeader(4) = 57) Then
  58.       GetImageFileForm = GIF89
  59.       Exit Function
  60.     End If
  61.     If (FileHeader(0) = 71) And (FileHeader(1) = 73) And (FileHeader(2) = 70) And (FileHeader(4) = 55) Then
  62.       GetImageFileForm = GIF87
  63.       Exit Function
  64.     End If
  65.     If (FileHeader(0) = 137) And (FileHeader(1) = 80) Then
  66.       GetImageFileForm = PNG  
  67.       Exit Function
  68.     End If
  69.     If (FileHeader(0) = 73) And (FileHeader(1) = 73) Then
  70.       GetImageFileForm = TIFF  'TIFF 摩托罗拉
  71.        Exit Function
  72.      End If
  73.      If (FileHeader(0) = 77) And (FileHeader(1) = 77) Then
  74.        GetImageFileForm = TIFF  'TIFF Intel
  75.       Exit Function
  76.     End If
  77.     If (FileHeader(2) = 1) And (FileHeader(4) = 1) Then
  78.       GetImageFileForm = ICO
  79.       Exit Function
  80.     End If
  81.     If (FileHeader(2) = 2) And (FileHeader(4) = 1) Then
  82.       GetImageFileForm = CUR
  83.       Exit Function
  84.     End If
  85.     If (FileHeader(0) = 82) And (FileHeader(1) = 73) And (FileHeader(2) = 70) And (FileHeader(3) = 70) Then
  86.       GetImageFileForm = ANI  
  87.       Exit Function
  88.     End If
  89.     If (FileHeader(2) = 2) And (FileHeader(4) = 0) Then
  90.       GetImageFileForm = [TGA Normal]
  91.       Exit Function
  92.     End If
  93.     If (FileHeader(2) = 16) And (FileHeader(4) = 0) Then
  94.       GetImageFileForm = [TGA RLE]
  95.       Exit Function
  96.     End If
  97.     If (FileHeader(0) = 10) Then
  98.       GetImageFileForm = PCX
  99.       Exit Function
  100.     End If
  101.   End If
  102. End Function
  103. //delphi/8991

回复 "Delphi根据头信息获取常用图片文件格式信息"

这儿你可以回复上面这条便签

captcha