示例#1
0
 protected override void DrawOntoOutputTarget(WicRenderTarget target)
 {
     // draw all the images into the output image
     foreach (var image in ItemArray)
     {
         var location = ImagePlacement[image.Key];
         var img      = image.Value;
         using (var bmp = global::SharpDX.Direct2D1.Bitmap.FromWicBitmap(target, img))
         {
             target.DrawBitmap(bmp,
                               new RawRectangleF(location.Left, location.Top, location.Right, location.Bottom),
                               1, global::SharpDX.Direct2D1.BitmapInterpolationMode.Linear);
         }
     }
 }
        public async static Task <MemoryStream> InterpolateImageMarkup(StorageFile imageFile, StorageFile markupFile)
        {
            try
            {
                var bitmap = new BitmapImage();
                using (var strm = await imageFile.OpenAsync(FileAccessMode.Read))
                {
                    bitmap.SetSource(strm);
                }
                var width       = bitmap.PixelWidth;
                var height      = bitmap.PixelHeight;
                var pixelFormat = WicPixelFormat.Format32bppBGR;

                var             wicFactory    = new ImagingFactory2();
                var             markupFactory = new ImagingFactory2();
                var             dddFactory    = new SharpDX.Direct2D1.Factory();
                var             dwFactory     = new SharpDX.DirectWrite.Factory();
                WicRenderTarget renderTarget;
                Bitmap          wicBitmap;


                SharpDX.Direct3D11.Device defaultDevice = new SharpDX.Direct3D11.Device(DriverType.Hardware, DeviceCreationFlags.Debug | DeviceCreationFlags.BgraSupport);


                var device = defaultDevice.QueryInterface <SharpDX.Direct3D11.Device1>();
                SharpDX.DXGI.Device2     dxgiDevice2 = device.QueryInterface <SharpDX.DXGI.Device2>();
                SharpDX.Direct2D1.Device d2dDevice   = new SharpDX.Direct2D1.Device(dxgiDevice2);
                // d2dContext = new SharpDX.Direct2D1.DeviceContext(d2dDevice, SharpDX.Direct2D1.DeviceContextOptions.None);


                using (var bitmapSource = LoadBitmap(wicFactory, imageFile.Path))
                {
                    wicBitmap = new Bitmap(wicFactory, bitmapSource, BitmapCreateCacheOption.CacheOnLoad);

                    int pixelWidth  = (int)(wicBitmap.Size.Width * DisplayProperties.LogicalDpi / 96.0);
                    int pixelHeight = (int)(wicBitmap.Size.Height * DisplayProperties.LogicalDpi / 96.0);

                    var renderTargetProperties = new RenderTargetProperties(RenderTargetType.Default,
                                                                            new D2DPixelFormat(Format.Unknown, AlphaMode.Unknown), 0, 0, RenderTargetUsage.None,
                                                                            SharpDX.Direct2D1.FeatureLevel.Level_DEFAULT);

                    renderTarget = new WicRenderTarget(dddFactory, wicBitmap, renderTargetProperties)
                    {
                        TextAntialiasMode = SharpDX.Direct2D1.TextAntialiasMode.Cleartype
                    };
                }

                renderTarget.BeginDraw();


                renderTarget.DrawBitmap(LoadBitmapFromContentFile(wicFactory, markupFile.Path, renderTarget), 1.0f, SharpDX.Direct2D1.BitmapInterpolationMode.NearestNeighbor, new SharpDX.RectangleF(30, 50, 100, 80));



                //new RectangleF(width - 150, 0, width, height + 25),

                renderTarget.EndDraw();

                var ms = new MemoryStream();

                var stream = new WICStream(wicFactory, ms);

                BitmapEncoder encoder = null;
                if (imageFile.FileType == ".png")
                {
                    encoder = new PngBitmapEncoder(wicFactory);
                }
                else if (imageFile.FileType == ".jpg")
                {
                    encoder = new JpegBitmapEncoder(wicFactory);
                }

                encoder.Initialize(stream);

                var frameEncoder = new BitmapFrameEncode(encoder);
                frameEncoder.Initialize();
                frameEncoder.SetSize(width, height);
                //frameEncoder.piPixelFormat = WicPixelFormat.FormatDontCare;
                frameEncoder.WriteSource(wicBitmap);
                frameEncoder.Commit();

                encoder.Commit();

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

                ms.Position = 0;
                return(ms);
            }
            catch (Exception)
            {
                throw;
            }
        }