示例#1
0
 public void Dispose()
 {
     Direct2DResourceManager.OnResourceDestroy(this);
     if (Texture2D != null)
     {
         Texture2D.Dispose();
         Texture2D = null;
     }
     if (fConverter != null)
     {
         fConverter.Dispose();
         fConverter = null;
     }
     if (BFDecorde != null)
     {
         BFDecorde.Dispose();
         BFDecorde = null;
     }
     if (BitDecorder != null)
     {
         BitDecorder.Dispose();
         BitDecorder = null;
     }
     if (MemStream != null)
     {
         MemStream.Dispose();
         MemStream = null;
     }
 }
示例#2
0
        private SharpDX.WIC.FormatConverter DecodeImage()
        {
            var path = Windows.ApplicationModel.Package.Current.InstalledLocation.Path;

            SharpDX.WIC.BitmapDecoder bitmapDecoder = new SharpDX.WIC.BitmapDecoder(
                _deviceManager.WICFactory,
                @"Assets\Nepal.jpg",
                SharpDX.IO.NativeFileAccess.Read,
                SharpDX.WIC.DecodeOptions.CacheOnDemand
                );

            SharpDX.WIC.BitmapFrameDecode bitmapFrameDecode = bitmapDecoder.GetFrame(0);

            SharpDX.WIC.BitmapSource bitmapSource = new SharpDX.WIC.BitmapSource(bitmapFrameDecode.NativePointer);

            SharpDX.WIC.FormatConverter formatConverter = new SharpDX.WIC.FormatConverter(_deviceManager.WICFactory);
            //formatConverter.Initialize( bitmapSource, SharpDX.WIC.PixelFormat.Format32bppBGRA);
            formatConverter.Initialize(
                bitmapSource,
                SharpDX.WIC.PixelFormat.Format32bppBGRA,
                SharpDX.WIC.BitmapDitherType.None,
                null,
                0.0f,
                SharpDX.WIC.BitmapPaletteType.Custom
                );

            imageSize = formatConverter.Size;

            return(formatConverter);
        }
示例#3
0
        /// <summary>
        /// This doesnt work! SharpDX errors when trying to open a local system file (not relative)
        /// </summary>
        /// <param name="assetNativeUri"></param>
        /// <param name="backgroundImageFormatConverter"></param>
        /// <param name="backgroundImageSize"></param>
        public void LoadNativeAsset(string assetNativeUri, out SharpDX.WIC.FormatConverter backgroundImageFormatConverter, out Size2 backgroundImageSize)
        {
            var path = Windows.ApplicationModel.Package.Current.InstalledLocation.Path;


            var nativeFileStream = new SharpDX.IO.NativeFileStream(
                assetNativeUri,
                SharpDX.IO.NativeFileMode.Open,
                SharpDX.IO.NativeFileAccess.Read,
                SharpDX.IO.NativeFileShare.Read);


            var r = SharpDX.IO.NativeFile.ReadAllBytes(assetNativeUri);

            var data = SharpDX.IO.NativeFile.ReadAllBytes(assetNativeUri);

            using (System.IO.MemoryStream ms = new System.IO.MemoryStream(data))
            {
                if (ms != null)
                {
                    using (SharpDX.WIC.BitmapDecoder bitmapDecoder = new SharpDX.WIC.BitmapDecoder(
                               _deviceManager.WICFactory,
                               ms,
                               SharpDX.WIC.DecodeOptions.CacheOnDemand
                               ))
                    {
                        using (SharpDX.WIC.BitmapFrameDecode bitmapFrameDecode = bitmapDecoder.GetFrame(0))
                        {
                            using (SharpDX.WIC.BitmapSource bitmapSource = new SharpDX.WIC.BitmapSource(bitmapFrameDecode.NativePointer))
                            {
                                SharpDX.WIC.FormatConverter formatConverter = new SharpDX.WIC.FormatConverter(_deviceManager.WICFactory);
                                //formatConverter.Initialize( bitmapSource, SharpDX.WIC.PixelFormat.Format32bppBGRA);
                                formatConverter.Initialize(
                                    bitmapSource,
                                    SharpDX.WIC.PixelFormat.Format32bppBGRA,
                                    SharpDX.WIC.BitmapDitherType.None,
                                    null,
                                    0.0f,
                                    SharpDX.WIC.BitmapPaletteType.Custom
                                    );

                                backgroundImageSize = formatConverter.Size;

                                backgroundImageFormatConverter = formatConverter;
                            }
                        }
                    }
                }
            }

            backgroundImageFormatConverter = null;
            backgroundImageSize            = new Size2(0, 0);
        }
示例#4
0
 private void CreateFromBitmap(SharpDX.Direct2D1.RenderTarget InRenderTarget, Bitmap InBitmap)
 {
     MemStream = new System.IO.MemoryStream();
     InBitmap.Save(MemStream, System.Drawing.Imaging.ImageFormat.Png);
     BitDecorder = new SharpDX.WIC.BitmapDecoder(Direct2DDrawingSystem.instance.ImagingFactory,
                                                 MemStream,
                                                 SharpDX.WIC.DecodeOptions.CacheOnDemand);
     BFDecorde  = BitDecorder.GetFrame(0);
     fConverter = new SharpDX.WIC.FormatConverter(Direct2DDrawingSystem.instance.ImagingFactory);
     fConverter.Initialize(BFDecorde, SharpDX.WIC.PixelFormat.Format32bppPBGRA, SharpDX.WIC.BitmapDitherType.None, null, 0, SharpDX.WIC.BitmapPaletteType.Custom);
     Texture2D = SharpDX.Direct2D1.Bitmap.FromWicBitmap(InRenderTarget, fConverter);
 }
示例#5
0
        public override void OnRenderTargetChanged()
        {
            base.OnRenderTargetChanged();
            if (RenderTarget == null || RenderTarget.IsDisposed)
            {
                return;
            }

            // Dispose all Render dependant resources on RenderTarget change.
            if (myBitmap != null)
            {
                myBitmap.Dispose();
            }
            if (fileStream != null)
            {
                fileStream.Dispose();
            }
            if (bitmapDecoder != null)
            {
                bitmapDecoder.Dispose();
            }
            if (converter != null)
            {
                converter.Dispose();
            }
            if (frame != null)
            {
                frame.Dispose();
            }

            // Neccessary for creating WIC objects.
            fileStream = new SharpDX.IO.NativeFileStream(System.IO.Path.Combine(NinjaTrader.Core.Globals.UserDataDir, "SampleDrawBitmap.png"), SharpDX.IO.NativeFileMode.Open, SharpDX.IO.NativeFileAccess.Read);
            // Used to read the image source file.
            bitmapDecoder = new SharpDX.WIC.BitmapDecoder(Core.Globals.WicImagingFactory, fileStream, SharpDX.WIC.DecodeOptions.CacheOnDemand);
            // Get the first frame of the image.
            frame = bitmapDecoder.GetFrame(0);
            // Convert it to a compatible pixel format.
            converter = new SharpDX.WIC.FormatConverter(Core.Globals.WicImagingFactory);
            converter.Initialize(frame, SharpDX.WIC.PixelFormat.Format32bppPRGBA);
            // Create the new Bitmap1 directly from the FormatConverter.
            myBitmap = SharpDX.Direct2D1.Bitmap.FromWicBitmap(RenderTarget, converter);

            if (!HWSet && myBitmap != null)
            {
                H     = myBitmap.Size.Height;
                W     = myBitmap.Size.Width;
                Ratio = W / H;
                HWSet = true;
            }
        }
示例#6
0
        public static SharpDX.Direct2D1.Bitmap LoadFromFile(string filePath)
        {
            SharpDX.WIC.ImagingFactory  imagingFactory = new SharpDX.WIC.ImagingFactory();
            SharpDX.IO.NativeFileStream fileStream     = new SharpDX.IO.NativeFileStream(filePath,
                                                                                         SharpDX.IO.NativeFileMode.Open, SharpDX.IO.NativeFileAccess.Read);

            SharpDX.WIC.BitmapDecoder bitmapDecoder =
                new SharpDX.WIC.BitmapDecoder(imagingFactory, fileStream, SharpDX.WIC.DecodeOptions.CacheOnDemand);
            SharpDX.WIC.BitmapFrameDecode frame = bitmapDecoder.GetFrame(0);

            SharpDX.WIC.FormatConverter converter = new SharpDX.WIC.FormatConverter(imagingFactory);
            converter.Initialize(frame, SharpDX.WIC.PixelFormat.Format32bppPRGBA);

            return(SharpDX.Direct2D1.Bitmap.FromWicBitmap(RenderForm.RenderTarget, converter));
        }
示例#7
0
 Bitmap GetBitmap(RenderTarget target, Stream stream)
 {
     using (SharpDX.WIC.ImagingFactory wicFactory = new SharpDX.WIC.ImagingFactory())
     {
         using (SharpDX.WIC.BitmapDecoder bmpDecoder = new SharpDX.WIC.BitmapDecoder(wicFactory, stream, SharpDX.WIC.DecodeOptions.CacheOnLoad))
         {
             using (SharpDX.WIC.FormatConverter converter = new SharpDX.WIC.FormatConverter(wicFactory))
             {
                 using (SharpDX.WIC.BitmapFrameDecode frameDecode = bmpDecoder.GetFrame(0))
                 {
                     converter.Initialize(frameDecode, SharpDX.WIC.PixelFormat.Format32bppBGR);
                     return(SharpDX.Direct2D1.Bitmap.FromWicBitmap(target, converter));
                 }
             }
         }
     }
 }
示例#8
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="assetUri"></param>
        /// <param name="backgroundImageFormatConverter"></param>
        /// <param name="backgroundImageSize"></param>
        public void LoadAsset(string assetUri, out SharpDX.WIC.FormatConverter backgroundImageFormatConverter, out Size2 backgroundImageSize)
        {
            SharpDX.WIC.ImagingFactory2 wicFactory = null;
            if (_deviceManager == null)
            {
                wicFactory = new SharpDX.WIC.ImagingFactory2();
            }
            else if (_deviceManager.WICFactory == null)
            {
                wicFactory = new SharpDX.WIC.ImagingFactory2();
            }
            else
            {
                wicFactory = _deviceManager.WICFactory;
            }

            var path = Windows.ApplicationModel.Package.Current.InstalledLocation.Path;

            SharpDX.WIC.BitmapDecoder bitmapDecoder = new SharpDX.WIC.BitmapDecoder(
                wicFactory,
                assetUri,
                SharpDX.IO.NativeFileAccess.Read,
                SharpDX.WIC.DecodeOptions.CacheOnDemand
                );

            SharpDX.WIC.BitmapFrameDecode bitmapFrameDecode = bitmapDecoder.GetFrame(0);

            SharpDX.WIC.BitmapSource bitmapSource = new SharpDX.WIC.BitmapSource(bitmapFrameDecode.NativePointer);

            SharpDX.WIC.FormatConverter formatConverter = new SharpDX.WIC.FormatConverter(wicFactory);
            //formatConverter.Initialize( bitmapSource, SharpDX.WIC.PixelFormat.Format32bppBGRA);
            formatConverter.Initialize(
                bitmapSource,
                SharpDX.WIC.PixelFormat.Format32bppBGRA,
                SharpDX.WIC.BitmapDitherType.None,
                null,
                0.0f,
                SharpDX.WIC.BitmapPaletteType.Custom
                );

            backgroundImageSize = formatConverter.Size;

            backgroundImageFormatConverter = formatConverter;
        }
示例#9
0
        private SharpDX.WIC.FormatConverter DecodeImage()
        {
            var path = Windows.ApplicationModel.Package.Current.InstalledLocation.Path;

            SharpDX.WIC.BitmapDecoder bitmapDecoder = new SharpDX.WIC.BitmapDecoder(
                _deviceManager.WICFactory,
                @"Assets\SydneyOperaHouse002.png",
                SharpDX.IO.NativeFileAccess.Read,
                SharpDX.WIC.DecodeOptions.CacheOnDemand
                );

            SharpDX.WIC.BitmapFrameDecode bitmapFrameDecode = bitmapDecoder.GetFrame(0);

            SharpDX.WIC.BitmapSource bitmapSource = new SharpDX.WIC.BitmapSource(bitmapFrameDecode.NativePointer);

            SharpDX.WIC.FormatConverter formatConverter = new SharpDX.WIC.FormatConverter(_deviceManager.WICFactory);
            formatConverter.Initialize(bitmapSource, SharpDX.WIC.PixelFormat.Format32bppBGRA);

            return(formatConverter);
        }
示例#10
0
 private SharpDX.Direct2D1.Bitmap CreateFromBitmap(SharpDX.Direct2D1.RenderTarget InRenderTarget, Bitmap InBitmap)
 {
     using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
     {
         InBitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
         using (SharpDX.WIC.BitmapDecoder bitDecorder =
                    new SharpDX.WIC.BitmapDecoder(Direct2DDrawingSystem.instance.ImagingFactory,
                                                  ms,
                                                  SharpDX.WIC.DecodeOptions.CacheOnDemand)
                )
         {
             using (SharpDX.WIC.BitmapFrameDecode bfDecode = bitDecorder.GetFrame(0))
             {
                 using (SharpDX.WIC.FormatConverter fConverter = new SharpDX.WIC.FormatConverter(Direct2DDrawingSystem.instance.ImagingFactory))
                 {
                     fConverter.Initialize(bfDecode, SharpDX.WIC.PixelFormat.Format32bppPBGRA, SharpDX.WIC.BitmapDitherType.None, null, 0, SharpDX.WIC.BitmapPaletteType.Custom);
                     return(SharpDX.Direct2D1.Bitmap.FromWicBitmap(InRenderTarget, fConverter));
                 }
             }
         }
     }
 }
示例#11
0
        ///// <summary>
        /////
        ///// </summary>
        ///// <param name="assetUri"></param>
        ///// <param name="backgroundImageFormatConverter"></param>
        ///// <param name="backgroundImageSize"></param>
        //public void LoadAsset(
        //    SharpDX.WIC.ImagingFactory2 wicFactory,
        //    string assetUri,
        //    out SharpDX.WIC.FormatConverter backgroundImageFormatConverter,
        //    out DrawingSize backgroundImageSize)
        //{

        //    //var path = Windows.ApplicationModel.Package.Current.InstalledLocation.Path;

        //    SharpDX.WIC.BitmapDecoder bitmapDecoder = new SharpDX.WIC.BitmapDecoder(
        //                                                                                wicFactory,
        //                                                                                assetUri,
        //                                                                                SharpDX.IO.NativeFileAccess.Read,
        //                                                                                SharpDX.WIC.DecodeOptions.CacheOnDemand
        //                                                                            );

        //    SharpDX.WIC.BitmapFrameDecode bitmapFrameDecode = bitmapDecoder.GetFrame(0);

        //    SharpDX.WIC.BitmapSource bitmapSource = new SharpDX.WIC.BitmapSource(bitmapFrameDecode.NativePointer);

        //    SharpDX.WIC.FormatConverter formatConverter = new SharpDX.WIC.FormatConverter(wicFactory);
        //    //formatConverter.Initialize( bitmapSource, SharpDX.WIC.PixelFormat.Format32bppBGRA);
        //    formatConverter.Initialize(
        //        bitmapSource,
        //        SharpDX.WIC.PixelFormat.Format32bppBGRA,
        //        SharpDX.WIC.BitmapDitherType.None,
        //        null,
        //        0.0f,
        //        SharpDX.WIC.BitmapPaletteType.Custom
        //        );

        //    backgroundImageSize = formatConverter.Size;

        //    backgroundImageFormatConverter = formatConverter;
        //}


        /// <summary>
        /// Loads bitmap asynchronously and injects into global variables. I need to work out how to NOT make them global
        /// </summary>
        /// <param name="assetNativeUri"></param>
        /// <returns></returns>
        public async Task <Tuple <SharpDX.WIC.FormatConverter, Size2, Stream> > LoadAssetAsync(
            SharpDX.WIC.ImagingFactory2 wicFactory,
            string assetNativeUri,
            string cacheId,
            string path = ""
            )
        {
            if (_listOfAssets.ContainsKey(cacheId))
            {
                return(_listOfAssets[cacheId]);
            }


            SharpDX.WIC.FormatConverter _backgroundImageFormatConverter = null;
            Size2 _backgroundImageSize = new Size2(0, 0);

            Windows.Storage.StorageFile storageFile = null;

            if (path == string.Empty)
            {
                path        = Windows.ApplicationModel.Package.Current.InstalledLocation.Path;
                storageFile = await Windows.Storage.StorageFile.GetFileFromPathAsync(path + assetNativeUri);
            }
            else if (path == "PicturesLibrary")
            {
                var assetNativeUriParts = assetNativeUri.Split("\\".ToCharArray());

                var foundFolder = await Windows.Storage.KnownFolders.PicturesLibrary.GetFolderAsync(assetNativeUriParts[0]);

                storageFile = await foundFolder.GetFileAsync(assetNativeUriParts[1]);
            }
            else if (path == "PublicPicturesLibrary")
            {
                var assetNativeUriParts = assetNativeUri.Split("\\".ToCharArray());

                var foundFolder = await Windows.Storage.KnownFolders.PicturesLibrary.GetFolderAsync(assetNativeUriParts[0]);

                storageFile = await foundFolder.GetFileAsync(assetNativeUriParts[1]);
            }

            if (storageFile == null)
            {
                return(null);
            }

            Stream ms = await storageFile.OpenStreamForReadAsync();  //ras.GetResults().AsStreamForRead())

            //var data = SharpDX.IO.NativeFile.ReadAllBytes(assetNativeUri);
            //using (System.IO.MemoryStream ms = new System.IO.MemoryStream(data))
            {
                if (ms != null)
                {
                    SharpDX.WIC.BitmapDecoder bitmapDecoder = new SharpDX.WIC.BitmapDecoder(
                        wicFactory,
                        ms,
                        SharpDX.WIC.DecodeOptions.CacheOnDemand
                        );
                    {
                        using (SharpDX.WIC.BitmapFrameDecode bitmapFrameDecode = bitmapDecoder.GetFrame(0))
                        {
                            using (SharpDX.WIC.BitmapSource bitmapSource = new SharpDX.WIC.BitmapSource(bitmapFrameDecode.NativePointer))
                            {
                                SharpDX.WIC.FormatConverter formatConverter = new SharpDX.WIC.FormatConverter(wicFactory);
                                //formatConverter.Initialize( bitmapSource, SharpDX.WIC.PixelFormat.Format32bppBGRA);
                                formatConverter.Initialize(
                                    bitmapSource,
                                    SharpDX.WIC.PixelFormat.Format32bppBGRA,
                                    SharpDX.WIC.BitmapDitherType.None,
                                    null,
                                    0.0f,
                                    SharpDX.WIC.BitmapPaletteType.Custom
                                    );

                                _backgroundImageSize            = formatConverter.Size;
                                _backgroundImageFormatConverter = formatConverter;


                                //return Tuple.Create<SharpDX.WIC.FormatConverter, Size2>(_backgroundImageFormatConverter, _backgroundImageSize);
                            }
                        }
                    }
                }
            }

            //ras.Close();


            var ret = Tuple.Create <SharpDX.WIC.FormatConverter, Size2, Stream>(_backgroundImageFormatConverter, _backgroundImageSize, ms);

            _listOfAssets.Add(cacheId, ret);

            return(ret);
        }