Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1330945
  • 博文数量: 124
  • 博客积分: 5772
  • 博客等级: 大校
  • 技术积分: 1647
  • 用 户 组: 普通用户
  • 注册时间: 2010-09-27 10:39
文章分类

全部博文(124)

文章存档

2020年(1)

2019年(1)

2018年(5)

2017年(2)

2016年(17)

2015年(3)

2014年(7)

2013年(11)

2012年(13)

2011年(30)

2010年(34)

分类: WINDOWS

2011-08-30 14:57:13

手头有很多照片,想通过拍摄时间来对照片命名,这样方便进行管理。前段时间写了一个类似的程序,但是使用的是照片的创建时间,但有的照片的创建时间和拍摄时间并不一致,因此这次重写了一下这个程序,使用的是照片的拍摄时间来命名。本程序参考了网上的部分代码,如有侵权,请告知。
'*****************************************************************
'Name:filename.frm
'Desc:使用照片的拍摄日期和时间,命名该照片
'Parameter:
'Return:
'Author: yoyoba(stuyou@126.com)
'Date: 2011-8-30
'Modify:2011-8-30
'*****************************************************************

'需要建立一个FileListBox控件和一个CommandButton控件
  1. Private Type GdiplusStartupInput
  2.     GdiplusVersion As Long
  3.     DebugEventCallback As Long
  4.     SuppressBackgroundThread As Long
  5.     SuppressE xternalCodecs As Long
  6. End Type


  7. Private Type PropertyItem
  8.    propId As Long ' ID of this property
  9.    Length As Long ' Length of the property value, in bytes
  10.    Type As Long ' Type of the value, as one of TAG_TYPE_XXX defined above
  11.    Value As Long ' property value
  12. End Type


  13. Private Declare Function GdiplusStartup Lib "gdiplus" (Token As Long, inputbuf As GdiplusStartupInput, Optional ByVal outputbuf As Long = 0) As Long
  14. Private Declare Sub GdiplusShutdown Lib "gdiplus" (ByVal Token As Long)
  15. Private Declare Function GdipLoadImageFromFile Lib "gdiplus" (ByVal filename As Long, hImage As Long) As Long
  16. Private Declare Function GdipDisposeImage Lib "gdiplus" (ByVal Image As Long) As Long

  17. Private Declare Function GdipGetPropertyCount Lib "gdiplus" (ByVal Image As Long, numOfProperty As Long) As Long
  18. Private Declare Function GdipGetPropertyIdList Lib "gdiplus" (ByVal Image As Long, ByVal numOfProperty As Long, list As Long) As Long
  19. Private Declare Function GdipGetPropertyItemSize Lib "gdiplus" (ByVal Image As Long, ByVal propId As Long, Size As Long) As Long
  20. Private Declare Function GdipGetPropertyItem Lib "gdiplus" (ByVal Image As Long, ByVal propId As Long, ByVal propSize As Long, Buffer As Long) As Long
  21. Private Declare Function GdipGetPropertySize Lib "gdiplus" (ByVal Image As Long, totalBufferSize As Long, numProperties As Long) As Long
  22. Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpDst As Any, lpSrc As Any, ByVal ByteLength As Long)



  23. Private Function GetPhotoDate(ImagePath As String) As String
  24.     Dim Bitmap As Long
  25.     Dim Token As Long
  26.     Dim Index As Long
  27.     Dim PropertyCount As Long
  28.     Dim ItemSize As Long
  29.     Dim Prop As PropertyItem
  30.     Dim GdipInput As GdiplusStartupInput
  31.     Const PropertyTagExifDTOrig As Long = &H9003& ' Date & time of original

  32.     GdipInput.GdiplusVersion = 1
  33.     GdiplusStartup Token, GdipInput
  34.     GdipLoadImageFromFile StrPtr(ImagePath), Bitmap
  35.     GdipGetPropertyCount Bitmap, PropertyCount
  36.     ReDim PropertyList(PropertyCount - 1) As Long
  37.     GdipGetPropertyIdList Bitmap, PropertyCount, PropertyList(0)
  38.     For Index = 0 To PropertyCount - 1
  39.         GdipGetPropertyItemSize Bitmap, PropertyList(Index), ItemSize
  40.         ReDim Buffer(ItemSize - 1) As Byte
  41.         GdipGetPropertyItem Bitmap, PropertyList(Index), ItemSize, ByVal VarPtr(Buffer(0))
  42.         CopyMemory Prop, ByVal VarPtr(Buffer(0)), Len(Prop)
  43.         ReDim Data(ItemSize - 16) As Byte
  44.         CopyMemory Data(0), ByVal Prop.Value, ItemSize - 16
  45.         Select Case PropertyList(Index)
  46.         Case PropertyTagExifDTOrig
  47.             GetPhotoDate = StrConv(Data, vbUnicode)
  48.         End Select
  49.     Next
  50.     GdipDisposeImage Bitmap
  51.     GdiplusShutdown Token
  52. End Function


  53. Private Sub Command1_Click()
  54.     Dim filename, newfilename As String, filedt As String, exname As String
  55.     Dim i%
  56.     For i = 0 To File1.ListCount - 1
  57.         File1.ListIndex = i
  58.         filename = CStr(File1.filename)
  59.         exname = Right(filename, 4)
  60.         
  61.         filedt = GetPhotoDate("E:\Photo\" & filename)
  62.         filedt = Left(filedt, 19)
  63.         'MsgBox filedt
  64.         newfilename = getdtname(filedt)
  65.         newfilename = newfilename & exname
  66.         Name "E:\Photo\" & filename As "E:\Photo\" & newfilename
  67.     Next i
  68. End Sub


  69. Private Sub Form_Load()
  70.     File1.Path = "E:\Photo\"
  71. End Sub
  72. Private Function getdtname(dt As String) As String
  73.     '文件的创建时间格式为2010:05:13 19:30:23,即为形参dt。
  74.     '通过getdtname函数得到以文件创建时间为名字的文件名,格式为20100513193023
  75.     Dim dtarray() As String, darray() As String, tarray() As String
  76.     If Len(dt) > 0 Then
  77.         dtarray() = Split(dt)
  78.         'MsgBox dtarray(0)
  79.         'MsgBox dtarray(1)
  80.         darray() = Split(dtarray(0), ":")
  81.         'MsgBox darray(0)
  82.         'MsgBox darray(1)
  83.         'MsgBox darray(2)
  84.         tarray() = Split(dtarray(1), ":")
  85.         'MsgBox tarray(0)
  86.         'MsgBox tarray(1)
  87.         'MsgBox tarray(2)
  88.         If Len(darray(1)) = 1 Then darray(1) = 0 & darray(1)
  89.         If Len(darray(2)) = 1 Then darray(2) = 0 & darray(2)
  90.         If Len(tarray(0)) = 1 Then tarray(0) = 0 & tarray(0)
  91.         If Len(tarray(1)) = 1 Then tarray(1) = 0 & tarray(1)
  92.         If Len(tarray(2)) = 1 Then tarray(2) = 0 & tarray(2)
  93.         getdtname = darray(0) & darray(1) & darray(2) & tarray(0) & tarray(1) & tarray(2)
  94.     End If
  95. End Function

阅读(4824) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~