示例#1
0
 /// <summary>
 /// Creates a <see cref="SharpDX.Direct3D11.Texture2D"/> from a WIC <see cref="SharpDX.WIC.BitmapSource"/>
 /// </summary>
 /// <param name="device">The Direct3D11 device</param>
 /// <param name="bitmapSource">The WIC bitmap source</param>
 /// <returns>A Texture2D</returns>
 public static Texture2D CreateTexture2DFromBitmap(Device device, BitmapSource bitmapSource)
 {
     // Allocate DataStream to receive the WIC image pixels
         int stride = bitmapSource.Size.Width * 4;
         using (var buffer = new DataStream(bitmapSource.Size.Height * stride, true, true))
         {
         // Copy the content of the WIC to the buffer
         Rectangle rect = new Rectangle();
         var methods = bitmapSource.GetType().GetMethods();
         bitmapSource.GetType().GetMethods().First(item => item.Name == "CopyPixels").Invoke(bitmapSource, new object[] { stride, buffer });
           //  bitmapSource.CopyPixels(stride, buffer);
             return new Texture2D(device, new Texture2DDescription()
             {
                 Width = bitmapSource.Size.Width,
                 Height = bitmapSource.Size.Height,
                 ArraySize = 1,
                 BindFlags = BindFlags.ShaderResource,
                 Usage = ResourceUsage.Immutable,
                 CpuAccessFlags =CpuAccessFlags.None,
                 Format = Format.R8G8B8A8_UNorm,
                 MipLevels = 1,
                 OptionFlags = ResourceOptionFlags.None,
                 SampleDescription = new SampleDescription(1, 0),
             }, new DataRectangle(buffer.DataPointer, stride));
         }
 }