D2D.Effect ApplyGaussianBlur(D2D.Bitmap1 bitmap) { _gaussianBlur.SetInput(0, bitmap); _gaussianBlur.BorderMode = D2D.BorderMode.Soft; _gaussianBlur.StandardDeviation = 3f; return(_gaussianBlur); }
D2D.Effect ApplyDisplacementMap(D2D.Bitmap1 bitmap) { _displacementMap.SetInput(0, bitmap); _turbulence.Stitchable = true; _tile.SetInputEffect(0, _turbulence); _tile.Rectangle = new Vector4(0, 0, 512, 512); _displacementMap.SetInputEffect(1, _tile); _displacementMap.Scale = 100f; return(_displacementMap); }
D2D.Effect ApplyShadow(D2D.Bitmap1 bitmap) { _shadow.SetInput(0, bitmap); _shadow.BlurStandardDeviation = 5f; _affineTransform.SetInputEffect(0, _shadow); _affineTransform.TransformMatrix = Matrix3x2.Translation(20f, 20f); _composite.SetInputEffect(0, _affineTransform); _composite.SetInput(1, bitmap); return(_composite); }
D2D.Effect ApplySepia(D2D.Bitmap1 bitmap) { _colorMatrix.SetInput(0, bitmap); _colorMatrix.Matrix = new Matrix5x4( 0.393f, 0.349f, 0.272f, 0, 0.769f, 0.686f, 0.534f, 0, 0.189f, 0.168f, 0.131f, 0, 0, 0, 0, 1, 0, 0, 0, 0 ); return(_colorMatrix); }
D2D.Effect ApplyHorizontalSmear(D2D.Bitmap1 bitmap) { _convolveMatrix.SetInput(0, bitmap); _convolveMatrix.KernelSizeX = 20; _convolveMatrix.KernelSizeY = 1; _convolveMatrix.KernelMatrix = new float[] { 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f }; _convolveMatrix.Divisor = 20f; return(_convolveMatrix); }
D2D.Effect ApplyEmboss(D2D.Bitmap1 bitmap) { _convolveMatrix.SetInput(0, bitmap); _convolveMatrix.KernelSizeX = 3; _convolveMatrix.KernelSizeY = 3; _convolveMatrix.KernelMatrix = new float[] { 2.0f, 1.0f, 0.0f, 1.0f, 1.0f, -1.0f, 0.0f, -1.0f, -2.0f }; _convolveMatrix.Divisor = 1f; return(_convolveMatrix); }
D2D.Effect ApplySharpen(D2D.Bitmap1 bitmap) { _convolveMatrix.SetInput(0, bitmap); _convolveMatrix.KernelSizeX = 3; _convolveMatrix.KernelSizeY = 3; _convolveMatrix.KernelMatrix = new float[] { 0.0f, -1.0f, 0.0f, -1.0f, 6.0f, -1.0f, 0.0f, -1.0f, 0.0f }; _convolveMatrix.Divisor = 2f; return(_convolveMatrix); }
D2D.Effect ApplyEdgeDetect(D2D.Bitmap1 bitmap) { _flood.Color = ColorF.Black; _crop.SetInputEffect(0, _flood); _crop.Rectangle = new Vector4(0, 0, _bitmap.PixelWidth, _bitmap.PixelHeight); _convolveMatrix.SetInput(0, bitmap); _convolveMatrix.KernelSizeX = 3; _convolveMatrix.KernelSizeY = 3; _convolveMatrix.KernelMatrix = new float[] { 0.0f, -1.0f, 0.0f, -1.0f, 4.0f, -1.0f, 0.0f, -1.0f, 0.0f }; _convolveMatrix.Divisor = 1f; _composite.SetInputEffect(0, _crop); _composite.SetInputEffect(1, _convolveMatrix); return(_composite); }
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 }