Esempio n. 1
0
 void Restart(object sender, RoutedEventArgs e)
 {
     _bitmap.ImportAsFragment(_savedCopy, 0, 0);
     image.Source = _bitmap.ToWriteableBitmap();
 }
Esempio n. 2
0
 void Restart(object sender, RoutedEventArgs e)
 {
     _bitmap.ImportAsFragment(_savedCopy, 0, 0);
     UpdateImageSource(false);
 }
        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
        }