private void GetImageFromPath(RenderTarget pRenderer, string pPath) { using (System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(pPath)) { System.Drawing.Imaging.BitmapData bmpData = bitmap.LockBits( new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, bitmap.PixelFormat ); Size = new Size(bitmap.Width, bitmap.Height); //// Declare an array to hold the bytes of the bitmap. //IntPtr ptr = bmpData.Scan0; //byte[] bytes = new byte[bmpData.Stride * bitmap.Height]; // Unlock the bits. bitmap.UnlockBits(bmpData); } BitmapDecoder decoder = ImagingFactory.CreateDecoderFromFilename(pPath, DesiredAccess.Read, DecodeMetadataCacheOptions.OnDemand); BitmapFrameDecode frameDeocder = decoder.GetFrame(0); WICFormatConverter formatConverter = ImagingFactory.CreateFormatConverter(); BitmapSource src = frameDeocder.ToBitmapSource(); formatConverter.Initialize(frameDeocder.ToBitmapSource(), PixelFormats.Pf32bppPBGRA, BitmapDitherType.None, BitmapPaletteType.MedianCut); ImageResource = pRenderer.CreateBitmapFromWicBitmap(formatConverter.ToBitmapSource()); }
/// <summary> /// Loads an existing image file into a SharpDX.Direct2D1.Bitmap1. /// </summary> /// <param name="filePath">Relative path to the content file.</param> /// <returns>Loaded bitmap.</returns> private SharpDX.Direct2D1.Bitmap1 LoadBitmapFromContentFile(string filePath) { SharpDX.Direct2D1.Bitmap1 newBitmap; // Neccessary for creating WIC objects. ImagingFactory imagingFactory = new ImagingFactory(); NativeFileStream fileStream = new NativeFileStream(Package.Current.InstalledLocation.Path + filePath, NativeFileMode.Open, NativeFileAccess.Read); // Used to read the image source file. BitmapDecoder bitmapDecoder = new BitmapDecoder(imagingFactory, fileStream, DecodeOptions.CacheOnDemand); // Get the first frame of the image. BitmapFrameDecode frame = bitmapDecoder.GetFrame(0); // Convert it to a compatible pixel format. FormatConverter converter = new FormatConverter(imagingFactory); converter.Initialize(frame, SharpDX.WIC.PixelFormat.Format32bppPRGBA); // Create the new Bitmap1 directly from the FormatConverter. newBitmap = SharpDX.Direct2D1.Bitmap1.FromWicBitmap(d2dContext, converter); Utilities.Dispose(ref bitmapDecoder); Utilities.Dispose(ref fileStream); Utilities.Dispose(ref imagingFactory); Utilities.Dispose(ref converter); Utilities.Dispose(ref frame); return(newBitmap); }
private Bitmap CreateBitmap(string path) { var renderTarget = renderTargetProvider.RenderTarget; var imagingFactory = imagingFactoryProvider.ImagingFactory; if (renderTarget == null || renderTarget.IsDisposed || imagingFactory == null || imagingFactory.IsDisposed) { return(null); // TODO: throw exception } var fileStream = new NativeFileStream(path, NativeFileMode.Open, NativeFileAccess.Read); var bitmapDecoder = new BitmapDecoder(imagingFactory, fileStream, DecodeOptions.CacheOnDemand); var frame = bitmapDecoder.GetFrame(0); var converter = new FormatConverter(imagingFactory); converter.Initialize(frame, PixelFormat.Format32bppPRGBA); var bitmap = Bitmap.FromWicBitmap(renderTarget, converter); Utilities.Dispose(ref bitmapDecoder); Utilities.Dispose(ref fileStream); return(bitmap); }
private static BitmapSource LoadBitmap(string filename) { Imgfactory = new ImagingFactory(); var d = new BitmapDecoder( Imgfactory, filename, DecodeOptions.CacheOnDemand ); // var frame = d.GetFrame(0); var fconv = new FormatConverter(Imgfactory); fconv.Initialize(frame, PixelFormat.Format32bppPRGBA, BitmapDitherType.None, null, 0.0, BitmapPaletteType.Custom); d.Dispose(); frame.Dispose(); //fconv.Dispose(); Imgfactory.Dispose(); //MessageBox.Show("Pause"); return(fconv); //return null; }
/// <summary> /// Заргуражет писели из картинки /// </summary> /// <param name="device">Устройство с помощью которого будет рисоваться эта картинка</param> /// <param name="filename">Путь к файлу с картинкой</param> /// <returns>Набор пикселей</returns> public static BitmapSource LoadBitmapSource(SharpDX.Direct3D11.DeviceContext device, string filename) { var pFormat = SharpDX.WIC.PixelFormat.Format32bppPRGBA; string ext = System.IO.Path.GetExtension(filename); if (ext.ToLower() == ".dds") { pFormat = SharpDX.WIC.PixelFormat.Format32bppRGBA; } using (var Imgfactory = new ImagingFactory2()) using (var d = new BitmapDecoder( Imgfactory, filename, DecodeOptions.CacheOnDemand )) using (var frame = d.GetFrame(0)) { var fconv = new FormatConverter(Imgfactory); fconv.Initialize( frame, pFormat, BitmapDitherType.None, null, 0.0, BitmapPaletteType.Custom); return(fconv); } }
public ShaderResourceView LoadBitmapShaderResource(System.Drawing.Bitmap btm) { ShaderResourceView res = null; try { var ms = new MemoryStream(); btm.Save(ms, System.Drawing.Imaging.ImageFormat.Png); ms.Position = 0; var factory = new SharpDX.WIC.ImagingFactory(); var bitmapDecoder = new BitmapDecoder(factory, ms, DecodeOptions.CacheOnDemand); var result = new FormatConverter(factory); result.Initialize(bitmapDecoder.GetFrame(0), PixelFormat.Format32bppPRGBA, BitmapDitherType.None, null, 0.0, BitmapPaletteType.Custom); using (var texture = CreateTexture2DFromBitmap(device, result)) { var srvDesc = new ShaderResourceViewDescription() { Format = texture.Description.Format, Dimension = SharpDX.Direct3D.ShaderResourceViewDimension.Texture2D, }; srvDesc.Texture2D.MostDetailedMip = 0; srvDesc.Texture2D.MipLevels = -1; res = new ShaderResourceView(device, texture, srvDesc); device.ImmediateContext.GenerateMips(res); } // TextureResource = ShaderResourceView.FromFile(device, fileName); } catch (Exception ex) { System.Diagnostics.Trace.WriteLine($"TexturedLoader {ex.Message}"); } return(res); }
public bool LoadFromStream(string textureKey, Stream stream) { try { if (this.textureCache.ContainsKey(textureKey)) { return(true); } using (var bitmapDecoder = new BitmapDecoder(this.imagingFactory, stream, DecodeOptions.CacheOnDemand)) { var frame = bitmapDecoder.GetFrame(0); using (var converter = new FormatConverter(this.imagingFactory)) { converter.Initialize(frame, PixelFormat.Format32bppPRGBA); // Format32bppPRGBA var bitmap = Bitmap.FromWicBitmap(this.renderContext.RenderTarget, converter); this.textureCache[textureKey] = new D3D11Texture(bitmap); } } return(true); } catch (Exception e) { Log.Error(e); return(false); } }
public static Bitmap Decode(RenderTarget device, BitmapDecoder decoder) { var frame = decoder.GetFrame(0); var converter = new FormatConverter(Image.ImageFactory); foreach (var format in _pixelFormatEnumerator) { try { converter.Initialize(frame, format); var bmp = Bitmap.FromWicBitmap(device, converter); TryCatch(() => converter.Dispose()); TryCatch(() => frame.Dispose()); return(bmp); } catch { TryCatch(() => converter.Dispose()); converter = new FormatConverter(Image.ImageFactory); } } TryCatch(() => converter.Dispose()); TryCatch(() => frame.Dispose()); throw new Exception("Unsupported Image Format!"); }
/// <summary> /// Initializes a new instance of the <see cref="WicBitmapImpl"/> class. /// </summary> /// <param name="stream">The stream to read the bitmap from.</param> public WicBitmapImpl(Stream stream) { // https://stackoverflow.com/questions/48982749/decoding-image-from-stream-using-wic/48982889#48982889 _decoder = new BitmapDecoder(Direct2D1Platform.ImagingFactory, stream, DecodeOptions.CacheOnLoad); WicImpl = new Bitmap(Direct2D1Platform.ImagingFactory, _decoder.GetFrame(0), BitmapCreateCacheOption.CacheOnLoad); }
/// <summary> /// Gets bitmap. /// </summary> /// <param name="image">The image.</param> /// <returns>Returns bitmap.</returns> private Bitmap GetBitmap(OxyImage image) { if (image == null) { return(null); } if (!this.imagesInUse.Contains(image)) { this.imagesInUse.Add(image); } Bitmap res; using (var stream = new MemoryStream(image.GetData())) { var decoder = new BitmapDecoder(this.wicFactory, stream, DecodeOptions.CacheOnDemand); var frame = decoder.GetFrame(0); var converter = new FormatConverter(this.wicFactory); converter.Initialize(frame, dx.WIC.PixelFormat.Format32bppPRGBA); res = Bitmap.FromWicBitmap(this.renderTarget, converter); } this.imageCache.Add(image, res); return(res); }
private static BitmapSource LoadBitmap(string filename, out bool IsSRgb) { var bitmapDecoder = new BitmapDecoder( Factory, filename, DecodeOptions.CacheOnDemand ); BitmapFrameDecode bitmapFrameDecode = bitmapDecoder.GetFrame(0); var metaReader = bitmapFrameDecode.MetadataQueryReader; if (metaReader == null) { IsSRgb = false; } else { var list = metaReader.QueryPaths; Dictionary <string, object> test = new Dictionary <string, object>(); foreach (var item in list) { test.Add(item, metaReader.GetMetadataByName(item)); } if (metaReader.TryGetMetadataByName("/sRGB/RenderingIntent", out object t).Success) { if ((ushort)t == 1) { IsSRgb = true; } } if (metaReader.TryGetMetadataByName("/app1/ifd/exif/{ushort=40961}", out t).Success) { if ((ushort)t == 1) { IsSRgb = true; } } IsSRgb = false; } if (IsSRgb) { System.Console.WriteLine(filename + " SRGB!"); } var formatConverter = new FormatConverter(Factory); formatConverter.Initialize( bitmapFrameDecode, PixelFormat.Format32bppPRGBA, BitmapDitherType.None, null, 0.0, BitmapPaletteType.Custom); return(formatConverter); }
static byte[] CreateRaw(IntPtr pixels, int width, int height) { using (var bitmap = new System.Drawing.Bitmap(width, height, width * 4, System.Drawing.Imaging.PixelFormat.Format32bppArgb, pixels)) { using (var stream = new MemoryStream()) { bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp); var factory = new ImagingFactory2(); using (var decoder = new BitmapDecoder(factory, stream, DecodeOptions.CacheOnDemand)) { using (var formatConverter = new FormatConverter(factory)) { formatConverter.Initialize(decoder.GetFrame(0), PixelFormat.Format32bppPRGBA); var stride = formatConverter.Size.Width * 4; using (var dataStream = new SharpDX.DataStream(formatConverter.Size.Height * stride, true, true)) { formatConverter.CopyPixels(stride, dataStream); byte[] b; using (BinaryReader br = new BinaryReader(dataStream)) { b = br.ReadBytes((int)dataStream.Length); } return(b); } } } } } }
/// <summary> /// Initializes a new instance of the <see cref="WicBitmapImpl"/> class. /// </summary> /// <param name="stream">The stream to read the bitmap from.</param> public WicBitmapImpl(Stream stream) { using (BitmapDecoder decoder = new BitmapDecoder(Direct2D1Platform.ImagingFactory, stream, DecodeOptions.CacheOnLoad)) { WicImpl = new Bitmap(Direct2D1Platform.ImagingFactory, decoder.GetFrame(0), BitmapCreateCacheOption.CacheOnLoad); } }
/// <summary> /// Called when a game resource is loaded. /// </summary> /// <param name="sender">The sender.</param> /// <param name="e">The <see cref="Platformer.Engine.Resources.ResourceLoadEventArgs" /> instance containing the event data.</param> protected override void OnGameResourceLoaded(Object sender, ResourceLoadEventArgs e) { if (e.ResourceType == typeof(Map)) { var map = e.LoadedResource as Map; foreach (var tileSet in map.TileSets) { using (var decoder = new BitmapDecoder(_imagingFactory, tileSet.ImagePath, DecodeOptions.CacheOnDemand)) { using (var formatConverter = new FormatConverter(_imagingFactory)) { formatConverter.Initialize( decoder.GetFrame(0), PixelFormat.Format32bppPBGRA, BitmapDitherType.DualSpiral8x8, null, 0.0, BitmapPaletteType.Custom); var bitmap = SharpDX.Direct2D1.Bitmap1.FromWicBitmap(_d2dContext, formatConverter); _tileSetTextures.Add(tileSet.Id, bitmap); } } } } }
public BitmapFrameDecoder(Func <Device, int, int, int, Format, Usage, Texture> textureFactory, MemoryPool memoryPool, Stream stream) : base(textureFactory, memoryPool) { var dataStream = stream as SharpDX.DataStream; using (var wicStream = new WICStream(Factory, new SharpDX.DataPointer(dataStream.DataPointer, (int)dataStream.Length))) using (var decoder = new BitmapDecoder(Factory, wicStream, DecodeOptions.CacheOnLoad)) { using (var frame = decoder.GetFrame(0)) { var dstPixelFormat = PixelFormat.Format32bppBGRA; Width = frame.Size.Width; Height = frame.Size.Height; FStride = PixelFormat.GetStride(dstPixelFormat, Width); FLength = FStride * Height; FBuffer = memoryPool.UnmanagedPool.GetMemory(FLength); if (frame.PixelFormat != dstPixelFormat) { using (var converter = new FormatConverter(Factory)) { converter.Initialize(frame, dstPixelFormat); converter.CopyPixels(FStride, FBuffer); } } else { frame.CopyPixels(FStride, FBuffer); } } } memoryPool.StreamPool.PutStream(stream); }
/// <summary> /// Initializes a new instance of the <see cref="WicBitmapImpl"/> class. /// </summary> /// <param name="fileName">The filename of the bitmap to load.</param> public WicBitmapImpl(string fileName) { using (BitmapDecoder decoder = new BitmapDecoder(Direct2D1Platform.ImagingFactory, fileName, DecodeOptions.CacheOnDemand)) { WicImpl = new Bitmap(Direct2D1Platform.ImagingFactory, decoder.GetFrame(0), BitmapCreateCacheOption.CacheOnDemand); } }
private static SharpDX.Direct2D1.Bitmap LoadBitmapFromContentFile(ImagingFactory2 imagingFactory, string filePath, RenderTarget renderTarget) { SharpDX.Direct2D1.Bitmap newBitmap; // Neccessary for creating WIC objects. //NativeFileStream fileStream = new NativeFileStream(filePath, // NativeFileMode.Open, NativeFileAccess.Read); // Used to read the image source file. BitmapDecoder bitmapDecoder = new BitmapDecoder(imagingFactory, filePath, DecodeOptions.CacheOnDemand); // Get the first frame of the image. BitmapFrameDecode frame = bitmapDecoder.GetFrame(0); // Convert it to a compatible pixel format. FormatConverter converter = new FormatConverter(imagingFactory); converter.Initialize(frame, SharpDX.WIC.PixelFormat.Format32bppPRGBA); // Create the new Bitmap1 directly from the FormatConverter. newBitmap = SharpDX.Direct2D1.Bitmap1.FromWicBitmap(renderTarget, converter); //Utilities.Dispose(ref bitmapDecoder); //Utilities.Dispose(ref fileStream); //Utilities.Dispose(ref imagingFactory); return(newBitmap); }
/// <summary> /// Loads the specified image(s). /// </summary> /// <param name="imagingFactory"> /// The factory for creating components for the Windows Imaging Component (WIC). /// </param> /// <param name="stream">The stream to read from.</param> /// <param name="flags">Additional options.</param> /// <returns>A <see cref="Texture"/> representing the image(s).</returns> /// <exception cref="ArgumentNullException"> /// <paramref name="imagingFactory"/>, <paramref name="stream"/> is <see langword="null"/>. /// </exception> public static Texture Load(ImagingFactory imagingFactory, Stream stream, WicFlags flags) { if (imagingFactory == null) { throw new ArgumentNullException("imagingFactory"); } if (stream == null) { throw new ArgumentNullException("stream"); } // Simple version: /* * using (var bitmapDecoder = new BitmapDecoder(_imagingFactory, stream, DecodeOptions.CacheOnDemand)) * { * // Convert the image to pre-multiplied RGBA8. * using (var formatConverter = new FormatConverter(_imagingFactory)) * { * formatConverter.Initialize(bitmapDecoder.GetFrame(0), PixelFormat.Format32bppPRGBA, BitmapDitherType.None, null, 0, BitmapPaletteType.Custom); * * // Return a API-independent texture. * var description = new TextureDescription * { * Dimension = TextureDimension.Texture2D, * Width = formatConverter.Size.Width, * Height = formatConverter.Size.Height, * Depth = 1, * MipLevels = 1, * ArraySize = 1, * Format = DataFormat.R8G8B8A8_UNorm * }; * * var texture = new Texture(description); * var image = texture.Images[0]; * formatConverter.CopyPixels(image.Data, image.RowPitch); * * return texture; * } * } * //*/ // DirectXTex version: using (var decoder = new BitmapDecoder(imagingFactory, stream, DecodeOptions.CacheOnDemand)) { var frame = decoder.GetFrame(0); // Get metadata. Guid convertGuid; var description = DecodeMetadata(imagingFactory, flags, decoder, frame, out convertGuid); if (description.ArraySize > 1 && (flags & WicFlags.AllFrames) != 0) { return(DecodeMultiframe(imagingFactory, flags, description, decoder)); } return(DecodeSingleframe(imagingFactory, flags, description, convertGuid, frame)); } }
/// <summary> /// Initializes a new instance of the <see cref="WicBitmapImpl"/> class. /// </summary> /// <param name="factory">The WIC imaging factory to use.</param> /// <param name="fileName">The filename of the bitmap to load.</param> public WicBitmapImpl(ImagingFactory factory, string fileName) : base(factory) { using (BitmapDecoder decoder = new BitmapDecoder(factory, fileName, DecodeOptions.CacheOnDemand)) { WicImpl = new Bitmap(factory, decoder.GetFrame(0), BitmapCreateCacheOption.CacheOnDemand); } }
public BitmapFrameDecode LoadBitmapFromFile(string fileName) { BitmapDecoder decoder = new BitmapDecoder(_imagingFactory, fileName, DecodeOptions.CacheOnDemand); BitmapFrameDecode frame = decoder.GetFrame(0); Utilities.Dispose(ref decoder); return(frame); }
public BitmapSource LoadBitmap(ImagingFactory2 factory, string filename) { var bitmapDecoder = new BitmapDecoder(factory, filename, DecodeOptions.CacheOnDemand); var formatConverter = new FormatConverter(factory); formatConverter.Initialize(bitmapDecoder.GetFrame(0), PixelFormat.Format32bppPRGBA, BitmapDitherType.None, null, 0.0, BitmapPaletteType.Custom); return(formatConverter); }
/// <summary> /// Initializes a new instance of the <see cref="WicBitmapImpl"/> class. /// </summary> /// <param name="factory">The WIC imaging factory to use.</param> /// <param name="stream">The stream to read the bitmap from.</param> public WicBitmapImpl(ImagingFactory factory, Stream stream) : base(factory) { using (BitmapDecoder decoder = new BitmapDecoder(factory, stream, DecodeOptions.CacheOnLoad)) { WicImpl = new Bitmap(factory, decoder.GetFrame(0), BitmapCreateCacheOption.CacheOnLoad); } }
private static Texture DecodeMultiframe(ImagingFactory imagingFactory, WicFlags flags, TextureDescription description, BitmapDecoder decoder) { var texture = new Texture(description); Guid dstFormat = ToWic(description.Format, false); for (int index = 0; index < description.ArraySize; ++index) { var image = texture.Images[index]; using (var frame = decoder.GetFrame(index)) { var pfGuid = frame.PixelFormat; var size = frame.Size; if (size.Width == description.Width && size.Height == description.Height) { // This frame does not need resized if (pfGuid == dstFormat) { frame.CopyPixels(image.Data, image.RowPitch); } else { using (var converter = new FormatConverter(imagingFactory)) { converter.Initialize(frame, dstFormat, GetWicDither(flags), null, 0, BitmapPaletteType.Custom); converter.CopyPixels(image.Data, image.RowPitch); } } } else { // This frame needs resizing using (var scaler = new BitmapScaler(imagingFactory)) { scaler.Initialize(frame, description.Width, description.Height, GetWicInterp(flags)); Guid pfScaler = scaler.PixelFormat; if (pfScaler == dstFormat) { scaler.CopyPixels(image.Data, image.RowPitch); } else { // The WIC bitmap scaler is free to return a different pixel format than the source image, so here we // convert it to our desired format using (var converter = new FormatConverter(imagingFactory)) { converter.Initialize(scaler, dstFormat, GetWicDither(flags), null, 0, BitmapPaletteType.Custom); converter.CopyPixels(image.Data, image.RowPitch); } } } } } } return texture; }
private static BitmapSource GetBitmapSource(byte[] data) { MemoryStream memStream = new MemoryStream(data); BitmapDecoder decoder = new BitmapDecoder(ImagingFactory, memStream, DecodeOptions.CacheOnDemand); FormatConverter formatConverter = new FormatConverter(ImagingFactory); formatConverter.Initialize(decoder.GetFrame(0), PixelFormat.Format32bppPRGBA, BitmapDitherType.None, null, 0.0, BitmapPaletteType.Custom); return(formatConverter); }
Texture2D LoadFromFile(Device device, ImagingFactory factory, MemoryStream ms) { var bitmapDecoder = new BitmapDecoder(factory, ms, DecodeOptions.CacheOnLoad); var bs = new FormatConverter(factory); bs.Initialize(bitmapDecoder.GetFrame(0), PixelFormat.Format32bppPRGBA, BitmapDitherType.None, null, 0.0, BitmapPaletteType.Custom); return(CreateTexture2DFromBitmap(device, bs)); }
/// <summary> /// Initializes a new instance of the <see cref="WicBitmapImpl"/> class. /// </summary> /// <param name="fileName">The filename of the bitmap to load.</param> public WicBitmapImpl(string fileName) { using (var decoder = new BitmapDecoder(Direct2D1Platform.ImagingFactory, fileName, DecodeOptions.CacheOnDemand)) using (var frame = decoder.GetFrame(0)) { WicImpl = new Bitmap(Direct2D1Platform.ImagingFactory, frame, BitmapCreateCacheOption.CacheOnDemand); Dpi = new Vector(96, 96); } }
private BitmapSource LoadBitmap(ImagingFactory factory, string fileName) { var bitmapDecoder = new BitmapDecoder(factory, fileName, DecodeOptions.CacheOnDemand); var result = new FormatConverter(factory); result.Initialize(bitmapDecoder.GetFrame(0), PixelFormat.Format32bppPRGBA, BitmapDitherType.None, null, 0.0D, BitmapPaletteType.Custom); return(result); }
public static BitmapSource LoadBitmapSourceFromFile(string fileName) { using (var factory = new ImagingFactory()) { using (var decoder = new BitmapDecoder(factory, fileName, DecodeOptions.CacheOnDemand)) { var converter = new FormatConverter(factory); converter.Initialize(decoder.GetFrame(0), PixelFormat.Format32bppPBGRA, BitmapDitherType.None, null, 0, BitmapPaletteType.Custom); return(converter); } } }
public static BitmapSource LoadBitmapSourceFromFile(ImagingFactory factory, string filename) { using (var bitmapDecoder = new BitmapDecoder(factory, filename, DecodeOptions.CacheOnDemand)) { var result = new FormatConverter(factory); using (var bitmapFrameDecode = bitmapDecoder.GetFrame(0)) { result.Initialize(bitmapFrameDecode, PixelFormat.Format32bppPRGBA, BitmapDitherType.None, null, 0, BitmapPaletteType.Custom); } return(result); } }
public IBitmapImage LoadBitmap(string FileName) { using var decoder = new BitmapDecoder(_editorSession.ImagingFactory, FileName, 0); using var bmpSource = decoder.GetFrame(0); using var convertedBmp = new FormatConverter(_editorSession.ImagingFactory); convertedBmp.Initialize(bmpSource, PixelFormat.Format32bppPBGRA); var bmp = Bitmap.FromWicBitmap(_editorSession.RenderTarget, convertedBmp); return(new Direct2DImage(bmp)); }
private static D2DBitmap CreateBitmapFromDecoder(RenderTarget renderTarget, ImagingFactory wicFactory, BitmapDecoder decoder) { BitmapFrameDecode source; FormatConverter converter; // Create the initial frame. source = decoder.GetFrame(0); // Convert the image format to 32bppPBGRA -- which Direct2D expects. converter = wicFactory.CreateFormatConverter(); converter.Initialize( source.ToBitmapSource(), PixelFormats.Pbgra32Bpp, BitmapDitherType.None, BitmapPaletteType.MedianCut ); // Create a Direct2D bitmap from the WIC bitmap. return renderTarget.CreateBitmapFromWicBitmap( converter.ToBitmapSource()); }
/// <summary> /// Gets bitmap. /// </summary> /// <param name="image">The image.</param> /// <returns>Returns bitmap.</returns> private Bitmap GetBitmap(OxyImage image) { if (image == null) { return null; } if (!this.imagesInUse.Contains(image)) { this.imagesInUse.Add(image); } Bitmap res; using (var stream = new MemoryStream(image.GetData())) { var decoder = new BitmapDecoder(this.wicFactory, stream, DecodeOptions.CacheOnDemand); var frame = decoder.GetFrame(0); var converter = new FormatConverter(this.wicFactory); converter.Initialize(frame, dx.WIC.PixelFormat.Format32bppPRGBA); res = Bitmap.FromWicBitmap(this.renderTarget, converter); } this.imageCache.Add(image, res); return res; }
private static ShaderResourceView LoadFromDecoder(D3DDevice device, ImagingFactory factory, BitmapDecoder bitmapDecoder) { if (bitmapDecoder.FrameCount == 0) throw new ArgumentException("Image file successfully loaded, but it has no image frames."); BitmapFrameDecode bitmapFrameDecode = bitmapDecoder.GetFrame(0); BitmapSource bitmapSource = bitmapFrameDecode.ToBitmapSource(); // create texture description Texture2DDescription textureDescription = new Texture2DDescription() { Width = bitmapSource.Size.Width, Height = bitmapSource.Size.Height, MipLevels = 1, ArraySize = 1, Format = Format.R8G8B8A8_UNORM, SampleDescription = new SampleDescription() { Count = 1, Quality = 0, }, Usage = Usage.Dynamic, BindFlags = BindFlag.ShaderResource, CpuAccessFlags = CpuAccessFlag.Write, MiscFlags = 0 }; // create texture Texture2D texture = device.CreateTexture2D(textureDescription); // Create a format converter WICFormatConverter converter = factory.CreateFormatConverter(); converter.Initialize( bitmapSource, PixelFormats.Pf32bppRGBA, BitmapDitherType.None, BitmapPaletteType.Custom); // get bitmap data byte[] buffer = converter.CopyPixels(); // Copy bitmap data to texture MappedTexture2D texmap = texture.Map(0, Map.WriteDiscard, MapFlag.Unspecified); Marshal.Copy(buffer, 0, texmap.Data, buffer.Length); texture.Unmap(0); // create shader resource view description ShaderResourceViewDescription srvDescription = new ShaderResourceViewDescription() { Format = textureDescription.Format, ViewDimension = ShaderResourceViewDimension.Texture2D, Texture2D = new Texture2DShaderResourceView() { MipLevels = textureDescription.MipLevels, MostDetailedMip = 0 } }; // create shader resource view from texture return device.CreateShaderResourceView(texture, srvDescription); }