コード例 #1
0
        void Page_Unloaded(object sender, RoutedEventArgs e)
        {
            if (_initialized)
            {
                _initialized = false;
                _dragHelper.FinalizeHelper();

                CleanImageSource();
                ClearSavedCopy();
                _bitmap.Dispose();
            }
        }
コード例 #2
0
 void CleanUp()
 {
     if (_initialized)
     {
         if (_window != null)
         {
             _window.Closing -= Window_Closing;
         }
         _initialized = false;
         _dragHelper.FinalizeHelper();
         image.Source = null;
         ClearSavedCopy();
         _bitmap.Dispose();
     }
 }
コード例 #3
0
 void ClearSavedCopy()
 {
     if (_savedCopy != null)
     {
         _savedCopy.Dispose();
         _savedCopy = null;
     }
 }
コード例 #4
0
        void CleanUp()
        {
            DiscardDeviceResources();

            _bitmap.Dispose();
            _d2dFactory.Dispose();
            _dwFactory.Dispose();
        }
コード例 #5
0
 /// <summary>
 /// Clean up any resources being used.
 /// </summary>
 /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
 protected override void Dispose(bool disposing)
 {
     if (disposing)
     {
         if (_initialized)
         {
             _initialized = false;
             _savedCopy.Dispose();
             _bitmap.Dispose();
         }
         if (components != null)
         {
             components.Dispose();
         }
     }
     base.Dispose(disposing);
 }
コード例 #6
0
        void MainPage_Unloaded(object sender, RoutedEventArgs e)
        {
            DiscardDeviceResources();

            _bitmap.Dispose();
            _d2dFactory.Dispose();
            _dwFactory.Dispose();

            image.Source = null;
            _sisNative.Dispose();
            _sisNative = null;
        }
コード例 #7
0
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                ClearGdiBitmap();
                DiscardDeviceResources();

                _bitmap.Dispose();
                _d2dFactory.Dispose();
                _dwFactory.Dispose();

                if (components != null)
                {
                    components.Dispose();
                    components = null;
                }
            }
            base.Dispose(disposing);
        }
コード例 #8
0
 /// <summary>
 /// Clean up any resources being used.
 /// </summary>
 /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
 protected override void Dispose(bool disposing)
 {
     if (disposing)
     {
         if (_initialized)
         {
             _initialized = false;
             ClearSavedCopy();
             _bitmap.Dispose();
             DiscardDeviceResources();
             DiscardDeviceIndependentResources();
         }
         if (components != null)
         {
             components.Dispose();
         }
     }
     base.Dispose(disposing);
 }
コード例 #9
0
        void Page_Unloaded(object sender, RoutedEventArgs e)
        {
            if (_initialized)
            {
                if (_app != null)
                {
                    _app.Suspending -= App_Suspending;
                }
                _initialized = false;

                _dragHelper.FinalizeHelper();

                DiscardDeviceResources();

                ClearSavedCopy();
                _bitmap.Dispose();

                DiscardDeviceIndependentResources();
            }
        }
コード例 #10
0
        void CleanUp()
        {
            if (_initialized)
            {
                if (_window != null)
                {
                    _window.Closing -= Window_Closing;
                }
                _initialized = false;

                image.Source = null;
                _dragHelper.FinalizeHelper();

                DiscardDeviceResources();

                ClearSavedCopy();
                _bitmap.Dispose();

                DiscardDeviceIndependentResources();
            }
        }
コード例 #11
0
        void ExportToGrayscale()
        {
            SaveFileDialog sfd = new SaveFileDialog();

            sfd.Filter          = "Jpeg Files (*.jpg)|*.jpg";
            sfd.CheckPathExists = true;

            // the user should pick the output file
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                // the render target object
                var rt = _d2dContext;

                // create the target Direct2D bitmap for the given DXGI.Surface
                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);
                Size2L bmpSize   = new Size2L(_bitmap.PixelWidth, _bitmap.PixelHeight);
                var    targetBmp = D2D.Bitmap1.Create(rt, bmpSize, bpTarget);

                // associate the target bitmap with render target
                rt.SetTarget(targetBmp);

                // start drawing
                rt.BeginDraw();

                // clear the target bitmap
                rt.Clear(null);

                // convert C1Bitmap image to Direct2D image
                var d2dBitmap = _bitmap.ToD2DBitmap1(rt, D2D.BitmapOptions.None);

                // create the Grayscale effect
                _colorMatrix.SetInput(0, d2dBitmap);
                _colorMatrix.Matrix = new Matrix5x4(
                    0.299f, 0.299f, 0.299f, 0,
                    0.587f, 0.587f, 0.587f, 0,
                    0.114f, 0.114f, 0.114f, 0,
                    0, 0, 0, 1,
                    0, 0, 0, 0
                    );

                // and draw the result
                rt.DrawImage(_colorMatrix, Point2F.Empty);
                d2dBitmap.Dispose();

                // now let's draw the text label with shadow
                rt.Transform = Matrix3x2.Rotation(-90f) * Matrix3x2.Translation(6f, 344f);
                _brush.SetColor(ColorF.White);
                rt.DrawTextLayout(new Point2F(-1f, -1f), _textLayout, _brush);
                _brush.SetColor(ColorF.DimGray);
                rt.DrawTextLayout(Point2F.Empty, _textLayout, _brush);
                rt.Transform = Matrix3x2.Identity;

                // finish drawing (all drawing commands are executed now)
                rt.EndDraw();

                // detach the target bitmap
                rt.SetTarget(null);

                // create a temporary C1Bitmap object
                var exportBitmap = new C1Bitmap(_bitmap.ImagingFactory);

                // import the image from Direct2D target bitmap to C1Bitmap
                var srcRect = new RectL(_bitmap.PixelWidth, _bitmap.PixelHeight);
                exportBitmap.Import(targetBmp, rt, srcRect);
                targetBmp.Dispose();

                // save the image to file
                exportBitmap.SaveAsJpeg(sfd.FileName, null);
                exportBitmap.Dispose();
            }
        }
コード例 #12
0
        void UpdateImageSource(ImageEffect imageEffect)
        {
            // some effects can change pixels outside the bounds of the source
            // image, so we need a margin to make those pixels visible
            var targetOffset = new Point2F(_marginLT, _marginLT);
            int w            = _bitmap.PixelWidth + _marginLT + _marginRB;
            int h            = _bitmap.PixelHeight + _marginLT + _marginRB;

            // the render target object
            var rt = _d2dContext;

            // create the target Direct2D bitmap
            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);

            // associate the target bitmap with render target
            rt.SetTarget(targetBmp);

            // start drawing
            rt.BeginDraw();

            // clear the target bitmap
            rt.Clear(null);

            // convert C1Bitmap image to Direct2D image
            var d2dBitmap = _bitmap.ToD2DBitmap1(rt, D2D.BitmapOptions.None);

            // apply the effect or just draw the original image
            switch (imageEffect)
            {
            case ImageEffect.Original:
                rt.DrawImage(d2dBitmap, targetOffset);
                break;

            case ImageEffect.GaussianBlur:
                rt.DrawImage(ApplyGaussianBlur(d2dBitmap), targetOffset);
                break;

            case ImageEffect.Sharpen:
                rt.DrawImage(ApplySharpen(d2dBitmap), targetOffset);
                break;

            case ImageEffect.HorizontalSmear:
                rt.DrawImage(ApplyHorizontalSmear(d2dBitmap), targetOffset);
                break;

            case ImageEffect.Shadow:
                rt.DrawImage(ApplyShadow(d2dBitmap), targetOffset);
                break;

            case ImageEffect.DisplacementMap:
                rt.DrawImage(ApplyDisplacementMap(d2dBitmap), targetOffset);
                break;

            case ImageEffect.Emboss:
                rt.DrawImage(ApplyEmboss(d2dBitmap), targetOffset);
                break;

            case ImageEffect.EdgeDetect:
                rt.DrawImage(ApplyEdgeDetect(d2dBitmap), targetOffset);
                break;

            case ImageEffect.Sepia:
                rt.DrawImage(ApplySepia(d2dBitmap), targetOffset);
                break;
            }
            d2dBitmap.Dispose();

            // draw the text label in case of the Shadow effect
            if (imageEffect == ImageEffect.Shadow)
            {
                var mr = Matrix3x2.Rotation(-90f);
                var mt = Matrix3x2.Translation(targetOffset.X + 6f, targetOffset.Y + 344f);
                rt.Transform = mr * mt;
                _brush.SetColor(ColorF.White);
                rt.DrawTextLayout(new Point2F(-1f, -1f), _textLayout, _brush);
                _brush.SetColor(ColorF.DimGray);
                rt.DrawTextLayout(Point2F.Empty, _textLayout, _brush);
                rt.Transform = Matrix3x2.Identity;
            }

            // finish drawing (all drawing commands are executed at that moment)
            if (!rt.EndDraw(true))
            {
                targetBmp.Dispose();

                // try to recreate the device resources if the old GPU device was removed
                DiscardDeviceResources();
                CreateDeviceResources();
                return;
            }

            // detach the target bitmap
            rt.SetTarget(null);

            // create a temporary C1Bitmap object
            var 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;
        }
コード例 #13
0
        async Task ExportGrayscale()
        {
            var picker = new FileSavePicker();

            picker.FileTypeChoices.Add("Jpeg files", new List <string> {
                ".jpg"
            });
            picker.DefaultFileExtension = ".jpg";

            // the user should pick the output file
            StorageFile file = await picker.PickSaveFileAsync();

            if (file != null)
            {
                // the render target object
                var rt = _d2dContext;

                // create the target Direct2D bitmap for the given DXGI.Surface
                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);
                Size2L bmpSize   = new Size2L(_bitmap.PixelWidth, _bitmap.PixelHeight);
                var    targetBmp = D2D.Bitmap1.Create(rt, bmpSize, bpTarget);

                // associate the target bitmap with render target
                rt.SetTarget(targetBmp);

                // start drawing
                rt.BeginDraw();

                // clear the target bitmap
                rt.Clear(null);

                // convert C1Bitmap image to Direct2D image
                var d2dBitmap = _bitmap.ToD2DBitmap1(rt, D2D.BitmapOptions.None);

                // create the Grayscale effect
                _colorMatrix.SetInput(0, d2dBitmap);
                _colorMatrix.Matrix = new Matrix5x4(
                    0.299f, 0.299f, 0.299f, 0,
                    0.587f, 0.587f, 0.587f, 0,
                    0.114f, 0.114f, 0.114f, 0,
                    0, 0, 0, 1,
                    0, 0, 0, 0
                    );

                // and draw the result
                rt.DrawImage(_colorMatrix, Point2F.Empty);
                d2dBitmap.Dispose();

                // now let's draw the additional text label with shadow
                rt.Transform = Matrix3x2.Rotation(-90f) * Matrix3x2.Translation(6f, 344f);
                _brush.SetColor(ColorF.White);
                rt.DrawTextLayout(new Point2F(-1f, -1f), _textLayout, _brush);
                _brush.SetColor(ColorF.DimGray);
                rt.DrawTextLayout(Point2F.Empty, _textLayout, _brush);
                rt.Transform = Matrix3x2.Identity;

                // finish drawing (all drawing commands are executed now)
                if (!rt.EndDraw(true))
                {
                    // clean up and return if the GPU device was removed and we can't draw
                    targetBmp.Dispose();
                    return;
                }

                // detach the target bitmap
                rt.SetTarget(null);

                // create a temporary C1Bitmap object
                var exportBitmap = new C1Bitmap(_bitmap.ImagingFactory);

                // import the image from Direct2D target bitmap to C1Bitmap
                var srcRect = new RectL(_bitmap.PixelWidth, _bitmap.PixelHeight);
                exportBitmap.Import(targetBmp, rt, srcRect);
                targetBmp.Dispose();

                // save the image to file
                await exportBitmap.SaveAsJpegAsync(file, null);

                exportBitmap.Dispose();
            }
        }