public static void WatermarkText(Stream imageStream, Stream outputStream, string watermark, string font = "Times New Roman", float fontSize = 30.0f, int colorARGB = TransparentWhite) { using (var wic = new WIC.ImagingFactory2()) using (var d2d = new D2D.Factory()) using (var image = CreateWicImage(wic, imageStream)) using (var wicBitmap = new WIC.Bitmap(wic, image.Size.Width, image.Size.Height, WIC.PixelFormat.Format32bppPBGRA, WIC.BitmapCreateCacheOption.CacheOnDemand)) using (var target = new D2D.WicRenderTarget(d2d, wicBitmap, new D2D.RenderTargetProperties())) using (var bmpPicture = D2D.Bitmap.FromWicBitmap(target, image)) using (var dwriteFactory = new DWrite.Factory()) using (var brush = new D2D.SolidColorBrush(target, new Color(colorARGB))) { target.BeginDraw(); { target.DrawBitmap(bmpPicture, new RectangleF(0, 0, target.Size.Width, target.Size.Height), 1.0f, D2D.BitmapInterpolationMode.Linear); target.DrawRectangle(new RectangleF(0, 0, target.Size.Width, target.Size.Height), brush); var textFormat = new DWrite.TextFormat(dwriteFactory, font, DWrite.FontWeight.Bold, DWrite.FontStyle.Normal, fontSize) { ParagraphAlignment = DWrite.ParagraphAlignment.Far, TextAlignment = DWrite.TextAlignment.Trailing, }; target.DrawText(watermark, textFormat, new RectangleF(0, 0, target.Size.Width, target.Size.Height), brush); } target.EndDraw(); SaveD2DBitmap(wic, wicBitmap, outputStream); } }
private static void DrawSmallRaw(Person person, WIC.ImagingFactory wic, D2D.Factory d2dFactory, WIC.FormatConverter converter) { var whRate = 1.0f * converter.Size.Width / converter.Size.Height; var smallRawSize = new Vector2(whRate * ImageDefines.SmallRawY, ImageDefines.SmallRawY); var scale = ImageDefines.SmallRawY / converter.Size.Height; using (var wicBitmap = new WIC.Bitmap(wic, (int)smallRawSize.X, (int)smallRawSize.Y, WIC.PixelFormat.Format32bppPBGRA, WIC.BitmapCreateCacheOption.CacheOnDemand)) using (var target = new D2D.WicRenderTarget(d2dFactory, wicBitmap, new D2D.RenderTargetProperties())) using (var bmp = D2D.Bitmap.FromWicBitmap(target, converter)) using (var bmpBrush = new D2D.BitmapBrush(target, bmp)) { target.BeginDraw(); target.Transform = Matrix3x2.Scaling(scale, scale); target.DrawBitmap(bmp, 1.0f, D2D.BitmapInterpolationMode.Linear); target.EndDraw(); using (var file = File.Create(person.SmallRawImage)) { WicTools.SaveD2DBitmap(wic, wicBitmap, file); } } }
public static void EncodeImage(this d2.Bitmap target, ImageFormat imageFormat, Stream outputStream) { var width = target.PixelSize.Width; var height = target.PixelSize.Height; var wicBitmap = new wic.Bitmap(DXGraphicsService.FactoryImaging, width, height, wic.PixelFormat.Format32bppBGR, wic.BitmapCreateCacheOption.CacheOnLoad); var renderTargetProperties = new d2.RenderTargetProperties(d2.RenderTargetType.Default, new d2.PixelFormat(Format.Unknown, d2.AlphaMode.Unknown), 0, 0, d2.RenderTargetUsage.None, d2.FeatureLevel.Level_DEFAULT); var d2DRenderTarget = new d2.WicRenderTarget(target.Factory, wicBitmap, renderTargetProperties); d2DRenderTarget.BeginDraw(); d2DRenderTarget.Clear(global::SharpDX.Color.Transparent); d2DRenderTarget.DrawBitmap(target, 1, d2.BitmapInterpolationMode.Linear); d2DRenderTarget.EndDraw(); var stream = new wic.WICStream(DXGraphicsService.FactoryImaging, outputStream); // Initialize a Jpeg encoder with this stream var encoder = new wic.BitmapEncoder(DXGraphicsService.FactoryImaging, GetImageFormat(imageFormat)); encoder.Initialize(stream); // Create a Frame encoder var bitmapFrameEncode = new wic.BitmapFrameEncode(encoder); bitmapFrameEncode.Initialize(); bitmapFrameEncode.SetSize(width, height); Guid pixelFormatGuid = wic.PixelFormat.FormatDontCare; bitmapFrameEncode.SetPixelFormat(ref pixelFormatGuid); bitmapFrameEncode.WriteSource(wicBitmap); bitmapFrameEncode.Commit(); encoder.Commit(); bitmapFrameEncode.Dispose(); encoder.Dispose(); stream.Dispose(); d2DRenderTarget.Dispose(); wicBitmap.Dispose(); }
public static string GenerateWallpaper(string pictureFileName, string english, string chinese, string weather) { var wic = new WIC.ImagingFactory2(); var d2d = new D2D.Factory(); float dpi = d2d.DesktopDpi.Width; Size2 size = new Size2(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height); WIC.FormatConverter image = CreateWicImage(wic, pictureFileName); using (var wicBitmap = new WIC.Bitmap(wic, size.Width, size.Height, WIC.PixelFormat.Format32bppPBGRA, WIC.BitmapCreateCacheOption.CacheOnDemand)) using (var target = new D2D.WicRenderTarget(d2d, wicBitmap, new D2D.RenderTargetProperties())) using (var dc = target.QueryInterface <D2D.DeviceContext>()) using (var bmpPicture = Bitmap.FromWicBitmap(target, image)) using (var dwriteFactory = new SharpDX.DirectWrite.Factory()) using (var brush = new SolidColorBrush(target, new RawColor4(255, 255, 0, 1))) using (var bmpLayer = new D2D.Bitmap1(dc, target.PixelSize, new D2D.BitmapProperties1(new D2D.PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied), dpi, dpi, BitmapOptions.Target))) { var oldTarget = dc.Target; dc.Target = bmpLayer; target.BeginDraw(); { var textFormat = new DWrite.TextFormat(dwriteFactory, "Tahoma", size.Height / 27); // draw English { var textLayout = new DWrite.TextLayout(dwriteFactory, english, textFormat, target.Size.Width * 0.75f, float.MaxValue); var center = new Vector2((target.Size.Width - textLayout.Metrics.Width) / 2, (target.Size.Height - textLayout.Metrics.Height) / 2); target.DrawTextLayout(new RawVector2(center.X, center.Y), textLayout, brush); } { var textLayout = new DWrite.TextLayout(dwriteFactory, chinese, textFormat, target.Size.Width * 0.75f, float.MaxValue); var center = new Vector2((target.Size.Width - textLayout.Metrics.Width) / 2, target.Size.Height - textLayout.Metrics.Height - size.Height / 18); target.DrawTextLayout(new RawVector2(center.X, center.Y), textLayout, brush); } { var textLayout = new DWrite.TextLayout(dwriteFactory, weather, textFormat, target.Size.Width * 0.75f, float.MaxValue); var center = new Vector2((target.Size.Width - textLayout.Metrics.Width) / 2, size.Height / 18 + textLayout.Metrics.Height); target.DrawTextLayout(new RawVector2(center.X, center.Y), textLayout, brush); } } target.EndDraw(); // shadow var shadow = new D2D.Effects.Shadow(dc); shadow.SetInput(0, bmpLayer, new RawBool(false)); dc.Target = oldTarget; target.BeginDraw(); { target.DrawBitmap(bmpPicture, new SharpDX.Mathematics.Interop.RawRectangleF(0, 0, target.Size.Width, target.Size.Height), 1.0f, BitmapInterpolationMode.Linear); dc.DrawImage(shadow, new RawVector2(size.Height / 150.0f, size.Height / 150.0f)); dc.UnitMode = UnitMode.Pixels; target.DrawBitmap(bmpLayer, 1.0f, BitmapInterpolationMode.Linear); } target.EndDraw(); string wallpaperFileName = Path.GetTempPath() + "wallpaper.png"; using (var wallpaperStream = File.OpenWrite(wallpaperFileName)) { SaveD2DBitmap(wic, wicBitmap, wallpaperStream); wallpaperStream.Close(); return(wallpaperFileName); } } }
/// <summary> /// 制作背景图片 /// </summary> /// <param name="pictureFileFullPath"></param> /// <param name="fileDirectory"></param> /// <param name="textContent"></param> /// <returns></returns> static string GenerateWallPaper(string pictureFileFullPath, string fileDirectory, string textContent) { var wic = new SharpDX.WIC.ImagingFactory2(); var d2d = new SharpDX.Direct2D1.Factory(); float dpi = d2d.DesktopDpi.Width; Size2 size = new Size2(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height); SharpDX.WIC.FormatConverter image = CreateWicImage(wic, pictureFileFullPath); using var wicBitmap = new SharpDX.WIC.Bitmap(wic, size.Width, size.Height, SharpDX.WIC.PixelFormat.Format32bppPBGRA, SharpDX.WIC.BitmapCreateCacheOption.CacheOnDemand); using var target = new SharpDX.Direct2D1.WicRenderTarget(d2d, wicBitmap, new SharpDX.Direct2D1.RenderTargetProperties()); using var dc = target.QueryInterface <SharpDX.Direct2D1.DeviceContext>(); using var bmpPicture = SharpDX.Direct2D1.Bitmap.FromWicBitmap(target, image); using var dwriteFactory = new SharpDX.DirectWrite.Factory(); using var brush = new SolidColorBrush(target, SharpDX.Color.LightGoldenrodYellow); using var bmpLayer = new SharpDX.Direct2D1.Bitmap1(dc, target.PixelSize, new SharpDX.Direct2D1.BitmapProperties1(new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied), dpi, dpi, SharpDX.Direct2D1.BitmapOptions.Target)); var oldTarget = dc.Target; dc.Target = bmpLayer; target.BeginDraw(); { var textFormat = new SharpDX.DirectWrite.TextFormat(dwriteFactory, "Tahoma", size.Height / 27); // draw textContent { var textLayout = new SharpDX.DirectWrite.TextLayout(dwriteFactory, textContent, textFormat, target.Size.Width * 0.75f, float.MaxValue); var center = new Vector2((target.Size.Width - textLayout.Metrics.Width) / 2, (target.Size.Height - textLayout.Metrics.Height) / 2); target.DrawTextLayout(new Vector2(center.X, center.Y), textLayout, brush); } //{ // // draw otherContent // var textLayout = new SharpDX.DirectWrite.TextLayout(dwriteFactory, chinese, textFormat, target.Size.Width * 0.75f, float.MaxValue); // var center = new Vector2((target.Size.Width - textLayout.Metrics.Width) / 2, target.Size.Height - textLayout.Metrics.Height - size.Height / 18); // target.DrawTextLayout(new Vector2(center.X, center.Y), textLayout, brush); //} } target.EndDraw(); // shadow var shadow = new SharpDX.Direct2D1.Effects.Shadow(dc); shadow.SetInput(0, bmpLayer, new RawBool(false)); dc.Target = oldTarget; target.BeginDraw(); { target.DrawBitmap(bmpPicture, new SharpDX.RectangleF(0, 0, target.Size.Width, target.Size.Height), 1.0f, BitmapInterpolationMode.Linear); dc.DrawImage(shadow, new Vector2(size.Height / 150.0f, size.Height / 150.0f)); dc.UnitMode = UnitMode.Pixels; target.DrawBitmap(bmpLayer, 1.0f, BitmapInterpolationMode.Linear); } target.EndDraw(); if (!Directory.Exists(fileDirectory)) { Directory.CreateDirectory(fileDirectory); } string wallpaperFileName = fileDirectory + DateTime.Now.ToString("yyyyMMdd") + "_" + "wallpaper.png"; using var wallpaperStream = File.OpenWrite(wallpaperFileName); SaveD2DBitmap(wic, wicBitmap, wallpaperStream); wallpaperStream.Close(); return(wallpaperFileName); }