Dim vSecondarySurface As Surface
Dim vSecondarySurfaceCaps As New SurfaceCaps
Dim vSecondarySurfaceDesc As SurfaceDescriptionDirectX与VB.NET编程(十五)*色深理论与图片智能加载
'取得图片大小
Dim vBitmap As New Bitmap(OpenFileDialog1.FileName)
Dim vSize As Integer = vBitmap.Width * vBitmap.Height * Convert(vBitmap.PixelFormat)
'取得剩余显存空间
Dim vVideoFree As Integer = vDevice.HardwareCaps.VideoMemoryFree
'比较显存是否足够
If vVideoFree > vSize Then
'显存足够,使用显存进行存放
MsgBox("显存空间足够,剩余显存:" + vVideoFree.ToString + " 图片大小:" + vSize.ToString)
vSecondarySurfaceCaps.VideoMemory = True
Else
'显存不足,使用内存进行存放
MsgBox("显存空间不足,剩余显存:" + vVideoFree.ToString + " 图片大小:" + vSize.ToString)
vSecondarySurfaceCaps.SystemMemory = True
End If
'初始化表面、加载图片并显示剩余显存
vSecondarySurfaceCaps.OffScreenPlain = True
vSecondarySurfaceDesc = New SurfaceDescription(vSecondarySurfaceCaps)
vSecondarySurface = New Surface(OpenFileDialog1.FileName, vSecondarySurfaceDesc, vDevice)
vVideoFree = vDevice.HardwareCaps.VideoMemoryFree
MsgBox("图片已加载,剩余显存:" + vVideoFree.ToString)
此外需要注意的是,Bitmap的PixelFormat属性取到的只是一些枚举,因此我们自己写一个函数需要将其转化为Integer类型:
Private Function Convert(ByVal EnumType As Drawing.Imaging.PixelFormat) As Integer
Select Case EnumType
Case Imaging.PixelFormat.Format16bppArgb1555
Return 16DirectX与VB.NET编程(十五)*色深理论与图片智能加载
Case Imaging.PixelFormat.Format16bppGrayScale
Return 16
Case Imaging.PixelFormat.Format16bppRgb555
Return 16
Case Imaging.PixelFormat.Format1bppIndexed
Return 1
Case Imaging.PixelFormat.Format24bppRgb
Return 24
Case Imaging.PixelFormat.Format32bppArgb
Return 32
Case Imaging.PixelFormat.Format32bppPArgb
Return 32
Case Imaging.PixelFormat.Format32bppRgb
Return 32
Case Imaging.PixelFormat.Format48bppRgb
Return 48
Case Imaging.PixelFormat.Format4bppIndexed
Return 4
Case Imaging.PixelFormat.Format64bppArgb
Return 64
Case Imaging.PixelFormat.Format64bppPArgb
Return 64
Case Imaging.PixelFormat.Format8bppIndexed
Return 8
Case Else
Return 1DirectX与VB.NET编程(十五)*色深理论与图片智能加载
End Select
End Function