예제 #1
0
        /// <summary>
        ///
        /// </summary>
        public static unsafe WriteableBitmap GetBitmap(this SharpDX.Direct3D11.Texture2D tex)
        {
            DataRectangle db;

            using (var copy = tex.GetCopy())
                using (var surface = copy.QueryInterface <SharpDX.DXGI.Surface>())
                {
                    // can't destroy the surface now with WARP driver
                    DataStream ds;
                    db = surface.Map(DXGI.MapFlags.Read, out ds);

                    int w  = tex.Description.Width;
                    int h  = tex.Description.Height;
                    var wb = new WriteableBitmap(w, h, 96.0, 96.0, PixelFormats.Bgra32, null);
                    wb.Lock();
                    try
                    {
                        uint *wbb = (uint *)wb.BackBuffer;

                        ds.Position = 0;
                        for (int y = 0; y < h; y++)
                        {
                            ds.Position = y * db.Pitch;
                            for (int x = 0; x < w; x++)
                            {
                                var c = ds.Read <uint>();
                                wbb[y * w + x] = c;
                            }
                        }
                        ds.Dispose();
                    }
                    finally
                    {
                        wb.AddDirtyRect(new Int32Rect(0, 0, w, h));
                        wb.Unlock();
                    }
                    return(wb);
                }
        }