Пример #1
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();
            }
        }