Esempio n. 1
0
        public static MemoryStream ToMemoryStream(this global::SharpDX.WIC.Bitmap bitmap,
                                                  IDevice2DResources deviceResources,
                                                  Direct2DImageFormat imageType = Direct2DImageFormat.Bmp)
        {
            if (bitmap == null)
            {
                return(null);
            }

            var systemStream = new MemoryStream();

            using (var stream = new WICStream(deviceResources.WICImgFactory, systemStream))
            {
                using (var encoder = new BitmapEncoder(deviceResources.WICImgFactory, imageType.ToWICImageFormat()))
                {
                    encoder.Initialize(stream);
                    using (var frameEncoder = new BitmapFrameEncode(encoder))
                    {
                        frameEncoder.Initialize();
                        frameEncoder.SetSize(bitmap.Size.Width, bitmap.Size.Height);
                        frameEncoder.WriteSource(bitmap);
                        frameEncoder.Commit();
                        encoder.Commit();
                        return(systemStream);
                    }
                }
            }
        }
Esempio n. 2
0
        public static global::SharpDX.WIC.Bitmap CreateBitmapStream(IDevice2DResources deviceResources, int width, int height, Direct2DImageFormat imageType, Action <RenderTarget> drawingAction)
        {
            if (width <= 0 || height <= 0)
            {
                return(null);
            }

            var bitmap = new global::SharpDX.WIC.Bitmap(deviceResources.WICImgFactory, width, height, global::SharpDX.WIC.PixelFormat.Format32bppBGR,
                                                        BitmapCreateCacheOption.CacheOnDemand);

            using (var target = new WicRenderTarget(deviceResources.Factory2D, bitmap,
                                                    new RenderTargetProperties()
            {
                DpiX = 96,
                DpiY = 96,
                MinLevel = FeatureLevel.Level_DEFAULT,
                PixelFormat = new global::SharpDX.Direct2D1.PixelFormat(global::SharpDX.DXGI.Format.Unknown, AlphaMode.Unknown)
            }))
            {
                target.Transform = Matrix3x2.Identity;
                target.BeginDraw();
                drawingAction(target);
                target.EndDraw();
            }
            return(bitmap);
        }
Esempio n. 3
0
        public IImage Downsize(float maxWidthOrHeight, bool disposeOriginal = false)
        {
            if (Width > maxWidthOrHeight || Height > maxWidthOrHeight)
            {
                float factor;

                if (Width > Height)
                {
                    factor = maxWidthOrHeight / Width;
                }
                else
                {
                    factor = maxWidthOrHeight / Height;
                }

                var w = (int)Math.Round(factor * Width);
                var h = (int)Math.Round(factor * Height);

                using (var memoryStream = new MemoryStream())
                {
                    Save(memoryStream);
                    memoryStream.Position = 0;

                    global::SharpDX.WIC.Bitmap wicBitmap = WicBitmapExtensions.LoadFromStream(memoryStream);
                    using (var memoryStream2 = new MemoryStream())
                    {
                        wicBitmap.WritePngToStream(memoryStream2, w, h);
                        memoryStream2.Position = 0;
                        var newImage = DXGraphicsService.Instance.LoadImageFromStream(memoryStream2);
                        if (disposeOriginal)
                        {
                            _bitmap.Dispose();
                        }

                        return(newImage);
                    }
                }
            }

            return(this);
        }
Esempio n. 4
0
        /// <summary>
        /// Copies to a stream using WIC. The format is converted if necessary.
        /// </summary>
        /// <param name="context"></param>
        /// <param name="texture"></param>
        /// <param name="outputFormat"></param>
        /// <param name="stream"></param>
        public void ToStream(global::SharpDX.Direct3D11.DeviceContext context, Texture2D texture, ImageFormat outputFormat, Stream stream)
        {
            if (wicFactory == null)
            {
                wicFactory = ToDispose(new global::SharpDX.WIC.ImagingFactory2());
            }

            DataStream dataStream;
            var        dataBox = context.MapSubresource(
                texture,
                0,
                0,
                MapMode.Read,
                global::SharpDX.Direct3D11.MapFlags.None,
                out dataStream);

            try
            {
                var dataRectangle = new DataRectangle
                {
                    DataPointer = dataStream.DataPointer,
                    Pitch       = dataBox.RowPitch
                };

                var format = PixelFormatFromFormat(texture.Description.Format);

                if (format == Guid.Empty)
                {
                    return;
                }

                using (var bitmap = new global::SharpDX.WIC.Bitmap(
                           wicFactory,
                           texture.Description.Width,
                           texture.Description.Height,
                           format,
                           dataRectangle))
                {
                    stream.Position = 0;

                    global::SharpDX.WIC.BitmapEncoder bitmapEncoder = null;
                    switch (outputFormat)
                    {
                    case ImageFormat.Bitmap:
                        bitmapEncoder = new global::SharpDX.WIC.BmpBitmapEncoder(wicFactory, stream);
                        break;

                    case ImageFormat.Jpeg:
                        bitmapEncoder = new global::SharpDX.WIC.JpegBitmapEncoder(wicFactory, stream);
                        break;

                    case ImageFormat.Png:
                        bitmapEncoder = new global::SharpDX.WIC.PngBitmapEncoder(wicFactory, stream);
                        break;

                    default:
                        return;
                    }

                    try
                    {
                        using (var bitmapFrameEncode = new global::SharpDX.WIC.BitmapFrameEncode(bitmapEncoder))
                        {
                            bitmapFrameEncode.Initialize();
                            bitmapFrameEncode.SetSize(bitmap.Size.Width, bitmap.Size.Height);
                            var pixelFormat = format;
                            bitmapFrameEncode.SetPixelFormat(ref pixelFormat);

                            if (pixelFormat != format)
                            {
                                // IWICFormatConverter
                                var converter = new global::SharpDX.WIC.FormatConverter(wicFactory);
                                if (converter.CanConvert(format, pixelFormat))
                                {
                                    converter.Initialize(bitmap, global::SharpDX.WIC.PixelFormat.Format24bppBGR, global::SharpDX.WIC.BitmapDitherType.None, null, 0, global::SharpDX.WIC.BitmapPaletteType.MedianCut);
                                    bitmapFrameEncode.SetPixelFormat(ref pixelFormat);
                                    bitmapFrameEncode.WriteSource(converter);
                                }
                                else
                                {
                                    this.DebugMessage(string.Format("Unable to convert Direct3D texture format {0} to a suitable WIC format", texture.Description.Format.ToString()));
                                    return;
                                }
                            }
                            else
                            {
                                bitmapFrameEncode.WriteSource(bitmap);
                            }
                            bitmapFrameEncode.Commit();
                            bitmapEncoder.Commit();
                        }
                    }
                    finally
                    {
                        bitmapEncoder.Dispose();
                    }
                }
            }
            finally
            {
                context.UnmapSubresource(texture, 0);
            }
        }