void UpdateImageSource() { int w = _bitmap.PixelWidth; int h = _bitmap.PixelHeight; var rt = _d2dContext; var bpTarget = new D2D.BitmapProperties1( new D2D.PixelFormat(DXGI.Format.B8G8R8A8_UNorm, D2D.AlphaMode.Premultiplied), (float)_bitmap.DpiX, (float)_bitmap.DpiY, D2D.BitmapOptions.Target | D2D.BitmapOptions.CannotDraw); var targetBmp = D2D.Bitmap1.Create(rt, new Size2L(w, h), bpTarget); rt.SetTarget(targetBmp); rt.BeginDraw(); rt.Clear(null); var buffer = _bitmap.ToD2DBitmap1(rt, D2D.BitmapOptions.None); _warp.SetNormPositions(_warpStart, _warpEnd); _warp.Effect.SetInput(0, buffer); rt.DrawImage(_warp.Effect, Point2F.Empty); //_blur.SetInputEffect(0, _warp.Effect); //rt.DrawImage(_blur, Point2F.Empty); buffer.Dispose(); if (!rt.EndDraw(true)) { targetBmp.Dispose(); DiscardDeviceResources(); CreateDeviceResources(); return; } rt.SetTarget(null); _bitmap.ImportAsFragment(targetBmp, rt, new RectL(w, h), 0, 0); targetBmp.Dispose(); }
void UpdateImageSource(bool applyWarpEffect) { Point2L surfaceOffset; DXGI.Surface dxgiSurface; int w = _bitmap.PixelWidth; int h = _bitmap.PixelHeight; var hr = _sisNative.BeginDraw(new RectL(w, h), out surfaceOffset, out dxgiSurface); if (hr == DXGI.ResultCode.DeviceRemoved || hr == DXGI.ResultCode.DeviceReset) { DiscardDeviceResources(); CreateDeviceResources(); _sisNative.SetDevice(_dxgiDevice); return; } hr.CheckError(); var rt = _d2dContext; var bpTarget = new D2D.BitmapProperties1( new D2D.PixelFormat(DXGI.Format.B8G8R8A8_UNorm, D2D.AlphaMode.Premultiplied), (float)_bitmap.DpiX, (float)_bitmap.DpiY, D2D.BitmapOptions.Target | D2D.BitmapOptions.CannotDraw); var targetBmp = D2D.Bitmap1.Create(rt, dxgiSurface, bpTarget); dxgiSurface.Dispose(); rt.SetTarget(targetBmp); rt.BeginDraw(); rt.Clear(null); var buffer = _bitmap.ToD2DBitmap1(rt, D2D.BitmapOptions.None); if (!applyWarpEffect) { rt.DrawBitmap(buffer, new RectF(surfaceOffset.X, surfaceOffset.Y, w, h)); } else { _warp.SetNormPositions(_warpStart, _warpEnd); _warp.Effect.SetInput(0, buffer); rt.DrawImage(_warp.Effect, new Point2F(surfaceOffset.X, surfaceOffset.Y)); //_blur.SetInputEffect(0, _warp.Effect); //rt.DrawImage(_blur, new Point2F(surfaceOffset.X, surfaceOffset.Y)); } buffer.Dispose(); rt.EndDraw(); rt.SetTarget(null); if (applyWarpEffect) { var srcRect = new RectL(surfaceOffset.X, surfaceOffset.Y, w, h); _bitmap.ImportAsFragment(targetBmp, rt, srcRect, 0, 0); } targetBmp.Dispose(); _sisNative.EndDraw(); }
void Render(IntPtr hdc) { #region prepare device resources var rt = _d2dContext; if (rt == null) { CreateDeviceResources(); rt = _d2dContext; } #endregion #region draw to the intermediate bitmap (of the original bitmap size) if (_imBmp == null && _bitmap.HasImage) { var bmProps = new D2D.BitmapProperties1( new D2D.PixelFormat(DXGI.Format.B8G8R8A8_UNorm, D2D.AlphaMode.Premultiplied), (float)_bitmap.DpiX, (float)_bitmap.DpiY, D2D.BitmapOptions.Target); _imBmp = D2D.Bitmap1.Create(rt, new Size2L(_bitmap.PixelWidth, _bitmap.PixelHeight), bmProps); _needUpdateIM = true; } if (_needUpdateIM && _imBmp != null) { _needUpdateIM = false; rt.SetTarget(_imBmp); rt.BeginDraw(); rt.Clear(null); var buffer = _bitmap.ToD2DBitmap1(rt, D2D.BitmapOptions.None); if (!_applyEffect) { rt.DrawImage(buffer); } else { _warp.SetNormPositions(_warpEnd, _warpStart); _warp.Effect.SetInput(0, buffer); rt.DrawImage(_warp.Effect, Point2F.Empty); //_blur.SetInputEffect(0, _warp.Effect); //rt.DrawImage(_blur, Point2F.Empty); } buffer.Dispose(); if (!rt.EndDraw(true)) { DiscardDeviceResources(); return; } rt.SetTarget(null); if (_applyEffect) { _bitmap.ImportAsFragment(_imBmp, rt, new RectL(_bitmap.PixelWidth, _bitmap.PixelHeight), 0, 0); _applyEffect = false; } } #endregion #region draw the result to the target bitmap (of the target control size) if (_targetBmp == null) { var bmProps = new D2D.BitmapProperties1( new D2D.PixelFormat(DXGI.Format.B8G8R8A8_UNorm, D2D.AlphaMode.Ignore), _dpiX, _dpiY, D2D.BitmapOptions.Target | D2D.BitmapOptions.GdiCompatible); _targetBmp = D2D.Bitmap1.Create(rt, new Size2L(Width, Height), bmProps); } rt.SetTarget(_targetBmp); rt.BeginDraw(); rt.Clear(new ColorF(this.BackColor)); if (!_targetRect.IsEmpty) { if (_imBmp != null) { rt.DrawBitmap(_imBmp, _targetRect); } if (_drawLine) { var pt1 = new Point2F(_targetRect.Width * _warpStart.X + _targetRect.Left, _targetRect.Height * _warpStart.Y + _targetRect.Top); var pt2 = new Point2F(_targetRect.Width * _warpEnd.X + _targetRect.Left, _targetRect.Height * _warpEnd.Y + _targetRect.Top); rt.DrawLine(pt1, pt2, _lineBrush, _scaleFactor * 7, _lineStrokeStyle); } } #endregion #region BitBlt to GDI var gi = rt.QueryGdiInterop(); var gidc = gi.GetDC(D2D.DeviceContextInitializeMode.Copy); Win32.BitBlt(hdc, 0, 0, Width, Height, gidc, 0, 0, Win32.SRCCOPY); gi.ReleaseDC(null); gi.Dispose(); #endregion #region EndDraw if (!rt.EndDraw(true)) { DiscardDeviceResources(); return; } rt.SetTarget(null); #endregion }