Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
UpdateImageSource(ImageEffect.Shadow)
End Sub
Private Sub CreateDeviceResources()
' create the Direct3D device
Dim actualLevel As D3D.FeatureLevel
Dim d3dContext As D3D.DeviceContext = Nothing
Dim d3dDevice = New D3D.Device(IntPtr.Zero)
Dim result = HResult.Ok
For i As Integer = 0 To 1
' use WARP if hardware is not available
Dim dt = If(i = 0, D3D.DriverType.Hardware, D3D.DriverType.Warp)
result = D3D.D3D11.CreateDevice(Nothing, dt, IntPtr.Zero,
D3D.DeviceCreationFlags.BgraSupport Or
D3D.DeviceCreationFlags.SingleThreaded,
Nothing, 0, D3D.D3D11.SdkVersion, d3dDevice,
actualLevel, d3dContext)
If result.Code <> &H887A0004 Then
' DXGI_ERROR_UNSUPPORTED
Exit For
End If
Next
result.CheckError()
d3dContext.Dispose()
' store the DXGI device (for trimming when the application is being suspended)
dxgiDevice = d3dDevice.QueryInterface(Of DXGI.Device)()
d3dDevice.Dispose()
' create a RenderTarget (DeviceContext for Direct2D drawing)
Dim d2dDevice = D2D.Device1.Create(d2dFactory, dxgiDevice)
Dim rt = D2D.DeviceContext1.Create(d2dDevice, D2D.DeviceContextOptions.None)
d2dDevice.Dispose()
rt.SetUnitMode(D2D.UnitMode.Pixels)
d2dContext = rt
' create built-in effects
shadow = D2D.Effects.Shadow.Create(rt)
affineTransform = D2D.Effects.AffineTransform2D.Create(rt)
composite = D2D.Effects.Composite.Create(rt)
End Sub
Private Sub DiscardDeviceResources()
shadow.Dispose()
affineTransform.Dispose()
composite.Dispose()
dxgiDevice.Dispose()
d2dContext.Dispose()
End Sub
Private Sub ClearGdiBitmap()
If lastGdiBitmap IsNot Nothing Then
PictureBox1.Image = Nothing
lastGdiBitmap.Dispose()
lastGdiBitmap = Nothing
End If
End Sub
Private Sub UpdateImageSource(imageEffect_1 As ImageEffect)
Dim targetOffset = New Point2F(marginLT, marginLT)
Dim w As Integer = bitmap.PixelWidth + marginLT + marginRB
Dim h As Integer = bitmap.PixelHeight + marginLT + marginRB
' the render target object
Dim rt = d2dContext
' create the target Direct2D bitmap
Dim bpTarget = New D2D.BitmapProperties1 _
(New D2D.PixelFormat(DXGI.Format.B8G8R8A8_UNorm, D2D.AlphaMode.Premultiplied),
CSng(bitmap.DpiX), CSng(bitmap.DpiY),
D2D.BitmapOptions.Target Or D2D.BitmapOptions.CannotDraw)
Dim targetBmp = D2D.Bitmap1.Create(rt, New Size2L(w, h), bpTarget)
' associate the target bitmap with render target
rt.SetTarget(targetBmp)
' start drawing
rt.BeginDraw()
' clear the target bitmap
rt.Clear(Nothing)
' convert C1Bitmap image to Direct2D image
Dim d2dBitmap = bitmap.ToD2DBitmap1(rt, D2D.BitmapOptions.None)
'apply the effect
Select Case imageEffect_1
Case ImageEffect.Original
rt.DrawImage(d2dBitmap, targetOffset)
Exit Select
Case ImageEffect.Shadow
rt.DrawImage(ApplyShadow(d2dBitmap), targetOffset)
Exit Select
End Select
d2dBitmap.Dispose()
' finish drawing (all drawing commands are executed at that moment)
rt.EndDraw()
' detach and actually dispose the target bitmap
rt.SetTarget(Nothing)
' create a temporary C1Bitmap object
Dim outBitmap = New C1Bitmap(bitmap.ImagingFactory)
' import the image from Direct2D target bitmap to C1Bitmap
outBitmap.Import(targetBmp, rt, New RectL(w, h))
targetBmp.Dispose()
' convert C1Bitmap to a System.Drawing.Bitmap
ClearGdiBitmap()
lastGdiBitmap = outBitmap.ToGdiBitmap()
outBitmap.Dispose()
' show the result in the PictureBox
PictureBox1.Image = lastGdiBitmap
End Sub
Private Function ApplyShadow(bitmap As D2D.Bitmap1) As D2D.Effect
shadow.SetInput(0, bitmap)
shadow.BlurStandardDeviation = 5.0F
affineTransform.SetInputEffect(0, shadow)
affineTransform.TransformMatrix = Matrix3x2.Translation(20.0F, 20.0F)
composite.SetInputEffect(0, affineTransform)
composite.SetInput(1, bitmap)
Return composite
End Function