Beispiel #1
0
        private MemoryStream GetBitmapAsStream(WIC.Bitmap wicBitmap)
        {
            int width  = wicBitmap.Size.Width;
            int height = wicBitmap.Size.Height;
            var ms     = new MemoryStream();

            using (var stream = new WIC.WICStream(
                       this.WicFactory,
                       ms))
            {
                using (var encoder = new WIC.PngBitmapEncoder(WicFactory))
                {
                    encoder.Initialize(stream);

                    using (var frameEncoder = new WIC.BitmapFrameEncode(encoder))
                    {
                        frameEncoder.Initialize();

                        frameEncoder.SetSize(width, height);
                        var format = WIC.PixelFormat.Format32bppBGRA;
                        frameEncoder.SetPixelFormat(ref format);
                        frameEncoder.WriteSource(wicBitmap);
                        frameEncoder.Commit();
                    }

                    encoder.Commit();
                }
            }

            ms.Position = 0;
            return(ms);
        }
Beispiel #2
0
 public void Save(Stream stream, ImageFormat format)
 {
     using (var encoder = new s.WIC.BitmapEncoder(
                SDFactory.WicImagingFactory,
                format.ToWic()))
     {
         encoder.Initialize(stream);
         using (var frameEncoder = new s.WIC.BitmapFrameEncode(encoder))
         {
             frameEncoder.WriteSource(Control);
             frameEncoder.Commit();
         }
         encoder.Commit();
     }
 }
Beispiel #3
0
        private Stream DumpAndSaveImage()
        {
            var stream            = new MemoryStream();
            var textureSDRCpuCopy = new D3D11.Texture2D(d3dDevice, new D3D11.Texture2DDescription
            {
                Width             = description.CanvasRect.Width,
                Height            = description.CanvasRect.Height,
                MipLevels         = 1,
                ArraySize         = 1,
                Format            = DXGI.Format.B8G8R8A8_UNorm_SRgb,
                Usage             = D3D11.ResourceUsage.Staging,
                SampleDescription = new DXGI.SampleDescription(1, 0),
                BindFlags         = D3D11.BindFlags.None,
                CpuAccessFlags    = D3D11.CpuAccessFlags.Read,
                OptionFlags       = D3D11.ResourceOptionFlags.None,
            });

            DataStream rawSdrImageDataStream;

            d3dContext.CopyResource(textureSDRImage, textureSDRCpuCopy);

            var dataBox       = d3dContext.MapSubresource(textureSDRCpuCopy, 0, 0, D3D11.MapMode.Read, D3D11.MapFlags.None, out rawSdrImageDataStream);
            var dataRectangle = new DataRectangle
            {
                DataPointer = rawSdrImageDataStream.DataPointer,
                Pitch       = dataBox.RowPitch,
            };

            using (var bitmap = new WIC.Bitmap(wicFactory, description.CanvasRect.Width, description.CanvasRect.Height, WIC.PixelFormat.Format32bppBGRA, dataRectangle))
                using (var imageEncoder = new WIC.BmpBitmapEncoder(wicFactory, stream))
                    using (var encodeInstance = new WIC.BitmapFrameEncode(imageEncoder))
                    {
                        encodeInstance.Initialize();
                        encodeInstance.SetSize(bitmap.Size.Width, bitmap.Size.Height);
                        var pixelFormat = WIC.PixelFormat.Format24bppBGR;
                        encodeInstance.SetPixelFormat(ref pixelFormat);
                        encodeInstance.WriteSource(bitmap);
                        encodeInstance.Commit();
                        imageEncoder.Commit();
                        stream.Flush();
                    }

            d3dContext.UnmapSubresource(textureSDRCpuCopy, 0);
            rawSdrImageDataStream.Dispose();
            textureSDRCpuCopy.Dispose();

            return(stream);
        }
        public void Save(WIC.Bitmap wicBitmap, Stream stream)
        {
            using (var encoder = new WIC.BitmapEncoder(WicFactory, WIC.ContainerFormatGuids.Jpeg, stream))
                using (var frame = new WIC.BitmapFrameEncode(encoder))
                {
                    frame.Initialize();
                    var pfx = WIC.PixelFormat.Format128bppRGBAFloat; //Format64bppRGBA; //FormatDontCare;


                    frame.SetResolution(600, 600);
                    frame.SetPixelFormat(ref pfx);
                    frame.WriteSource(wicBitmap);
                    frame.Commit();
                    encoder.Commit();
                }
        }
        public static void EncodeImage(this d2.Bitmap target, ImageFormat imageFormat, Stream outputStream)
        {
            var width  = target.PixelSize.Width;
            var height = target.PixelSize.Height;

            var wicBitmap = new wic.Bitmap(DXGraphicsService.FactoryImaging, width, height,
                                           wic.PixelFormat.Format32bppBGR, wic.BitmapCreateCacheOption.CacheOnLoad);
            var renderTargetProperties = new d2.RenderTargetProperties(d2.RenderTargetType.Default,
                                                                       new d2.PixelFormat(Format.Unknown,
                                                                                          d2.AlphaMode.Unknown), 0, 0,
                                                                       d2.RenderTargetUsage.None,
                                                                       d2.FeatureLevel.Level_DEFAULT);
            var d2DRenderTarget = new d2.WicRenderTarget(target.Factory, wicBitmap, renderTargetProperties);

            d2DRenderTarget.BeginDraw();
            d2DRenderTarget.Clear(global::SharpDX.Color.Transparent);
            d2DRenderTarget.DrawBitmap(target, 1, d2.BitmapInterpolationMode.Linear);
            d2DRenderTarget.EndDraw();

            var stream = new wic.WICStream(DXGraphicsService.FactoryImaging, outputStream);

            // Initialize a Jpeg encoder with this stream
            var encoder = new wic.BitmapEncoder(DXGraphicsService.FactoryImaging, GetImageFormat(imageFormat));

            encoder.Initialize(stream);

            // Create a Frame encoder
            var bitmapFrameEncode = new wic.BitmapFrameEncode(encoder);

            bitmapFrameEncode.Initialize();
            bitmapFrameEncode.SetSize(width, height);
            Guid pixelFormatGuid = wic.PixelFormat.FormatDontCare;

            bitmapFrameEncode.SetPixelFormat(ref pixelFormatGuid);
            bitmapFrameEncode.WriteSource(wicBitmap);

            bitmapFrameEncode.Commit();
            encoder.Commit();

            bitmapFrameEncode.Dispose();
            encoder.Dispose();
            stream.Dispose();

            d2DRenderTarget.Dispose();
            wicBitmap.Dispose();
        }
        public void SaveAsPng(System.IO.Stream stream)
        {
            using (var encoder = new WIC.PngBitmapEncoder(factories.WICFactory, stream)) {
                using (var bitmapFrameEncode = new WIC.BitmapFrameEncode(encoder)) {
                    bitmapFrameEncode.Initialize();
                    var size = bmp.Size;
                    bitmapFrameEncode.SetSize(size.Width, size.Height);
                    var pf = bmp.PixelFormat;
                    bitmapFrameEncode.SetPixelFormat(ref pf);

                    bitmapFrameEncode.WriteSource(bmp);

                    bitmapFrameEncode.Commit();
                    encoder.Commit();
                }
            }
        }
Beispiel #7
0
        private static void SaveD2DBitmap(WIC.ImagingFactory2 wic, WIC.Bitmap wicBitmap, Stream outputStream)
        {
            using (var encoder = new WIC.BitmapEncoder(wic, WIC.ContainerFormatGuids.Png))
            {
                encoder.Initialize(outputStream);
                using (var frame = new WIC.BitmapFrameEncode(encoder))
                {
                    frame.Initialize();
                    frame.SetSize(wicBitmap.Size.Width, wicBitmap.Size.Height);

                    var pixelFormat = wicBitmap.PixelFormat;
                    frame.SetPixelFormat(ref pixelFormat);
                    frame.WriteSource(wicBitmap);

                    frame.Commit();
                    encoder.Commit();
                }
            }
        }
Beispiel #8
0
        public void SaveAsPng(System.IO.Stream stream)
        {
            using (var encoder = new WIC.PngBitmapEncoder (factories.WICFactory, stream)) {
                using (var bitmapFrameEncode = new WIC.BitmapFrameEncode (encoder)) {
                    bitmapFrameEncode.Initialize ();
                    var size = bmp.Size;
                    bitmapFrameEncode.SetSize (size.Width, size.Height);
                    var pf = bmp.PixelFormat;
                    bitmapFrameEncode.SetPixelFormat (ref pf);

                    bitmapFrameEncode.WriteSource (bmp);

                    bitmapFrameEncode.Commit ();
                    encoder.Commit ();
                }
            }
        }
Beispiel #9
0
        public MemoryStream RenderToPngStream(FrameworkElement fe)
        {
            var width  = (int)Math.Ceiling(fe.ActualWidth);
            var height = (int)Math.Ceiling(fe.ActualHeight);

            // pixel format with transparency/alpha channel and RGB values premultiplied by alpha
            var pixelFormat = WIC.PixelFormat.Format32bppPRGBA;

            // pixel format without transparency, but one that works with Cleartype antialiasing
            //var pixelFormat = WicPixelFormat.Format32bppBGR;

            var wicBitmap = new WIC.Bitmap(
                this.WicFactory,
                width,
                height,
                pixelFormat,
                WIC.BitmapCreateCacheOption.CacheOnLoad);

            var renderTargetProperties = new D2D.RenderTargetProperties(
                D2D.RenderTargetType.Default,
                new D2D.PixelFormat(Format.R8G8B8A8_UNorm, D2D.AlphaMode.Premultiplied),
                //new D2DPixelFormat(Format.Unknown, AlphaMode.Unknown), // use this for non-alpha, cleartype antialiased text
                0,
                0,
                D2D.RenderTargetUsage.None,
                D2D.FeatureLevel.Level_DEFAULT);
            var renderTarget = new D2D.WicRenderTarget(
                this.D2DFactory,
                wicBitmap,
                renderTargetProperties)
            {
                //TextAntialiasMode = TextAntialiasMode.Cleartype // this only works with the pixel format with no alpha channel
                TextAntialiasMode = D2D.TextAntialiasMode.Grayscale // this is the best we can do for bitmaps with alpha channels
            };

            Compose(renderTarget, fe);
            // TODO: There is no need to encode the bitmap to PNG - we could just copy the texture pixel buffer to a WriteableBitmap pixel buffer.
            var ms = new MemoryStream();

            var stream = new WIC.WICStream(
                this.WicFactory,
                ms);

            var encoder = new WIC.PngBitmapEncoder(WicFactory);

            encoder.Initialize(stream);

            var frameEncoder = new WIC.BitmapFrameEncode(encoder);

            frameEncoder.Initialize();
            frameEncoder.SetSize(width, height);
            var format = WIC.PixelFormat.Format32bppBGRA;

            //var format = WicPixelFormat.FormatDontCare;
            frameEncoder.SetPixelFormat(ref format);
            frameEncoder.WriteSource(wicBitmap);
            frameEncoder.Commit();

            encoder.Commit();

            frameEncoder.Dispose();
            encoder.Dispose();
            stream.Dispose();

            ms.Position = 0;
            return(ms);
        }
        private MemoryStream GetBitmapAsStream(WIC.Bitmap wicBitmap)
        {
            int width = wicBitmap.Size.Width;
            int height = wicBitmap.Size.Height;
            var ms = new MemoryStream();

            using (var stream = new WIC.WICStream(
                this.WicFactory,
                ms))
            {
                using (var encoder = new WIC.PngBitmapEncoder(WicFactory))
                {
                    encoder.Initialize(stream);

                    using (var frameEncoder = new WIC.BitmapFrameEncode(encoder))
                    {
                        frameEncoder.Initialize();

                        frameEncoder.SetSize(width, height);
                        var format = WIC.PixelFormat.Format32bppBGRA;
                        frameEncoder.SetPixelFormat(ref format);
                        frameEncoder.WriteSource(wicBitmap);
                        frameEncoder.Commit();
                    }

                    encoder.Commit();
                }
            }

            ms.Position = 0;
            return ms;
        }
        public MemoryStream RenderToPngStream(FrameworkElement fe)
        {
            var width = (int)Math.Ceiling(fe.ActualWidth);
            var height = (int)Math.Ceiling(fe.ActualHeight);

            // pixel format with transparency/alpha channel and RGB values premultiplied by alpha
            var pixelFormat = WIC.PixelFormat.Format32bppPRGBA;

            // pixel format without transparency, but one that works with Cleartype antialiasing
            //var pixelFormat = WicPixelFormat.Format32bppBGR;

            var wicBitmap = new WIC.Bitmap(
                this.WicFactory,
                width,
                height,
                pixelFormat,
                WIC.BitmapCreateCacheOption.CacheOnLoad);

            var renderTargetProperties = new D2D.RenderTargetProperties(
                D2D.RenderTargetType.Default,
                new D2D.PixelFormat(Format.R8G8B8A8_UNorm, D2D.AlphaMode.Premultiplied),
                //new D2DPixelFormat(Format.Unknown, AlphaMode.Unknown), // use this for non-alpha, cleartype antialiased text
                0,
                0,
                D2D.RenderTargetUsage.None,
                D2D.FeatureLevel.Level_DEFAULT);
            var renderTarget = new D2D.WicRenderTarget(
                this.D2DFactory,
                wicBitmap,
                renderTargetProperties)
            {
                //TextAntialiasMode = TextAntialiasMode.Cleartype // this only works with the pixel format with no alpha channel
                TextAntialiasMode = D2D.TextAntialiasMode.Grayscale // this is the best we can do for bitmaps with alpha channels
            };

            Compose(renderTarget, fe);
            // TODO: There is no need to encode the bitmap to PNG - we could just copy the texture pixel buffer to a WriteableBitmap pixel buffer.
            var ms = new MemoryStream();

            var stream = new WIC.WICStream(
                this.WicFactory,
                ms);

            var encoder = new WIC.PngBitmapEncoder(WicFactory);
            encoder.Initialize(stream);

            var frameEncoder = new WIC.BitmapFrameEncode(encoder);
            frameEncoder.Initialize();
            frameEncoder.SetSize(width, height);
            var format = WIC.PixelFormat.Format32bppBGRA;
            //var format = WicPixelFormat.FormatDontCare;
            frameEncoder.SetPixelFormat(ref format);
            frameEncoder.WriteSource(wicBitmap);
            frameEncoder.Commit();

            encoder.Commit();

            frameEncoder.Dispose();
            encoder.Dispose();
            stream.Dispose();

            ms.Position = 0;
            return ms;
        }
Beispiel #12
0
        private Result SaveWICTextureToFileFix(
            DeviceContext context,
            Texture2D source,
            ref Guid guidContainerFormat,
            string fileName)
        {
            if (fileName == null)
            {
                return(Result.InvalidArg);
            }

            Result res = CaptureTextureFix(context, source, out Texture2DDescription desc, out Texture2D staging);

            if (res.Failure)
            {
                return(res);
            }

            Guid pfGuid;
            //bool sRGB = false;
            Guid targetGuid;

            switch (desc.Format)
            {
            case DXGI.Format.R32G32B32A32_Float: pfGuid = WIC.PixelFormat.Format128bppRGBAFloat; break;

            case DXGI.Format.R16G16B16A16_Float: pfGuid = WIC.PixelFormat.Format64bppRGBAHalf; break;

            case DXGI.Format.R16G16B16A16_UNorm: pfGuid = WIC.PixelFormat.Format64bppRGBA; break;

            case DXGI.Format.R10G10B10_Xr_Bias_A2_UNorm: pfGuid = WIC.PixelFormat.Format32bppRGBA1010102XR; break;     // DXGI 1.1

            case DXGI.Format.R10G10B10A2_UNorm: pfGuid = WIC.PixelFormat.Format32bppRGBA1010102; break;

            case DXGI.Format.B5G5R5A1_UNorm: pfGuid = WIC.PixelFormat.Format16bppBGRA5551; break;

            case DXGI.Format.B5G6R5_UNorm: pfGuid = WIC.PixelFormat.Format16bppBGR565; break;

            case DXGI.Format.R32_Float: pfGuid = WIC.PixelFormat.Format32bppGrayFloat; break;

            case DXGI.Format.R16_Float: pfGuid = WIC.PixelFormat.Format16bppGrayHalf; break;

            case DXGI.Format.R16_UNorm: pfGuid = WIC.PixelFormat.Format16bppGray; break;

            case DXGI.Format.R8_UNorm: pfGuid = WIC.PixelFormat.Format8bppGray; break;

            case DXGI.Format.A8_UNorm: pfGuid = WIC.PixelFormat.Format8bppAlpha; break;

            case DXGI.Format.R8G8B8A8_UNorm:
                pfGuid = WIC.PixelFormat.Format32bppRGBA;
                break;

            case DXGI.Format.R8G8B8A8_UNorm_SRgb:
                pfGuid = WIC.PixelFormat.Format32bppRGBA;
                //sRGB = true;
                break;

            case DXGI.Format.B8G8R8A8_UNorm:     // DXGI 1.1
                pfGuid = WIC.PixelFormat.Format32bppBGRA;
                break;

            case DXGI.Format.B8G8R8A8_UNorm_SRgb:     // DXGI 1.1
                pfGuid = WIC.PixelFormat.Format32bppBGRA;
                //sRGB = true;
                break;

            case DXGI.Format.B8G8R8X8_UNorm:     // DXGI 1.1
                pfGuid = WIC.PixelFormat.Format32bppBGR;
                break;

            case DXGI.Format.B8G8R8X8_UNorm_SRgb:     // DXGI 1.1
                pfGuid = WIC.PixelFormat.Format32bppBGR;
                //sRGB = true;
                break;

            default:
                return(Result.GetResultFromWin32Error(unchecked ((int)0x80070032)));
            }

            // Create file
            FileStream fs = new FileStream(fileName, FileMode.Create);

            WIC.BitmapEncoder encoder = new WIC.BitmapEncoder(DirectX.ImageFactory, guidContainerFormat);
            encoder.Initialize(fs);


            WIC.BitmapFrameEncode frameEncode = new WIC.BitmapFrameEncode(encoder);
            frameEncode.Initialize();
            frameEncode.SetSize(desc.Width, desc.Height);
            frameEncode.SetResolution(72.0, 72.0);


            switch (desc.Format)
            {
            case DXGI.Format.R32G32B32A32_Float:
            case DXGI.Format.R16G16B16A16_Float:
                targetGuid = WIC.PixelFormat.Format24bppBGR;
                break;

            case DXGI.Format.R16G16B16A16_UNorm: targetGuid = WIC.PixelFormat.Format48bppBGR; break;

            case DXGI.Format.B5G5R5A1_UNorm: targetGuid = WIC.PixelFormat.Format16bppBGR555; break;

            case DXGI.Format.B5G6R5_UNorm: targetGuid = WIC.PixelFormat.Format16bppBGR565; break;

            case DXGI.Format.R32_Float:
            case DXGI.Format.R16_Float:
            case DXGI.Format.R16_UNorm:
            case DXGI.Format.R8_UNorm:
            case DXGI.Format.A8_UNorm:
                targetGuid = WIC.PixelFormat.Format8bppGray;
                break;

            default:
                targetGuid = WIC.PixelFormat.Format24bppBGR;
                break;
            }

            frameEncode.SetPixelFormat(ref targetGuid);

            #region Write

            DataBox db = context.MapSubresource(staging, 0, MapMode.Read, MapFlags.None, out DataStream stream);

            if (pfGuid != targetGuid)
            {
                WIC.FormatConverter formatCoverter = new WIC.FormatConverter(DirectX.ImageFactory);

                if (formatCoverter.CanConvert(pfGuid, targetGuid))
                {
                    WIC.Bitmap src = new WIC.Bitmap(DirectX.ImageFactory, desc.Width, desc.Height, pfGuid,
                                                    new DataRectangle(db.DataPointer, db.RowPitch));

                    formatCoverter.Initialize(src, targetGuid, SharpDX.WIC.BitmapDitherType.None, null, 0, SharpDX.WIC.BitmapPaletteType.Custom);

                    frameEncode.WriteSource(formatCoverter, new Rectangle(0, 0, desc.Width, desc.Height));
                }
            }
            else
            {
                frameEncode.WritePixels(desc.Height, new DataRectangle(db.DataPointer, db.RowPitch));
            }

            context.UnmapSubresource(staging, 0);

            frameEncode.Commit();
            encoder.Commit();

            #endregion

            frameEncode.Dispose();
            encoder.Dispose();

            fs.Close();

            return(Result.Ok);
        }
Beispiel #13
0
        public static byte[] CreatePngImage(int width, int height, string text,
                                            float fontSize         = 30.0f,
                                            string font            = "Times New Roman",
                                            int lineCount          = 5,
                                            bool rotation          = false,
                                            float turbulenceAmount = 60.0f)
        {
            using (var wic = new WIC.ImagingFactory2())
                using (var d2d = new D2D.Factory())
                    using (var wicBitmap = new WIC.Bitmap(wic, width, height, WIC.PixelFormat.Format32bppPBGRA, WIC.BitmapCreateCacheOption.CacheOnDemand))
                        using (var target = new D2D.WicRenderTarget(d2d, wicBitmap, new D2D.RenderTargetProperties()))
                            using (var dwriteFactory = new DWrite.Factory())
                                using (var brush = new D2D.SolidColorBrush(target, Color.Yellow))
                                    using (var encoder = new WIC.PngBitmapEncoder(wic))

                                        using (var ms = new MemoryStream())
                                            using (var dc = target.QueryInterface <D2D.DeviceContext>())
                                                using (var bmpLayer = new D2D.Bitmap1(dc, target.PixelSize,
                                                                                      new D2D.BitmapProperties1(new D2D.PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, D2D.AlphaMode.Premultiplied),
                                                                                                                d2d.DesktopDpi.Width, d2d.DesktopDpi.Height,
                                                                                                                D2D.BitmapOptions.Target)))
                                                {
                                                    var r = new Random();
                                                    encoder.Initialize(ms);

                                                    D2D.Image oldTarget = dc.Target;

                                                    {
                                                        dc.Target = bmpLayer;
                                                        dc.BeginDraw();
                                                        var textFormat = new DWrite.TextFormat(dwriteFactory, font, fontSize);
                                                        for (int charIndex = 0; charIndex < text.Length; ++charIndex)
                                                        {
                                                            using (var layout = new DWrite.TextLayout(dwriteFactory, text[charIndex].ToString(), textFormat, float.MaxValue, float.MaxValue))
                                                            {
                                                                var layoutSize = new Vector2(layout.Metrics.Width, layout.Metrics.Height);
                                                                using (var b2 = new D2D.LinearGradientBrush(dc, new D2D.LinearGradientBrushProperties
                                                                {
                                                                    StartPoint = Vector2.Zero,
                                                                    EndPoint = layoutSize,
                                                                }, new D2D.GradientStopCollection(dc, new[]
                                                                {
                                                                    new D2D.GradientStop {
                                                                        Position = 0.0f, Color = ColorFromHsl(r.NextFloat(0, 1), 1.0f, 0.8f)
                                                                    },
                                                                    new D2D.GradientStop {
                                                                        Position = 1.0f, Color = ColorFromHsl(r.NextFloat(0, 1), 1.0f, 0.8f)
                                                                    },
                                                                })))
                                                                {
                                                                    var position = new Vector2(charIndex * width / text.Length, r.NextFloat(0, height - layout.Metrics.Height));
                                                                    dc.Transform =
                                                                        Matrix3x2.Translation(-layoutSize / 2) *
                                                                        Matrix3x2.Skew(r.NextFloat(0, 0.5f), r.NextFloat(0, 0.5f)) *
                                                                        (rotation ? Matrix3x2.Rotation(r.NextFloat(0, (float)(Math.PI * 2))) : Matrix3x2.Identity) *
                                                                        Matrix3x2.Translation(position + layoutSize / 2);
                                                                    dc.DrawTextLayout(Vector2.Zero, layout, b2);
                                                                }
                                                            }
                                                        }
                                                        for (var i = 0; i < lineCount; ++i)
                                                        {
                                                            target.Transform = Matrix3x2.Identity;
                                                            brush.Color      = ColorFromHsl(r.NextFloat(0, 1), 1.0f, 0.3f);
                                                            target.DrawLine(
                                                                r.NextVector2(Vector2.Zero, new Vector2(width, height)),
                                                                r.NextVector2(Vector2.Zero, new Vector2(width, height)),
                                                                brush, 3.0f);
                                                        }
                                                        target.EndDraw();
                                                    }

                                                    Color background = ColorFromHsl(r.NextFloat(0, 1), 1.0f, 0.3f);
                                                    {
                                                        dc.Target = null;
                                                        using (var displacement = new D2D.Effects.DisplacementMap(dc))
                                                        {
                                                            displacement.SetInput(0, bmpLayer, true);
                                                            displacement.Scale = turbulenceAmount;

                                                            var turbulence = new D2D.Effects.Turbulence(dc);
                                                            displacement.SetInputEffect(1, turbulence);

                                                            dc.Target = oldTarget;
                                                            dc.BeginDraw();
                                                            dc.Clear(background);
                                                            dc.DrawImage(displacement);
                                                            dc.EndDraw();

                                                            using (var frame = new WIC.BitmapFrameEncode(encoder))
                                                            {
                                                                frame.Initialize();
                                                                frame.SetSize(wicBitmap.Size.Width, wicBitmap.Size.Height);

                                                                var pixelFormat = wicBitmap.PixelFormat;
                                                                frame.SetPixelFormat(ref pixelFormat);
                                                                frame.WriteSource(wicBitmap);

                                                                frame.Commit();
                                                            }
                                                        }
                                                    }

                                                    encoder.Commit();
                                                    return(ms.ToArray());
                                                }
        }