コード例 #1
0
        /// <summary>
        /// Creates device independent resources.
        /// </summary>
        /// <remarks>
        /// This method is called at the initialization of this instance.
        /// </remarks>
        protected virtual void CreateDeviceIndependentResources()
        {
#if DEBUG
            var debugLevel = global::SharpDX.Direct2D1.DebugLevel.Information;
#else
            var debugLevel = global::SharpDX.Direct2D1.DebugLevel.None;
#endif
            // Dispose previous references and set to null
            RemoveAndDispose(ref d2dFactory);
            RemoveAndDispose(ref dwriteFactory);
            RemoveAndDispose(ref wicFactory);

            // Allocate new references
            d2dFactory    = Collect(new global::SharpDX.Direct2D1.Factory1(global::SharpDX.Direct2D1.FactoryType.SingleThreaded, debugLevel));
            dwriteFactory = Collect(new global::SharpDX.DirectWrite.Factory(global::SharpDX.DirectWrite.FactoryType.Shared));
            wicFactory    = Collect(new global::SharpDX.WIC.ImagingFactory2());
        }
コード例 #2
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);
            }
        }