VB中用Picturebox显示图片的问题
VB中picturebox不能显示PNG格式图片。如果Picturebox显示到PNG图片的时候就出错。有什么办法不提示错误,直接Picturebox为空有好的代码的话可...
VB中picturebox不能显示PNG格式图片。如果Picturebox显示到PNG图片的时候就出错。有什么办法不提示错误,直接Picturebox为空
有好的代码的话可以再另外加分! 展开
有好的代码的话可以再另外加分! 展开
6个回答
展开全部
其实最简单的是利用捕捉错误的方法,而且该方法适用于任何VB不兼容的图片格式的情况(当然包括PNG图片):
Private Sub Command1_Click()
On Error Resume Next '开始捕捉错误
Picture1.Picture = LoadPicture(App.Path & "\1.NPG")
if err.Number <>0 then Picture1.Picture = LoadPicture() '如果不兼容则置空
End Sub
Private Sub Command1_Click()
On Error Resume Next '开始捕捉错误
Picture1.Picture = LoadPicture(App.Path & "\1.NPG")
if err.Number <>0 then Picture1.Picture = LoadPicture() '如果不兼容则置空
End Sub
展开全部
可以通过判断文件的扩展名,或者文件的格式,直接忽略掉这类文件。
判断文件扩展名是不是png
Private Function GetExt(ByVal sFileName As String) As String
GetExt = Right(sFileName, Len(sFileName) - InStrRev(sFileName, "."))
End Function
if lcase(GetExt("文件")) = "png" then
picture1.picture = loadpicture()
else
picture1.picture = loadpicture("文件")
end if
下面这个函数是判断文件格式是否是PNG的。
Private Function ImageIsPNG(ByVal sFileName As String) As Boolean
Const BUFFERSIZE As Long = 65535
Dim bBuf(BUFFERSIZE) As Byte
Dim iFN As Integer
iFN = FreeFile
ImageIsPNG = False
Open sFileName For Binary As iFN
Get #iFN, 1, bBuf()
Close iFN
If bBuf(0) = 137 And bBuf(1) = 80 And bBuf(2) = 78 Then
ImageIsPNG = True
End If
End Function
if ImageIsPNG("文件") then
picture1.picture = loadpicture()
else
picture1.picture = loadpicture("文件")
end if
其实png也可以通过gdi+显示的,
判断文件扩展名是不是png
Private Function GetExt(ByVal sFileName As String) As String
GetExt = Right(sFileName, Len(sFileName) - InStrRev(sFileName, "."))
End Function
if lcase(GetExt("文件")) = "png" then
picture1.picture = loadpicture()
else
picture1.picture = loadpicture("文件")
end if
下面这个函数是判断文件格式是否是PNG的。
Private Function ImageIsPNG(ByVal sFileName As String) As Boolean
Const BUFFERSIZE As Long = 65535
Dim bBuf(BUFFERSIZE) As Byte
Dim iFN As Integer
iFN = FreeFile
ImageIsPNG = False
Open sFileName For Binary As iFN
Get #iFN, 1, bBuf()
Close iFN
If bBuf(0) = 137 And bBuf(1) = 80 And bBuf(2) = 78 Then
ImageIsPNG = True
End If
End Function
if ImageIsPNG("文件") then
picture1.picture = loadpicture()
else
picture1.picture = loadpicture("文件")
end if
其实png也可以通过gdi+显示的,
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
VB不兼容PNG格式图片,如果非要用就只能自己研究PNG格式了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
给我发一个png图片我试一下,我想到了一个主意,试试看
Private Sub Command1_Click()
On Error GoTo 10000
Picture1.Picture = LoadPicture(App.Path & "\1.NPG")
10000:
Picture1.Picture = LoadPicture()
End Sub
Private Sub Command1_Click()
On Error GoTo 10000
Picture1.Picture = LoadPicture(App.Path & "\1.NPG")
10000:
Picture1.Picture = LoadPicture()
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
错误陷阱呀。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询