这就是我如何将 System.Drawing.Bitmap 转换为 SharpDX.Direct2d1.Bitmap。
Dim bmpData As BitmapData = m_Bitmap.LockBits(New System.Drawing.Rectangle(0, 0, m_Bitmap.Width, m_Bitmap.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, m_Bitmap.PixelFormat)
Dim bgraArray As Byte() = New Byte(bmpData.Width * bmpData.Height * 4 - 1) {}
Dim scan As IntPtr = bmpData.Scan0
Marshal.Copy(scan, bgraArray, 0, (bmpData.Width * bmpData.Height) * 4)
Dim rgbaarray As Byte() = New Byte(bmpData.Width * bmpData.Height * 4 - 1) {}
Parallel.For(0, 4, Sub(range)
For i As Integer = range * bmpData.Height / 4 To bmpData.Height / 4 * (range + 1) - 1
Dim offset As Integer = i * bmpData.Width * 4
For j As Integer = 0 To bmpData.Width * 4 - 1 Step 4
rgbaarray(offset + j) = bgraArray(offset + j + 2)
rgbaarray(offset + j + 1) = bgraArray(offset + j + 1)
rgbaarray(offset + j + 2) = bgraArray(offset + j)
rgbaarray(offset + j + 3) = bgraArray(offset + j + 3)
Next
Next
End Sub)
Dim stream As DataStream = New DataStream(bmpData.Height * bmpData.Width * 4, True, True)
stream.WriteRange(rgbaarray)
stream.Position = 0
m_sharpDXbitmap = New SharpDX.Direct2D1.Bitmap(deviceContext, New Size2(m_Bitmap.Width, m_Bitmap.Height), stream, bmpData.Width * 4, bitmapProperties)
m_Bitmap.UnlockBits(bmpData)
stream.Dispose()
但是,这不是 10 毫秒,而是 120 毫秒。我为保持我想要的抽象所做的是创建一个包含两个位图的 CustomBitmap 类,并且在第一次绘制调用时,如果尚未转换,则只转换一次,依此类推未来的抽奖电话,这将是即时的。这是我的 CustomBitmap 类:
Imports System.Drawing.Imaging
Imports System.Reflection
Imports SharpDX
Imports SharpDX.Direct2D1
Imports System.Runtime.InteropServices
Imports Synergx.Common.Drawing
Imports System.Collections.Concurrent
Imports System.Threading.Tasks
Public Class CustomBitmap
Private bitmapProperties As BitmapProperties = New BitmapProperties(New SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.R8G8B8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Ignore))
Public Property Bitmap As System.Drawing.Bitmap
Get
Return m_Bitmap
End Get
Set(value As System.Drawing.Bitmap)
m_Bitmap = value
End Set
End Property
Private m_Width As Integer
Private m_Height As Integer
Private m_Bitmap As System.Drawing.Bitmap
Private m_sharpDXbitmap As SharpDX.Direct2D1.Bitmap = Nothing
Public ReadOnly Property Width As Integer
Get
Return m_Width
End Get
End Property
Public ReadOnly Property Height As Integer
Get
Return m_Height
End Get
End Property
Public ReadOnly Property GPUBitmap As SharpDX.Direct2D1.Bitmap
Get
Return m_sharpDXbitmap
End Get
End Property
Public Sub New(bitmap As System.Drawing.Bitmap)
Me.Bitmap = bitmap
m_Width = bitmap.Width
m_Height = bitmap.Height
End Sub
Friend Sub GenerateSharpDXBitmap(deviceContext As SharpDX.Direct2D1.DeviceContext)
If (m_Bitmap.PixelFormat = Imaging.PixelFormat.Format8bppIndexed) Then
GenerateSharpDXBitmap8bpp(deviceContext)
ElseIf (m_Bitmap.PixelFormat = Imaging.PixelFormat.Format32bppArgb) Then
GenerateSharpDXBitmap32argp(deviceContext)
End If
End Sub
Private Sub GenerateSharpDXBitmap8bpp(deviceContext As SharpDX.Direct2D1.DeviceContext)
Dim bmpData As BitmapData = m_Bitmap.LockBits(New System.Drawing.Rectangle(0, 0, m_Bitmap.Width, m_Bitmap.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, m_Bitmap.PixelFormat)
Dim integerArray As Integer()
ReDim integerArray(bmpData.Height * bmpData.Width - 1)
Parallel.For(0, 4, Sub(range)
Dim scan As IntPtr = bmpData.Scan0
For i As Integer = 0 To bmpData.Height * range / 4 - 1 - 1
scan += bmpData.Stride
Next
For y As Integer = bmpData.Height * range / 4 To bmpData.Height * (range + 1) / 4 - 1
Dim bytes As Byte() = New Byte(bmpData.Width - 1) {}
Marshal.Copy(scan, bytes, 0, bmpData.Width)
For x As Integer = 0 To bytes.Length - 1
Dim B As Integer = bytes(x)
Dim rgba As Integer = B Or (B << 8) Or (B << 16) Or (B << 24)
integerArray(x + y * bmpData.Width) = rgba
Next
scan += bmpData.Stride
Next
End Sub)
Dim stream As DataStream = New DataStream(bmpData.Height * bmpData.Width * 4, True, True)
stream.WriteRange(integerArray)
stream.Position = 0
m_sharpDXbitmap = New SharpDX.Direct2D1.Bitmap(deviceContext, New Size2(m_Bitmap.Width, m_Bitmap.Height), stream, bmpData.Width * 4, bitmapProperties)
m_Bitmap.UnlockBits(bmpData)
stream.Dispose()
End Sub
Private Sub GenerateSharpDXBitmap32argp(deviceContext As SharpDX.Direct2D1.DeviceContext)
Dim bmpData As BitmapData = m_Bitmap.LockBits(New System.Drawing.Rectangle(0, 0, m_Bitmap.Width, m_Bitmap.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, m_Bitmap.PixelFormat)
Dim bgraArray As Byte() = New Byte(bmpData.Width * bmpData.Height * 4 - 1) {}
Dim scan As IntPtr = bmpData.Scan0
Marshal.Copy(scan, bgraArray, 0, (bmpData.Width * bmpData.Height) * 4)
Dim rgbaarray As Byte() = New Byte(bmpData.Width * bmpData.Height * 4 - 1) {}
Parallel.For(0, 4, Sub(range)
For i As Integer = range * bmpData.Height / 4 To bmpData.Height / 4 * (range + 1) - 1
Dim offset As Integer = i * bmpData.Width * 4
For j As Integer = 0 To bmpData.Width * 4 - 1 Step 4
rgbaarray(offset + j) = bgraArray(offset + j + 2)
rgbaarray(offset + j + 1) = bgraArray(offset + j + 1)
rgbaarray(offset + j + 2) = bgraArray(offset + j)
rgbaarray(offset + j + 3) = bgraArray(offset + j + 3)
Next
Next
End Sub)
Dim stream As DataStream = New DataStream(bmpData.Height * bmpData.Width * 4, True, True)
stream.WriteRange(rgbaarray)
stream.Position = 0
m_sharpDXbitmap = New SharpDX.Direct2D1.Bitmap(deviceContext, New Size2(m_Bitmap.Width, m_Bitmap.Height), stream, bmpData.Width * 4, bitmapProperties)
m_Bitmap.UnlockBits(bmpData)
stream.Dispose()
End Sub
Public Sub Dispose()
If Bitmap IsNot Nothing Then
m_Bitmap.Dispose()
m_Bitmap = Nothing
End If
If m_sharpDXbitmap IsNot Nothing Then
m_sharpDXbitmap.Dispose()
m_sharpDXbitmap = Nothing
End If
End Sub
End Class
该类包含 2 种不同的方法,可以根据图像的原始 PixelFormat 分离为灰度颜色或 32bpp。