Exemplo n.º 1
0
        private static Bitmap LoadBitmapFromMemory(RenderTarget device, byte[] bytes)
        {
            if (device == null)
            {
                throw new ArgumentNullException(nameof(device));
            }
            if (bytes == null)
            {
                throw new ArgumentNullException(nameof(bytes));
            }
            if (bytes.Length == 0)
            {
                throw new ArgumentOutOfRangeException(nameof(bytes));
            }

            try
            {
                Bitmap bmp = null;

                var stream  = new MemoryStream(bytes);
                var decoder = new BitmapDecoder(ImageFactory, stream, DecodeOptions.CacheOnDemand);
                var frame   = decoder.GetFrame(0);

                var converter = new FormatConverter(ImageFactory);

                try
                {
                    converter.Initialize(frame, PixelFormat.Format32bppPRGBA);
                    bmp = Bitmap.FromWicBitmap(device, converter);
                }
                catch
                {
                    TryCatch(() => converter.Dispose());

                    converter = new FormatConverter(ImageFactory);
                    converter.Initialize(frame, PixelFormat.Format32bppRGB);
                    bmp = Bitmap.FromWicBitmap(device, converter);
                }

                converter.Dispose();
                frame.Dispose();
                decoder.Dispose();
                stream.Dispose();

                return(bmp);
            }
            catch
            {
                throw;
            }
        }
Exemplo n.º 2
0
        public static Bitmap Decode(RenderTarget device, BitmapDecoder decoder)
        {
            var frame     = decoder.GetFrame(0);
            var converter = new FormatConverter(Image.ImageFactory);

            foreach (var format in _pixelFormatEnumerator)
            {
                try
                {
                    converter.Initialize(frame, format);

                    var bmp = Bitmap.FromWicBitmap(device, converter);

                    TryCatch(() => converter.Dispose());
                    TryCatch(() => frame.Dispose());

                    return(bmp);
                }
                catch
                {
                    TryCatch(() => converter.Dispose());
                    converter = new FormatConverter(Image.ImageFactory);
                }
            }

            TryCatch(() => converter.Dispose());
            TryCatch(() => frame.Dispose());

            throw new Exception("Unsupported Image Format!");
        }
Exemplo n.º 3
0
        public void Reset(D2DDeviceContext d2dContext)
        {
            blackBrush?.Dispose();
            whiteBrush?.Dispose();
            bmToolBarBox?.Dispose();
            bmToolBarSelect?.Dispose();
            // textFormat?.Dispose();

            d2dDeviceContext = d2dContext;

            blackBrush = new SolidColorBrush(d2dContext, Color.Black);
            whiteBrush = new SolidColorBrush(d2dContext, Color.White);

            bmToolBarBox = D2DBitmap.FromWicBitmap(
                d2dContext,
                bsToolBarBox,
                new BitmapProperties(
                    new PixelFormat(SharpDX.DXGI.Format.R8G8B8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied)
                    ));
            bmToolBarSelect = D2DBitmap.FromWicBitmap(
                d2dContext,
                bsToolBarSelect,
                new BitmapProperties(
                    new PixelFormat(SharpDX.DXGI.Format.R8G8B8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied)
                    ));
        }
 /// <summary>
 /// 从给定的流中加载 Direct2D 位图。
 /// </summary>
 /// <param name="stream">要加载位图的流。</param>
 /// <returns>得到的 Direct2D 位图。</returns>
 public Bitmap LoadBitmapFromStream(Stream stream)
 {
     using (BitmapDecoder decoder = new BitmapDecoder(wicFactory, stream, DecodeOptions.CacheOnLoad))
     {
         using (FormatConverter formatConverter = new FormatConverter(wicFactory))
         {
             formatConverter.Initialize(decoder.GetFrame(0), WICPixelFormat);
             return(Bitmap.FromWicBitmap(this.renderTarget, formatConverter));
         }
     }
 }
Exemplo n.º 5
0
        public IBitmapImage LoadBitmap(string FileName)
        {
            using var decoder      = new BitmapDecoder(_editorSession.ImagingFactory, FileName, 0);
            using var bmpSource    = decoder.GetFrame(0);
            using var convertedBmp = new FormatConverter(_editorSession.ImagingFactory);
            convertedBmp.Initialize(bmpSource, PixelFormat.Format32bppPBGRA);

            var bmp = Bitmap.FromWicBitmap(_editorSession.RenderTarget, convertedBmp);

            return(new Direct2DImage(bmp));
        }
Exemplo n.º 6
0
        public IDisposable LoadBitmap(string FileName, out Size Size)
        {
            using (var decoder = new BitmapDecoder(_editorSession.ImagingFactory, FileName, 0))
                using (var bmpSource = decoder.GetFrame(0))
                    using (var convertedBmp = new FormatConverter(_editorSession.ImagingFactory))
                    {
                        convertedBmp.Initialize(bmpSource, PixelFormat.Format32bppPBGRA);

                        Size = new Size(bmpSource.Size.Width, bmpSource.Size.Height);

                        return(Bitmap.FromWicBitmap(_editorSession.RenderTarget, convertedBmp));
                    }
        }
Exemplo n.º 7
0
 protected override void LoadFromStream(string textureKey, Stream stream)
 {
     using (var bitmapDecoder = new BitmapDecoder(this.imagingFactory, stream, DecodeOptions.CacheOnDemand))
     {
         var frame = bitmapDecoder.GetFrame(0);
         using (var converter = new FormatConverter(this.imagingFactory))
         {
             converter.Initialize(frame, PixelFormat.Format32bppPRGBA);
             var bitmap = Bitmap.FromWicBitmap(this.renderContext.RenderTarget, converter, new BitmapProperties(new PixelFormat2D(Format.R8G8B8A8_UNorm_SRgb, AlphaMode.Premultiplied)));
             this.TextureCache[textureKey] = new D3D11Texture(bitmap);
         }
     }
 }
        private Direct2DBitmap CreateBitmap(BitmapFrameDecode bitmapFrameDecode)
        {
            FormatConverter imageFormatConverter = new FormatConverter(_imagingFactory);

            imageFormatConverter.Initialize(
                bitmapFrameDecode,
                WicPixelFormat.Format32bppPRGBA,
                BitmapDitherType.Ordered4x4, null, 0.0, BitmapPaletteType.Custom);
            Direct2DBitmap bitmap = Direct2DBitmap.FromWicBitmap(_renderTarget, imageFormatConverter);

            Utilities.Dispose(ref imageFormatConverter);

            return(bitmap);
        }
Exemplo n.º 9
0
        public static Image2D LoadFromFile(string path, Canvas canvas)
        {
            ImagingFactory   imagingFactory = new ImagingFactory();
            NativeFileStream fileStream     = new NativeFileStream(path,
                                                                   NativeFileMode.Open, NativeFileAccess.Read);
            BitmapDecoder     bitmapDecoder = new BitmapDecoder(imagingFactory, fileStream, DecodeOptions.CacheOnDemand);
            BitmapFrameDecode frame         = bitmapDecoder.GetFrame(0);
            FormatConverter   converter     = new FormatConverter(imagingFactory);

            converter.Initialize(frame, PixelFormatWic.Format32bppPRGBA);

            var result = new Image2D();

            result.nativeBitmap = Bitmap2D.FromWicBitmap(canvas.NativeDeviceContext, converter);
            return(result);
        }
Exemplo n.º 10
0
        private Bitmap CreateBitmap([NotNull] IImage image)
        {
            using (Stream stream = image.GetStream())
                using (BitmapDecoder decoder = new BitmapDecoder(
                           _imagingFactory,
                           stream,
                           DecodeOptions.CacheOnDemand))
                    using (BitmapFrameDecode source = decoder.GetFrame(0))
                        using (FormatConverter converter = new FormatConverter(_imagingFactory))
                        {
                            converter.Initialize(source, SharpDX.WIC.PixelFormat.Format32bppPRGBA);

                            Bitmap bitmap = Bitmap.FromWicBitmap(_renderTarget, converter);

                            Debug.Assert(bitmap != null, "bitmap != null");
                            return(bitmap);
                        }
        }
Exemplo n.º 11
0
        public Bitmap GetBitmap(string file)
        {
            string full;

            System.IO.FileInfo fi = null;
            if (BitmapFile.ContainsKey(file))
            {
                full = BitmapFile[file];
            }
            else
            {
                fi   = new System.IO.FileInfo(file);
                full = fi.FullName;
                BitmapFile.Add(file, full);
            }
            if (Bitmaps.ContainsKey(full))
            {
                return(Bitmaps[full]);
            }
            if (fi == null)
            {
                fi = new System.IO.FileInfo(full);
            }
            if (fi.Exists)
            {
                using (var fac = new ImagingFactory())
                    using (var fs = new NativeFileStream(full, NativeFileMode.Open, NativeFileAccess.Read))
                        using (var dec = new BitmapDecoder(fac, fs, DecodeOptions.CacheOnDemand))
                            using (var frame = dec.GetFrame(0))
                                using (var conv = new FormatConverter(fac))
                                {
                                    conv.Initialize(frame, SharpDX.WIC.PixelFormat.Format32bppPBGRA);
                                    var bit = Bitmap.FromWicBitmap(target, conv);
                                    Bitmaps[full] = bit;
                                    return(bit);
                                }
            }
            else
            {
                Bitmaps.Add(full, null);
                return(null);
            }
        }
Exemplo n.º 12
0
        private void LoadBitmap(RenderTarget device, byte[] bytes)
        {
            if (device == null)
            {
                throw new ArgumentNullException(nameof(device));
            }
            if (bytes == null)
            {
                throw new ArgumentNullException(nameof(bytes));
            }

            try
            {
                var stream    = new MemoryStream(bytes);
                var decoder   = new BitmapDecoder(ImagingFactory, stream, DecodeOptions.CacheOnDemand);
                var frame     = decoder.GetFrame(0);
                var converter = new FormatConverter(ImagingFactory);
                try
                {
                    converter.Initialize(frame, PixelFormat.Format32bppPRGBA);
                    SharpDxBitmap = Bitmap.FromWicBitmap(device, converter);
                }
                catch
                {
                    // falling back to RGB if unsupported
                    converter = new FormatConverter(ImagingFactory);
                    converter.Initialize(frame, PixelFormat.Format32bppRGB);
                    SharpDxBitmap = Bitmap.FromWicBitmap(device, converter);
                }

                converter.Dispose();
                frame.Dispose();
                decoder.Dispose();
                stream.Dispose();
            }
            catch (Exception ex)
            {
                throw new FormatException("Invalid or unsupported image format!", ex);
            }
        }
Exemplo n.º 13
0
        private static Bitmap LoadBitmapFromMemory(RenderTarget device, byte[] bytes)
        {
            if (device == null)
            {
                throw new ArgumentNullException(nameof(device));
            }
            if (bytes == null)
            {
                throw new ArgumentNullException(nameof(bytes));
            }
            if (bytes.Length == 0)
            {
                throw new ArgumentOutOfRangeException(nameof(bytes));
            }

            Bitmap            bmp       = null;
            MemoryStream      stream    = null;
            BitmapDecoder     decoder   = null;
            BitmapFrameDecode frame     = null;
            FormatConverter   converter = null;

            try
            {
                stream  = new MemoryStream(bytes);
                decoder = new BitmapDecoder(ImageFactory, stream, DecodeOptions.CacheOnDemand);

                var pixelFormat = ImagePixelFormats.GetBestPixelFormat(decoder.DecoderInfo?.PixelFormats);

                frame = decoder.GetFrame(0);

                converter = new FormatConverter(ImageFactory);

                try
                {
                    converter.Initialize(frame, pixelFormat);
                    bmp = Bitmap.FromWicBitmap(device, converter);
                }
                catch
                {
                    TryCatch(() => converter.Dispose());

                    converter = new FormatConverter(ImageFactory);
                    converter.Initialize(frame, PixelFormat.Format32bppRGB);
                    bmp = Bitmap.FromWicBitmap(device, converter);
                }

                converter.Dispose();
                frame.Dispose();
                decoder.Dispose();
                stream.Dispose();

                return(bmp);
            }
            catch
            {
                if (converter?.IsDisposed == false)
                {
                    converter.Dispose();
                }
                if (frame?.IsDisposed == false)
                {
                    frame.Dispose();
                }
                if (decoder?.IsDisposed == false)
                {
                    decoder.Dispose();
                }
                if (stream != null)
                {
                    TryCatch(() => stream.Dispose());
                }
                if (bmp?.IsDisposed == false)
                {
                    bmp.Dispose();
                }

                throw;
            }
        }
Exemplo n.º 14
0
 public static Bitmap LoadBitmap(string fileName, RenderTarget target)
 {
     using (var bmp = LoadBitmapAsWic(fileName)) {
         return(Bitmap.FromWicBitmap(target, bmp));
     }
 }
Exemplo n.º 15
0
 private static Bitmap LoadBitmap(SharpDX.Direct2D1.RenderTarget target, string fileName)
 {
     using (var bmp = WicHelper.LoadBitmapSourceFromFile(fileName)) {
         return(Bitmap.FromWicBitmap(target, bmp));
     }
 }
Exemplo n.º 16
0
        public static BitmapBrush makeBitmapBrush(RenderTarget renderTarget, string imgName, bool blankImage = false)
        {
            string imageSrc = Program.spriteFileDir + imgName;

            if (blankImage)
            {
                var pf = new SharpDX.Direct2D1.PixelFormat()
                {
                    AlphaMode = SharpDX.Direct2D1.AlphaMode.Premultiplied,
                    Format    = Format.B8G8R8A8_UNorm
                };

                BitmapRenderTarget pallete = new BitmapRenderTarget(renderTarget, CompatibleRenderTargetOptions.GdiCompatible, pf);
                return(new BitmapBrush(renderTarget, pallete.Bitmap, new BitmapBrushProperties()
                {
                    ExtendModeX = ExtendMode.Wrap, ExtendModeY = ExtendMode.Wrap
                }));
            }
            if (File.Exists(imageSrc))
            {
                ImagingFactory   imagingFactory = new ImagingFactory();
                NativeFileStream fileStream     = new NativeFileStream(imageSrc,
                                                                       NativeFileMode.Open, NativeFileAccess.Read);
                BitmapDecoder bitmapDecoder = new BitmapDecoder(imagingFactory, fileStream, DecodeOptions.CacheOnDemand);

                BitmapFrameDecode frame = bitmapDecoder.GetFrame(0);

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

                Bitmap bitmap = Bitmap.FromWicBitmap(renderTarget, converter);

                Utilities.Dispose(ref bitmapDecoder);
                Utilities.Dispose(ref fileStream);
                Utilities.Dispose(ref imagingFactory);
                Utilities.Dispose(ref converter);

                return(new BitmapBrush(renderTarget, bitmap, new BitmapBrushProperties()
                {
                    ExtendModeX = ExtendMode.Wrap, ExtendModeY = ExtendMode.Wrap
                }));
            }
            else
            {
                Console.WriteLine("{0} missing", imageSrc);

                var pf = new SharpDX.Direct2D1.PixelFormat()
                {
                    AlphaMode = SharpDX.Direct2D1.AlphaMode.Premultiplied,
                    Format    = Format.B8G8R8A8_UNorm
                };
                BitmapRenderTarget pallete = new BitmapRenderTarget(renderTarget, CompatibleRenderTargetOptions.GdiCompatible, new Size2F(30f, 30f), new Size2(1, 1), pf);

                pallete.BeginDraw();
                pallete.Clear(Color.Purple);
                pallete.EndDraw();

                return(new BitmapBrush(renderTarget, pallete.Bitmap, new BitmapBrushProperties()
                {
                    ExtendModeX = ExtendMode.Wrap, ExtendModeY = ExtendMode.Wrap
                }));
            }
        }
Exemplo n.º 17
0
        public BitmapBrush makeBitmapBrush(string imgName, RenderTarget renderTarget, bool blankImage = false)
        {
            //TODO : 여기 바꿔라!
            string imageSrc = "";

            if (blankImage)
            {
                SharpDX.Direct2D1.PixelFormat pf = new SharpDX.Direct2D1.PixelFormat()
                {
                    AlphaMode = D3DHandler.ALPHA_MODE,
                    Format    = D3DHandler.RENDER_FORMAT
                };

                BitmapRenderTarget pallete = new BitmapRenderTarget(renderTarget, CompatibleRenderTargetOptions.GdiCompatible, pf);
                return(new BitmapBrush(renderTarget, pallete.Bitmap, new BitmapBrushProperties()
                {
                    ExtendModeX = ExtendMode.Wrap, ExtendModeY = ExtendMode.Wrap
                }));
            }
            if (resourceCore.getFile(imgName, out ResFile resFile))
            {
                ImagingFactory imagingFactory = new ImagingFactory();

                /*NativeFileStream fileStream = new NativeFileStream(imageSrc,
                 *  NativeFileMode.Open, NativeFileAccess.Read);*/
                MemoryStream  ms            = new MemoryStream(resFile.rawData);
                BitmapDecoder bitmapDecoder = new BitmapDecoder(imagingFactory, ms, DecodeOptions.CacheOnDemand);

                BitmapFrameDecode frame = bitmapDecoder.GetFrame(0);

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

                Bitmap bitmap = Bitmap.FromWicBitmap(renderTarget, converter);

                Utilities.Dispose(ref bitmapDecoder);
                Utilities.Dispose(ref imagingFactory);
                Utilities.Dispose(ref converter);

                return(new BitmapBrush(renderTarget, bitmap, new BitmapBrushProperties()
                {
                    ExtendModeX = ExtendMode.Wrap, ExtendModeY = ExtendMode.Wrap
                }));
            }
            else
            {
                Console.WriteLine("{0} missing", imageSrc);

                SharpDX.Direct2D1.PixelFormat pf = new SharpDX.Direct2D1.PixelFormat()
                {
                    AlphaMode = D3DHandler.ALPHA_MODE,
                    Format    = D3DHandler.RENDER_FORMAT
                };
                BitmapRenderTarget pallete = new BitmapRenderTarget(renderTarget, CompatibleRenderTargetOptions.GdiCompatible, new Size2F(30f, 30f), new Size2(1, 1), pf);

                pallete.BeginDraw();
                pallete.Clear(Color.Purple);
                pallete.EndDraw();

                return(new BitmapBrush(renderTarget, pallete.Bitmap, new BitmapBrushProperties()
                {
                    ExtendModeX = ExtendMode.Wrap, ExtendModeY = ExtendMode.Wrap
                }));
            }
        }