Public Function GetImageFromFile(ByVal pstrFileName As String) As Boolean
Dim strExt As String = "" '扩展文件名
Dim blnFlag As Boolean = False
If ((pstrFileName.IndexOf(":") < 0) AndAlso (Not PictureHolder.mstrPath Is Nothing)) Then
pstrFileName = (PictureHolder.mstrPath & pstrFileName)
End If
If Not File.Exists(pstrFileName) Then
Return False
End If
'取得扩展名
strExt = Path.GetExtension(pstrFileName).ToLower
If strExt.Equals(".cur") Then
Try
Dim cursor As New Windows.Forms.Cursor(pstrFileName)
Dim targetRect As New Rectangle(New Point(0, 0), cursor.Size)
Me.mobjImage = New Bitmap(cursor.Size.Width, cursor.Size.Height)
Dim g As Graphics = Graphics.FromImage(Me.mobjImage)
cursor.Draw(g, targetRect)
cursor.Dispose()
Return True
Catch obj1 As Exception
Return False
End Try
End If
Try
Using stream As IO.FileStream = New IO.FileStream(pstrFileName, FileMode.Open, FileAccess.Read)
Dim stream2 As New IO.MemoryStream
Do While True
Dim count As Integer = stream.Read(PictureHolder.mbtyImage, 0, PictureHolder.mbtyImage.Length)
If (count = 0) Then
Exit Do
End If
stream2.Write(PictureHolder.mbtyImage, 0, count)
Loop
stream2.Flush()
stream2.Position = 0
'若为 ico 类型,则使用 Drawing.Icon 打开图像
If strExt.Equals(".ico") Then
Me.mobjImage = New Drawing.Icon(stream2).ToBitmap
Else
Me.mobjImage = Drawing.Image.FromStream(stream2)
End If
blnFlag = True
End Using
Catch obj2 As Exception
blnFlag = False
End Try
Return blnFlag
End Function