LoadPicture载入图片的问题
最近写了一个程序,要大量接收来自客户上传的图片,客户先上传到服务器,我在从服务器中下载,然后加载到Picture中,可能是客户上传的问题,有时会出现图片损坏的情况,一但我这里收到损坏的图片,程序就会死掉。
现在想在载入前判断,但是结果一样,判断时就死了
谁有好的方法判断图片是否有效,谢谢
Private Sub Command1_Click() Dim Pic As Picture Set Pic = LoadPicture(App.Path & "\正常图片.jpg") Picture1.Picture = PicEnd SubPrivate Sub Command2_Click() Dim Pic As Picture On Error Resume Next Set Pic = LoadPicture(App.Path & "\有问题的图片.jpg") Picture1.Picture = PicEnd SubPrivate Sub Command3_Click() MsgBox PicEffective(App.Path & "\有问题的图片.jpg")End SubPrivate Function PicEffective(FileName As String) As Boolean Dim Pic As StdPicture On Error GoTo er PicEffective = True Set Pic = LoadPicture(FileName) Set Pic = Nothing Exit Functioner: PicEffective = FalseEnd Function
Private Sub Command1_Click()'找出文件夹内文件最新的更新时间(包括里面的子文件夹的文件哈) 要把这个文件夹内的所有文件的最后修改时间都要检查一次,并取出近的一个时间,(主要是检查这个文件夹里面的内容有没有更新),并取出这个文件最后一次的更新时间.环境是VB6Dim after As DoubleDim f As IntegerDim dn As StringDim fn As StringDim ft As StringDim ft_date As DateCommand1.Enabled = FalseOn Error GoTo ERR0Kill "c:\files.txt"dn = "c:\windows"On Error GoTo ERR1Shell ("cmd /c dir " & dn & "\*.* /a-d /b /s /o-d >c:\files.txt")after = Now + 60# / 3600# / 24#f = FreeFile()DoREOPEN1: DoEvents If Now > after Then MsgBox "Wait c:\files.txt 60s overtime!" Exit Sub End If Open "c:\files.txt" For Input Lock Read Write As #f Line Input #f, fn Close #f Exit DoLoopOn Error GoTo ERR0Kill "c:\files.txt"On Error GoTo ERR2Shell ("cmd /c dir " & Chr(34) & fn & Chr(34) & ">c:\files.txt")after = Now + 60# / 3600# / 24#f = FreeFile()DoREOPEN2: DoEvents If Now > after Then MsgBox "Wait c:\files.txt 60s overtime!" Exit Sub End If Open "c:\files.txt" For Input Lock Read Write As #f Line Input #f, ft Line Input #f, ft Line Input #f, ft Line Input #f, ft Line Input #f, ft Line Input #f, ft Close #f Kill "c:\files.txt" Exit DoLoopft = Left(ft, 17)ft_date=CDate(ft)MsgBox "The newest file in [" & dn & "] is [" & fn & "], datetime is [" & ft_date & "]"Command1.Enabled = TrueExit SubERR0: Resume NextERR1: Resume REOPEN1ERR2: Resume REOPEN2End Sub
[解决办法]
下面代码 时间有限 未再做细节的处理 只给你思路
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal Hprocess As Long, ByVal uExitCode As Long) As Long
Dim Rtn&, Rtn2&, Phwnd&, StartTm&
Private Sub Command1_Click()
MsgBox IIf(ChkValidPic("c:\test.jpg"), "正常图片", "无效图片")
End Sub
Function ChkValidPic(PicNm$) As Boolean
Rtn = Shell("cmd /c mspaint " & PicNm, vbHide)
StartTm = Timer
Do
Loop Until Timer - StartTm >= 0.5
Phwnd = FindWindow(vbNullString, "画图")
ChkValidPic = IIf(Phwnd > 0, False, True)
If Phwnd > 0 Then Call CloseExe(Phwnd)
End Function
Sub CloseExe(Pid As Long)
Dim Hprocess&
Hprocess = OpenProcess(1, False, Pid)
TerminateProcess Hprocess, 1
CloseHandle Hprocess
End Sub