public static Guid ToWICImageFormat(this Direct2DImageFormat format) { switch (format) { case Direct2DImageFormat.Bmp: return(ContainerFormatGuids.Bmp); case Direct2DImageFormat.Ico: return(ContainerFormatGuids.Ico); case Direct2DImageFormat.Gif: return(ContainerFormatGuids.Gif); case Direct2DImageFormat.Jpeg: return(ContainerFormatGuids.Jpeg); case Direct2DImageFormat.Png: return(ContainerFormatGuids.Png); case Direct2DImageFormat.Tiff: return(ContainerFormatGuids.Tiff); case Direct2DImageFormat.Wmp: return(ContainerFormatGuids.Wmp); } throw new NotSupportedException(); }
/// <summary> /// Saves the bitmap. /// </summary> /// <param name="view">The view.</param> /// <param name="fileName">Name of the file.</param> public static void SaveScreen(this Viewport3DX view, string fileName) { var ext = System.IO.Path.GetExtension(fileName); Direct2DImageFormat format = Direct2DImageFormat.Bmp; switch (ext) { case "bmp": format = Direct2DImageFormat.Bmp; break; case "jpeg": case "jpg": format = Direct2DImageFormat.Jpeg; break; case "png": format = Direct2DImageFormat.Png; break; default: break; } SaveScreen(view, fileName, format); }
public static MemoryStream ToMemoryStream(this global::SharpDX.WIC.Bitmap bitmap, IDevice2DResources deviceResources, Direct2DImageFormat imageType = Direct2DImageFormat.Bmp) { if (bitmap == null) { return(null); } var systemStream = new MemoryStream(); using (var stream = new WICStream(deviceResources.WICImgFactory, systemStream)) { using (var encoder = new BitmapEncoder(deviceResources.WICImgFactory, imageType.ToWICImageFormat())) { encoder.Initialize(stream); using (var frameEncoder = new BitmapFrameEncode(encoder)) { frameEncoder.Initialize(); frameEncoder.SetSize(bitmap.Size.Width, bitmap.Size.Height); frameEncoder.WriteSource(bitmap); frameEncoder.Commit(); encoder.Commit(); return(systemStream); } } } }
public void Save(Stream systemStream, Direct2DImageFormat format) { renderTarget.EndDraw(); var stream = new WICStream(factoryManager.WicFactory, systemStream); var encoder = new BitmapEncoder(factoryManager.WicFactory, Direct2DConverter.ConvertImageFormat(format)); encoder.Initialize(stream); var bitmapFrameEncode = new BitmapFrameEncode(encoder); bitmapFrameEncode.Initialize(); bitmapFrameEncode.SetSize(imageWidth, imageHeight); Guid fdc = SharpDX.WIC.PixelFormat.FormatDontCare; //fdc = Direct2DConverter.ConvertImageFormat(Direct2DImageFormat.Gif); bitmapFrameEncode.SetPixelFormat(ref fdc); bitmapFrameEncode.WriteSource(wicBitmap); bitmapFrameEncode.Commit(); try { encoder.Commit(); }catch(Exception ex){ var f = ex.Message; } bitmapFrameEncode.Dispose(); encoder.Dispose(); stream.Dispose(); }
public void Save(Stream systemStream, Direct2DImageFormat format, string text, string faceName, float fontSize, out int width, out int height) { #if BENCHMARK using (var handler = Benchmark.Instance.Start("DirectWrite", "Save")) #endif using (var layout = new TextLayout(factoryManager.DwFactory, text, new TextFormat(factoryManager.DwFactory, faceName, fontSize * 1.3f), 4000, 4000)) { width = (int)Math.Ceiling(layout.Metrics.WidthIncludingTrailingWhitespace); height = (int)Math.Ceiling(layout.Metrics.Height); using (var wicBitmap = new SharpDX.WIC.Bitmap(factoryManager.WicFactory, width, height, SharpDX.WIC.PixelFormat.Format32bppPRGBA, BitmapCreateCacheOption.CacheOnLoad)) { var renderTargetProperties = new RenderTargetProperties(RenderTargetType.Default, new SharpDX.Direct2D1.PixelFormat(Format.R8G8B8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Unknown), imageDpi, imageDpi, RenderTargetUsage.None, FeatureLevel.Level_DEFAULT); using (var renderTarget = new WicRenderTarget(factoryManager.D2DFactory, wicBitmap, renderTargetProperties)) using (var brush = new SolidColorBrush(renderTarget, SharpDX.Color.White)) using (var encoder = new BitmapEncoder(factoryManager.WicFactory, Direct2DConverter.ConvertImageFormat(format))) { renderTarget.BeginDraw(); renderTarget.Clear(new Color4(1, 1, 1, 0)); renderTarget.DrawTextLayout(Vector2.Zero, layout, brush); renderTarget.EndDraw(); var stream = new WICStream(factoryManager.WicFactory, systemStream); encoder.Initialize(stream); using (var bitmapFrameEncode = new BitmapFrameEncode(encoder)) { bitmapFrameEncode.Initialize(); bitmapFrameEncode.SetSize(width, height); bitmapFrameEncode.WriteSource(wicBitmap); bitmapFrameEncode.Commit(); } encoder.Commit(); } } } }
public void Save(Stream systemStream, Direct2DImageFormat format) { renderTarget.EndDraw(); var stream = new WICStream(factoryManager.WicFactory, systemStream); var encoder = new BitmapEncoder(factoryManager.WicFactory, Direct2DConverter.ConvertImageFormat(format)); encoder.Initialize(stream); var bitmapFrameEncode = new BitmapFrameEncode(encoder); bitmapFrameEncode.Initialize(); bitmapFrameEncode.SetSize(imageWidth, imageHeight); Guid fdc = SharpDX.WIC.PixelFormat.FormatDontCare; //fdc = Direct2DConverter.ConvertImageFormat(Direct2DImageFormat.Gif); bitmapFrameEncode.SetPixelFormat(ref fdc); bitmapFrameEncode.WriteSource(wicBitmap); bitmapFrameEncode.Commit(); try { encoder.Commit(); }catch (Exception ex) { var f = ex.Message; } bitmapFrameEncode.Dispose(); encoder.Dispose(); stream.Dispose(); }
/// <summary> /// Saves the bitmap. /// </summary> /// <param name="view">The view.</param> /// <param name="fileName">Name of the file.</param> /// <param name="format">The format.</param> public static void SaveScreen(this Viewport3DX view, string fileName, Direct2DImageFormat format) { using (var file = System.IO.File.OpenWrite(fileName)) { if (!file.CanWrite) { throw new AccessViolationException($"File cannot be written. {fileName}"); } } if (view.RenderHost != null && view.RenderHost.IsRendering) { Utilities.ScreenCapture.SaveWICTextureToFile(view.RenderHost.EffectsManager, view.RenderHost.RenderBuffer.BackBuffer.Resource as Texture2D, fileName, format); } }
public static MemoryStream CreateRadiusGradientBitmapStream(IDevice2DResources deviceResources, int width, int height, Direct2DImageFormat imageType, Vector2 center, Vector2 gradientOriginOffset, float radiusX, float radiusY, GradientStop[] gradients, ExtendMode extendMode = ExtendMode.Clamp, Gamma gamma = Gamma.StandardRgb) { using (var bmp = CreateBitmapStream(deviceResources, width, height, imageType, (target) => { using (var gradientCol = new GradientStopCollection(target, gradients, gamma, extendMode)) { using (var brush = new RadialGradientBrush(target, new RadialGradientBrushProperties() { Center = center, GradientOriginOffset = gradientOriginOffset, RadiusX = radiusX, RadiusY = radiusY, }, gradientCol)) { target.FillRectangle(new RawRectangleF(0, 0, width, height), brush); } } })) { return(bmp.ToMemoryStream(deviceResources, imageType)); } }
public static MemoryStream CreateLinearGradientBitmapStream(IDevice2DResources deviceResources, int width, int height, Direct2DImageFormat imageType, Vector2 startPoint, Vector2 endPoint, GradientStop[] gradients, ExtendMode extendMode = ExtendMode.Clamp, Gamma gamma = Gamma.StandardRgb) { using (var bmp = CreateBitmapStream(deviceResources, width, height, imageType, (target) => { using (var gradientCol = new GradientStopCollection(target, gradients, gamma, extendMode)) { using (var brush = new LinearGradientBrush(target, new LinearGradientBrushProperties() { StartPoint = startPoint, EndPoint = endPoint }, gradientCol)) { target.FillRectangle(new RawRectangleF(0, 0, width, height), brush); } } })) { return(bmp.ToMemoryStream(deviceResources, imageType)); } }
public static MemoryStream CreateSolidColorBitmapStream(IDevice2DResources deviceResources, int width, int height, Direct2DImageFormat imageType, Color4 color) { using (var bmp = CreateBitmapStream(deviceResources, width, height, imageType, (target) => { using (var brush = new SolidColorBrush(target, color, new BrushProperties() { Opacity = color.Alpha })) { target.FillRectangle(new RawRectangleF(0, 0, width, height), brush); } })) { return(bmp.ToMemoryStream(deviceResources, imageType)); } }
public static global::SharpDX.WIC.Bitmap CreateBitmapStream(IDevice2DResources deviceResources, int width, int height, Direct2DImageFormat imageType, Action <RenderTarget> drawingAction) { if (width <= 0 || height <= 0) { return(null); } var bitmap = new global::SharpDX.WIC.Bitmap(deviceResources.WICImgFactory, width, height, global::SharpDX.WIC.PixelFormat.Format32bppBGR, BitmapCreateCacheOption.CacheOnDemand); using (var target = new WicRenderTarget(deviceResources.Factory2D, bitmap, new RenderTargetProperties() { DpiX = 96, DpiY = 96, MinLevel = FeatureLevel.Level_DEFAULT, PixelFormat = new global::SharpDX.Direct2D1.PixelFormat(global::SharpDX.DXGI.Format.Unknown, AlphaMode.Unknown) })) { target.Transform = Matrix3x2.Identity; target.BeginDraw(); drawingAction(target); target.EndDraw(); } return(bitmap); }
/// <summary> /// Saves the wic texture to file. /// </summary> /// <param name="deviceResource">The device resource.</param> /// <param name="source">The source.</param> /// <param name="file">The file.</param> /// <param name="format">The format.</param> /// <returns></returns> public static bool SaveWICTextureToFile(IDeviceResources deviceResource, Texture2D source, string file, Direct2DImageFormat format) { return(SaveWICTextureToFile(deviceResource, source, file, BitmapExtensions.ToWICImageFormat(format))); }
public static Guid ConvertImageFormat(Direct2DImageFormat format) { switch (format) { case Direct2DImageFormat.Bmp: return ContainerFormatGuids.Bmp; case Direct2DImageFormat.Ico: return ContainerFormatGuids.Ico; case Direct2DImageFormat.Gif: return ContainerFormatGuids.Gif; case Direct2DImageFormat.Jpeg: return ContainerFormatGuids.Jpeg; case Direct2DImageFormat.Png: return ContainerFormatGuids.Png; case Direct2DImageFormat.Tiff: return ContainerFormatGuids.Tiff; case Direct2DImageFormat.Wmp: return ContainerFormatGuids.Wmp; } throw new NotSupportedException(); }
public static MemoryStream CreateBitmapStream(IDevice2DResources deviceResources, int width, int height, Direct2DImageFormat imageType, Action <RenderTarget> drawingAction) { using (var bitmap = new global::SharpDX.WIC.Bitmap(deviceResources.WICImgFactory, (int)width, (int)height, global::SharpDX.WIC.PixelFormat.Format32bppBGR, BitmapCreateCacheOption.CacheOnDemand)) { using (var target = new WicRenderTarget(deviceResources.Factory2D, bitmap, new RenderTargetProperties() { DpiX = 96, DpiY = 96, MinLevel = FeatureLevel.Level_DEFAULT, PixelFormat = new global::SharpDX.Direct2D1.PixelFormat(global::SharpDX.DXGI.Format.Unknown, AlphaMode.Unknown) })) { target.Transform = Matrix3x2.Identity; target.BeginDraw(); drawingAction(target); target.EndDraw(); } var systemStream = new MemoryStream(); using (var stream = new WICStream(deviceResources.WICImgFactory, systemStream)) { using (var encoder = new BitmapEncoder(deviceResources.WICImgFactory, imageType.ToWICImageFormat())) { encoder.Initialize(stream); using (var frameEncoder = new BitmapFrameEncode(encoder)) { frameEncoder.Initialize(); frameEncoder.SetSize((int)width, (int)height); frameEncoder.WriteSource(bitmap); frameEncoder.Commit(); encoder.Commit(); return(systemStream); } } } } }