VB.NET可以帮助我们轻松的实现许多功能。下面我们可以通过一段代码范例来对VB.NET读取图像这一操作方法进行详细的解读,从而对VB.NET的灵活简便性有一个深入的了解,帮助我们提高编程效率。#t#
VB.NET读取图像代码示例:
- Public Function GetImageFromFile()
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
VB.NET读取图像取得扩展名
- 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 GraphicsGraphics =
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
VB.NET读取图像的相关介绍就为大家介绍到这里。