/// <summary> /// Saves this pixel buffer to a file. /// </summary> /// <param name="fileName">The destination file.</param> /// <param name="fileType">Specify the output format.</param> /// <remarks>This method support the following format: <c>dds, bmp, jpg, png, gif, tiff, wmp, tga</c>.</remarks> public void Save(string fileName, ImageFileType fileType) { using (var imageStream = new SDX.IO.NativeFileStream(fileName, SDX.IO.NativeFileMode.Create, SDX.IO.NativeFileAccess.Write)) { Save(imageStream, fileType); } }
/// <summary> /// This doesnt work! SharpDX errors when trying to open a local system file (not relative) /// </summary> /// <param name="assetNativeUri"></param> /// <param name="backgroundImageFormatConverter"></param> /// <param name="backgroundImageSize"></param> public void LoadNativeAsset(string assetNativeUri, out SharpDX.WIC.FormatConverter backgroundImageFormatConverter, out Size2 backgroundImageSize) { var path = Windows.ApplicationModel.Package.Current.InstalledLocation.Path; var nativeFileStream = new SharpDX.IO.NativeFileStream( assetNativeUri, SharpDX.IO.NativeFileMode.Open, SharpDX.IO.NativeFileAccess.Read, SharpDX.IO.NativeFileShare.Read); var r = SharpDX.IO.NativeFile.ReadAllBytes(assetNativeUri); var data = SharpDX.IO.NativeFile.ReadAllBytes(assetNativeUri); using (System.IO.MemoryStream ms = new System.IO.MemoryStream(data)) { if (ms != null) { using (SharpDX.WIC.BitmapDecoder bitmapDecoder = new SharpDX.WIC.BitmapDecoder( _deviceManager.WICFactory, ms, SharpDX.WIC.DecodeOptions.CacheOnDemand )) { using (SharpDX.WIC.BitmapFrameDecode bitmapFrameDecode = bitmapDecoder.GetFrame(0)) { using (SharpDX.WIC.BitmapSource bitmapSource = new SharpDX.WIC.BitmapSource(bitmapFrameDecode.NativePointer)) { SharpDX.WIC.FormatConverter formatConverter = new SharpDX.WIC.FormatConverter(_deviceManager.WICFactory); //formatConverter.Initialize( bitmapSource, SharpDX.WIC.PixelFormat.Format32bppBGRA); formatConverter.Initialize( bitmapSource, SharpDX.WIC.PixelFormat.Format32bppBGRA, SharpDX.WIC.BitmapDitherType.None, null, 0.0f, SharpDX.WIC.BitmapPaletteType.Custom ); backgroundImageSize = formatConverter.Size; backgroundImageFormatConverter = formatConverter; } } } } } backgroundImageFormatConverter = null; backgroundImageSize = new Size2(0, 0); }
public static byte[] LoadImage(string fileName, bool premultiplied, out int width, out int height) { WIC.ImagingFactory factory = new WIC.ImagingFactory(); SharpDX.IO.NativeFileStream stream = new SharpDX.IO.NativeFileStream(fileName, SharpDX.IO.NativeFileMode.Open, SharpDX.IO.NativeFileAccess.Read); WIC.BitmapDecoder decoder = new WIC.BitmapDecoder(factory, stream, WIC.DecodeOptions.CacheOnDemand); WIC.BitmapFrameDecode frame = decoder.GetFrame(0); width = frame.Size.Width; height = frame.Size.Height; byte[] data = new byte[width * height * 4]; frame.CopyPixels(data, width * 4); return(data); }
public override void OnRenderTargetChanged() { base.OnRenderTargetChanged(); if (RenderTarget == null || RenderTarget.IsDisposed) { return; } // Dispose all Render dependant resources on RenderTarget change. if (myBitmap != null) { myBitmap.Dispose(); } if (fileStream != null) { fileStream.Dispose(); } if (bitmapDecoder != null) { bitmapDecoder.Dispose(); } if (converter != null) { converter.Dispose(); } if (frame != null) { frame.Dispose(); } // Neccessary for creating WIC objects. fileStream = new SharpDX.IO.NativeFileStream(System.IO.Path.Combine(NinjaTrader.Core.Globals.UserDataDir, "SampleDrawBitmap.png"), SharpDX.IO.NativeFileMode.Open, SharpDX.IO.NativeFileAccess.Read); // Used to read the image source file. bitmapDecoder = new SharpDX.WIC.BitmapDecoder(Core.Globals.WicImagingFactory, fileStream, SharpDX.WIC.DecodeOptions.CacheOnDemand); // Get the first frame of the image. frame = bitmapDecoder.GetFrame(0); // Convert it to a compatible pixel format. converter = new SharpDX.WIC.FormatConverter(Core.Globals.WicImagingFactory); converter.Initialize(frame, SharpDX.WIC.PixelFormat.Format32bppPRGBA); // Create the new Bitmap1 directly from the FormatConverter. myBitmap = SharpDX.Direct2D1.Bitmap.FromWicBitmap(RenderTarget, converter); if (!HWSet && myBitmap != null) { H = myBitmap.Size.Height; W = myBitmap.Size.Width; Ratio = W / H; HWSet = true; } }
public static SharpDX.Direct2D1.Bitmap LoadFromFile(string filePath) { SharpDX.WIC.ImagingFactory imagingFactory = new SharpDX.WIC.ImagingFactory(); SharpDX.IO.NativeFileStream fileStream = new SharpDX.IO.NativeFileStream(filePath, SharpDX.IO.NativeFileMode.Open, SharpDX.IO.NativeFileAccess.Read); SharpDX.WIC.BitmapDecoder bitmapDecoder = new SharpDX.WIC.BitmapDecoder(imagingFactory, fileStream, SharpDX.WIC.DecodeOptions.CacheOnDemand); SharpDX.WIC.BitmapFrameDecode frame = bitmapDecoder.GetFrame(0); SharpDX.WIC.FormatConverter converter = new SharpDX.WIC.FormatConverter(imagingFactory); converter.Initialize(frame, SharpDX.WIC.PixelFormat.Format32bppPRGBA); return(SharpDX.Direct2D1.Bitmap.FromWicBitmap(RenderForm.RenderTarget, converter)); }
internal async static Task <IRandomAccessStream> OpenReadAsync(this StorageFile file, OperatingSystem os) { if (os == OperatingSystem.Windows) { return(await file.OpenReadAsync()); } else { var macFileStream = new SharpDX.IO.NativeFileStream( file.Path, SharpDX.IO.NativeFileMode.Open, SharpDX.IO.NativeFileAccess.Read, SharpDX.IO.NativeFileShare.Read); InMemoryRandomAccessStream macRandomAccessStream = new InMemoryRandomAccessStream(); await macFileStream.CopyToAsync(macRandomAccessStream.AsStreamForWrite()); macRandomAccessStream.Seek(0); return(macRandomAccessStream); } }
/// <summary> /// This doesnt work! SharpDX errors when trying to open a local system file (not relative) /// </summary> /// <param name="assetNativeUri"></param> /// <param name="backgroundImageFormatConverter"></param> /// <param name="backgroundImageSize"></param> public void LoadNativeAsset(string assetNativeUri, out SharpDX.WIC.FormatConverter backgroundImageFormatConverter, out Size2 backgroundImageSize) { var path = Windows.ApplicationModel.Package.Current.InstalledLocation.Path; var nativeFileStream = new SharpDX.IO.NativeFileStream( assetNativeUri, SharpDX.IO.NativeFileMode.Open, SharpDX.IO.NativeFileAccess.Read, SharpDX.IO.NativeFileShare.Read); var r = SharpDX.IO.NativeFile.ReadAllBytes(assetNativeUri); var data = SharpDX.IO.NativeFile.ReadAllBytes(assetNativeUri); using (System.IO.MemoryStream ms = new System.IO.MemoryStream(data)) { if (ms != null) { using (SharpDX.WIC.BitmapDecoder bitmapDecoder = new SharpDX.WIC.BitmapDecoder( _deviceManager.WICFactory, ms, SharpDX.WIC.DecodeOptions.CacheOnDemand )) { using (SharpDX.WIC.BitmapFrameDecode bitmapFrameDecode = bitmapDecoder.GetFrame(0)) { using (SharpDX.WIC.BitmapSource bitmapSource = new SharpDX.WIC.BitmapSource(bitmapFrameDecode.NativePointer)) { SharpDX.WIC.FormatConverter formatConverter = new SharpDX.WIC.FormatConverter(_deviceManager.WICFactory); //formatConverter.Initialize( bitmapSource, SharpDX.WIC.PixelFormat.Format32bppBGRA); formatConverter.Initialize( bitmapSource, SharpDX.WIC.PixelFormat.Format32bppBGRA, SharpDX.WIC.BitmapDitherType.None, null, 0.0f, SharpDX.WIC.BitmapPaletteType.Custom ); backgroundImageSize = formatConverter.Size; backgroundImageFormatConverter = formatConverter; } } } } } backgroundImageFormatConverter = null; backgroundImageSize = new Size2(0, 0); }