private static Texture2D CreateTexture2DFromBitmapSource(Device device, BitmapSource bitmapSource, Texture2DLoadOptions options) { var stride = bitmapSource.Size.Width * 4; // Allocate DataStream to receive the WIC image pixels using (var buffer = new DataStream(bitmapSource.Size.Height * stride, true, true)) { // Copy the content of the WIC to the buffer bitmapSource.CopyPixels(stride, buffer); var texture2DDescription = new Texture2DDescription { Width = bitmapSource.Size.Width, Height = bitmapSource.Size.Height, ArraySize = 1, BindFlags = options.BindFlags, Usage = options.ResourceUsage, CpuAccessFlags = options.CpuAccessFlags, Format = options.Format, MipLevels = options.MipLevels, OptionFlags = ResourceOptionFlags.None, SampleDescription = new SampleDescription(1, 0) }; bitmapSource.Dispose(); var dataRectangle = new DataRectangle(buffer.DataPointer, stride); return(new Texture2D(device, texture2DDescription, dataRectangle)); } }
private static Texture2D CreateTex2DFromBitmap(Device device, BitmapSource bsource) { Texture2DDescription desc; desc.Width = bsource.Size.Width; desc.Height = bsource.Size.Height; desc.ArraySize = 1; desc.BindFlags = BindFlags.ShaderResource; desc.Usage = ResourceUsage.Default; desc.CpuAccessFlags = CpuAccessFlags.None; desc.Format = Format.R8G8B8A8_UNorm; desc.MipLevels = 1; desc.OptionFlags = ResourceOptionFlags.None; desc.SampleDescription.Count = 1; desc.SampleDescription.Quality = 0; var s = new DataStream(bsource.Size.Height * bsource.Size.Width * 4, true, true); bsource.CopyPixels(bsource.Size.Width * 4, s); var rect = new DataRectangle(s.DataPointer, bsource.Size.Width * 4); var t2D = new Texture2D(device, desc, rect); s.Close(); bsource.Dispose(); return(t2D); }
/// <summary> /// Loads a Bitmap into the BitmapX /// </summary> /// <param name="bitmap">Bitmap Source</param> public void LoadBitmap(Bitmap bitmap) { // Dispose the Original if exists BitmapSource?.Dispose(); // Get Bpp _BitsPerPixel = Image.GetPixelFormatSize(bitmap.PixelFormat); // Check for supported Bpp if (!AcceptedBitsPerPixel.Contains(BitsPerPixel)) { throw new ArgumentException("Unsupported Bitmap Pixel Size: " + BitsPerPixel.ToString()); } // Set Bitmap BitmapSource = bitmap; // Set Width + Height _Width = BitmapSource.Width; _Height = BitmapSource.Height; LockBits(); }
/// <summary> /// Disposes of the BitmapX Object /// </summary> public void Dispose() { // Dispose the Bitmap BitmapSource?.Dispose(); }
private static Texture2D CreateTexture2DFromBitmapSource(Device device, BitmapSource bitmapSource, TextureLoadOptions options) { // Allocate DataStream to receive the WIC image pixels var 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 bitmapSource.CopyPixels(stride, buffer); var texture2DDescription = new Texture2DDescription() { Width = bitmapSource.Size.Width, Height = bitmapSource.Size.Height, ArraySize = 1, BindFlags = options.BindFlags, Usage = options.ResourceUsage, CpuAccessFlags = options.CpuAccessFlags, Format = options.Format, MipLevels = options.MipLevels, OptionFlags = ResourceOptionFlags.None, SampleDescription = new SampleDescription(1, 0), }; bitmapSource.Dispose(); var dataRectangle = new DataRectangle(buffer.DataPointer, stride); return new Texture2D(device, texture2DDescription, dataRectangle); } }