vb.net 如何从文件中读取图像

 


    
Public Function GetImageFromFile(ByVal pstrFileName As StringAs Boolean
      
Dim strExt As String = "" '扩展文件名
      Dim blnFlag As Boolean = False

      
If ((pstrFileName.IndexOf(":"< 0AndAlso (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(00), 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 = 0Then
              
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

posted @ 2008-06-14 09:27  追风  阅读(2854)  评论(0编辑  收藏  举报