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()); }
public static D2DBitmap GetBitmap(BitmapData pBitmapData, RenderTarget pRenderer) { try { BitmapDecoder decoder = ImagingFactory.CreateDecoderFromFilename(pBitmapData.FilePath, DesiredAccess.Read, DecodeMetadataCacheOptions.OnDemand);//.CreateDecoderFromStream(pBitmapData.Data, 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); return(pRenderer.CreateBitmapFromWicBitmap(formatConverter.ToBitmapSource())); } catch (Exception) { return(null); } }
private static D2DBitmap CreateBitmapFromDecoder(RenderTarget renderTarget, ImagingFactory wicFactory, BitmapDecoder decoder) { // Create the initial frame. BitmapFrameDecode source = decoder.GetFrame(0); // Convert the image format to 32bppPBGRA -- which Direct2D expects. FormatConverter converter = wicFactory.CreateFormatConverter(); converter.Initialize( source.ToBitmapSource(), PixelFormats.Pbgra32Bpp, BitmapDitherType.None, BitmapPaletteType.MedianCut ); // Create a Direct2D bitmap from the WIC bitmap. D2DBitmap ret = renderTarget.CreateBitmapFromWicBitmap(converter.ToBitmapSource()); converter.Dispose(); source.Dispose(); return(ret); }
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)); }