public void Dispose() { Device?.Dispose(); Device = null; ImagingFactory2?.Dispose(); ImagingFactory2 = null; }
public static Texture2D LoadTexture2D(Device device, string fileName) { var factory = new ImagingFactory2(); var bitmapSource = LoadBitmap(factory, fileName); factory.Dispose(); int stride = bitmapSource.Size.Width * 4; using (var buffer = new DataStream(bitmapSource.Size.Height * stride, true, true)) { bitmapSource.CopyPixels(stride, buffer); var texture = 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)); bitmapSource.Dispose(); return(texture); } }
private void SaveToFile(D3DX direct3D, Texture2D texture, Guid format) { var stream = new System.IO.FileStream("Output.png", System.IO.FileMode.Create); var textureTarget = texture; var textureCopy = new Texture2D(direct3D.Device, new Texture2DDescription { Width = (int)textureTarget.Description.Width, Height = (int)textureTarget.Description.Height, MipLevels = 1, ArraySize = 1, Format = textureTarget.Description.Format, Usage = ResourceUsage.Staging, SampleDescription = new SampleDescription(1, 0), BindFlags = BindFlags.None, CpuAccessFlags = CpuAccessFlags.Read, OptionFlags = ResourceOptionFlags.None }); direct3D.DeviceContext.CopyResource(textureTarget, textureCopy); var dataBox = direct3D.DeviceContext.MapSubresource( textureCopy, 0, 0, MapMode.Read, SharpDX.Direct3D11.MapFlags.None, out DataStream dataStream); var dataRectangle = new DataRectangle { DataPointer = dataStream.DataPointer, Pitch = dataBox.RowPitch }; var imagingFactory = new ImagingFactory2(); var bitmap = new SharpDX.WIC.Bitmap( imagingFactory, textureCopy.Description.Width, textureCopy.Description.Height, format, dataRectangle); using (var s = stream) { s.Position = 0; using (var bitmapEncoder = new PngBitmapEncoder(imagingFactory, s)) { using (var bitmapFrameEncode = new BitmapFrameEncode(bitmapEncoder)) { bitmapFrameEncode.Initialize(); bitmapFrameEncode.SetSize(bitmap.Size.Width, bitmap.Size.Height); var pixelFormat = SharpDX.WIC.PixelFormat.FormatDontCare; bitmapFrameEncode.SetPixelFormat(ref pixelFormat); bitmapFrameEncode.WriteSource(bitmap); bitmapFrameEncode.Commit(); bitmapEncoder.Commit(); bitmapFrameEncode.Dispose(); bitmapEncoder.Dispose(); } } } direct3D.DeviceContext.UnmapSubresource(textureCopy, 0); textureCopy.Dispose(); bitmap.Dispose(); imagingFactory.Dispose(); dataStream.Dispose(); stream.Dispose(); }
public static void DisposeFactory() { _factory?.Dispose(); _factory = null; }