Ejemplo n.º 1
0
        private void GetImageFromPath(RenderTarget pRenderer, string pPath)
        {
            using (System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(pPath))
            {
                System.Drawing.Imaging.BitmapData bmpData =
                    bitmap.LockBits(
                        new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
                        System.Drawing.Imaging.ImageLockMode.WriteOnly,
                        bitmap.PixelFormat
                        );

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

                //// Declare an array to hold the bytes of the bitmap.
                //IntPtr ptr = bmpData.Scan0;

                //byte[] bytes = new byte[bmpData.Stride * bitmap.Height];

                // Unlock the bits.
                bitmap.UnlockBits(bmpData);
            }

            BitmapDecoder decoder = ImagingFactory.CreateDecoderFromFilename(pPath, DesiredAccess.Read, DecodeMetadataCacheOptions.OnDemand);

            BitmapFrameDecode  frameDeocder    = decoder.GetFrame(0);
            WICFormatConverter formatConverter = ImagingFactory.CreateFormatConverter();
            BitmapSource       src             = frameDeocder.ToBitmapSource();

            formatConverter.Initialize(frameDeocder.ToBitmapSource(), PixelFormats.Pf32bppPBGRA, BitmapDitherType.None, BitmapPaletteType.MedianCut);

            ImageResource = pRenderer.CreateBitmapFromWicBitmap(formatConverter.ToBitmapSource());
        }
Ejemplo n.º 2
0
        public static D2DBitmap GetBitmap(BitmapData pBitmapData, RenderTarget pRenderer)
        {
            try
            {
                BitmapDecoder decoder = ImagingFactory.CreateDecoderFromFilename(pBitmapData.FilePath, DesiredAccess.Read, DecodeMetadataCacheOptions.OnDemand);//.CreateDecoderFromStream(pBitmapData.Data, DecodeMetadataCacheOptions.OnDemand);

                BitmapFrameDecode  frameDeocder    = decoder.GetFrame(0);
                WICFormatConverter formatConverter = ImagingFactory.CreateFormatConverter();
                BitmapSource       src             = frameDeocder.ToBitmapSource();
                formatConverter.Initialize(frameDeocder.ToBitmapSource(), PixelFormats.Pf32bppPBGRA, BitmapDitherType.None, BitmapPaletteType.MedianCut);

                return(pRenderer.CreateBitmapFromWicBitmap(formatConverter.ToBitmapSource()));
            }
            catch (Exception)
            {
                return(null);
            }
        }
        private static D2DBitmap CreateBitmapFromDecoder(RenderTarget renderTarget, ImagingFactory wicFactory, BitmapDecoder decoder)
        {
            BitmapFrameDecode source;
            FormatConverter converter;
            // Create the initial frame.
            source = decoder.GetFrame(0);

            // Convert the image format to 32bppPBGRA -- which Direct2D expects.
            converter = wicFactory.CreateFormatConverter();
            converter.Initialize(
                source.ToBitmapSource(),
                PixelFormats.Pbgra32Bpp,
                BitmapDitherType.None,
                BitmapPaletteType.MedianCut
                );

            // Create a Direct2D bitmap from the WIC bitmap.
            return renderTarget.CreateBitmapFromWicBitmap(
                converter.ToBitmapSource());
        }
Ejemplo n.º 4
0
        private static D2DBitmap CreateBitmapFromDecoder(RenderTarget renderTarget, ImagingFactory wicFactory, BitmapDecoder decoder)
        {
            BitmapFrameDecode source;
            FormatConverter   converter;

            // Create the initial frame.
            source = decoder.GetFrame(0);

            // Convert the image format to 32bppPBGRA -- which Direct2D expects.
            converter = wicFactory.CreateFormatConverter();
            converter.Initialize(
                source.ToBitmapSource(),
                PixelFormats.Pbgra32Bpp,
                BitmapDitherType.None,
                BitmapPaletteType.MedianCut
                );

            // Create a Direct2D bitmap from the WIC bitmap.
            return(renderTarget.CreateBitmapFromWicBitmap(
                       converter.ToBitmapSource()));
        }
Ejemplo n.º 5
0
        private void TryCreateBackgroundBitmap()
        {
            if (RenderTarget != null && (imageHash != lastImageHash) && ((isDirty && BackgroundImage != null) || (background == null && BackgroundImage != null)))
            {
                using (var ms = new System.IO.MemoryStream()) {
                    ConvertImageToStreamAndAdjustBrightness(BackgroundImage, Data.BackgroundAlpha, ms);
                    ms.Position = 0;

                    using (var factory = WIC.ImagingFactory.Create()) {
                        using (WIC.BitmapDecoder decoder = factory.CreateDecoderFromStream(ms, WIC.DecodeMetadataCacheOption.OnDemand)) {
                            using (WIC.BitmapFrameDecode source = decoder.GetFrame(0)) {
                                using (WIC.FormatConverter converter = factory.CreateFormatConverter()) {
                                    converter.Initialize(source.ToBitmapSource(), WIC.PixelFormats.Pbgra32Bpp, WIC.BitmapDitherType.None, WIC.BitmapPaletteType.MedianCut);
                                    background    = RenderTarget.CreateBitmapFromWicBitmap(converter.ToBitmapSource());
                                    isDirty       = false;
                                    lastImageHash = imageHash;
                                }
                            }
                        }
                    }
                }
            }
        }