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()); }
private Bitmap LoadBitmap(string path) { using (WicBitmapDecoder decoder = ImagingFactory.CreateDecoder(ImagePath, Guid.Empty, DesiredAccess.Read, DecodeOptions.MetadataCacheOnDemand)) { using (WicBitmapFrameDecode frame = decoder.GetFrame(0)) { using (WicFormatConverter converter = ImagingFactory.CreateFormatConverter()) { converter.Convert(frame, WicPixelFormats.PixelFormat32bppPBGRA, BitmapDitherType.None, null, 0, BitmapPaletteType.Custom); return(RenderTarget.CreateBitmap(converter, new BitmapProperties())); } } } }
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()); }
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())); }
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); }
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.R8G8B8A8UNorm, SampleDescription = new SampleDescription() { Count = 1, Quality = 0, }, Usage = Usage.Dynamic, BindingOptions = BindingOptions.ShaderResource, CpuAccessOptions = CpuAccessOptions.Write, MiscellaneousResourceOptions = MiscellaneousResourceOptions.None }; // create texture Texture2D texture = device.CreateTexture2D(textureDescription); // Create a format converter FormatConverter converter = factory.CreateFormatConverter(); converter.Initialize( bitmapSource, PixelFormats.Prgba32Bpp, BitmapDitherType.None, BitmapPaletteType.Custom); // get bitmap data byte[] buffer = converter.CopyPixels(); // Copy bitmap data to texture MappedTexture2D texmap = texture.Map(0, Map.WriteDiscard, Microsoft.WindowsAPICodePack.DirectX.Direct3D10.MapOptions.None); 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)); }