/// <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); }
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); } } }
public void DoRender(System.Drawing.Bitmap pic1, System.Drawing.Bitmap pic2, string addSong, bool IsPlaying) { try { InitSurfaceSize(); if (pic1 != null) { m_VideoBitmap = ConvertToSharpDXBitmap(m_D2DContext.d2dContext, pic1); pic1.Dispose(); int VideoW = (int)m_VideoBitmap.Size.Width; int VideoH = (int)m_VideoBitmap.Size.Height; if (SurfaceH2 > 0) { fontSize10 = (SurfaceH2) / 40; fontSize15 = (SurfaceH2) / 20; fontSize30 = (SurfaceH2) / 10; } else { //just guesing the font size fontSize10 = 10f; fontSize15 = 15f; fontSize30 = 30f; } LoadResources(); m_D2DContext.d2dContextCdgText.Target = m_Bitmap1; // associate bitmap with the d2d context m_D2DContext.d2dContextCdgText.BeginDraw(); m_D2DContext.d2dContextCdgText.Clear(Color.Transparent); m_D2DContext.d2dContextCdgText2.Target = m_Bitmap1FullScreen; // associate bitmap with the d2d context m_D2DContext.d2dContextCdgText2.BeginDraw(); m_D2DContext.d2dContextCdgText2.Clear(Color.Transparent); m_Shadow.SetInput(0, m_Bitmap1, true); // Create image shadow effect m_ShadowFullScreen.SetInput(0, m_Bitmap1FullScreen, true); m_ShadowFullScreen.Optimization = D2D1.ShadowOptimization.Quality; // Create image transform effect m_affineTransformEffect.SetInputEffect(0, m_ShadowFullScreen); m_affineTransformEffect.TransformMatrix = Matrix3x2.Translation(0, 0); // Create composite effect // m_compositeEffect.InputCount = 2; m_compositeEffect.SetInputCount(2); m_compositeEffect.SetInputEffect(0, m_ShadowFullScreen); m_compositeEffect.SetInputEffect(1, m_affineTransformEffect); m_compositeEffect.SetInput(2, m_Bitmap1FullScreen, true); if (pic2 != null) { m_CDGBitmap = ConvertToSharpDXBitmap(m_D2DContext.d2dContextCdgText, pic2); pic2.Dispose(); //float Height = (SurfaceH * 2) / 3f; //float Width = (SurfaceW * 2) / 3f; float Height2 = (SurfaceH2 * 2) / 3f; float Width2 = (SurfaceW2 * 2) / 3f; float CdgW = (m_CDGBitmap.Size.Width * 2) / 3f; float CdgH = (m_CDGBitmap.Size.Height * 2) / 3f; // float CdgWFullScreen = (m_CDGBitmap.Size.Width + SurfaceW2) / 2f; // float CdgHFullScreen = (m_CDGBitmap.Size.Height + SurfaceH2) / 2f; m_destCdgRectangle = new RawRectangleF((SurfaceR / 2) - (CdgW / 2), SurfaceH, (SurfaceR / 2) + (CdgW / 2), SurfaceB - CdgH); m_destCdgRectangle2 = new RawRectangleF(((SurfaceR2 / 2) - (Width2 / 2)), SurfaceH2, ((SurfaceR2 / 2) + (Width2 / 2)), SurfaceB2 - Height2); m_D2DContext.d2dContextCdgText.DrawBitmap(m_CDGBitmap, m_destCdgRectangle, 1.0f, D2D1.BitmapInterpolationMode.Linear); m_D2DContext.d2dContextCdgText2.DrawBitmap(m_CDGBitmap, m_destCdgRectangle2, 1.0f, D2D1.BitmapInterpolationMode.Linear); } m_videoBitmapRectangle = new RawRectangleF((SurfaceR / 2) - (VideoW / 6), 0, (SurfaceR / 2) + (VideoW / 6), SurfaceH); m_D2DContext.d2dContextCdgText.EndDraw(); m_D2DContext.d2dContextCdgText2.EndDraw(); m_D2DContext.d2dContext.BeginDraw(); m_D2DContext.d2dContext.Clear(Color.Transparent); m_D2DContext.d2dContext.DrawBitmap(m_VideoBitmap, m_videoBitmapRectangle, 1.0f, D2D1.BitmapInterpolationMode.Linear); if (!IsPlaying) { m_TitleName = DrawString(VideoW, VideoH, fontSize15, fontSize30); m_D2DContext.d2dContext.DrawBitmap(m_TitleName, new RawRectangleF((SurfaceR / 2) - (VideoW / 4), 0, (SurfaceR / 2) + (VideoW / 4), SurfaceH), 1.0f, D2D1.BitmapInterpolationMode.Linear); } if (PlayerControl.IsAddToReserve) { m_D2DContext.d2dContext.DrawRectangle(m_videoBitmapRectangle, roundedRecInColor, 10); } m_D2DContext.d2dContext.DrawImage(m_Shadow, new RawVector2(0, 0), D2D1.InterpolationMode.Linear, D2D1.CompositeMode.Xor); m_D2DContext.d2dContext.DrawBitmap(m_Bitmap1, 1f, D2D1.BitmapInterpolationMode.Linear); m_D2DContext.d2dContext.EndDraw(); m_D2DContext.swapChain.Present(0, DXGI.PresentFlags.None); m_D2DContext.d2dContext2.BeginDraw(); m_D2DContext.d2dContext2.Clear(Color.Transparent); m_D2DContext.d2dContext2.DrawBitmap(m_VideoBitmap, new RawRectangleF(SurfaceL2, SurfaceT2, SurfaceR2, SurfaceH2), 1.0f, D2D1.BitmapInterpolationMode.Linear); if (!IsPlaying) { //m_D2DContext.d2dContext2.DrawBitmap(m_TitleName, new RawRectangleF(SurfaceL2, SurfaceT2, SurfaceR2, SurfaceH2), 1.0f, D2D1.BitmapInterpolationMode.Linear); m_D2DContext.d2dContext2.DrawBitmap(m_TitleName, new RawRectangleF((SurfaceR2 / 2) - (VideoW / 2), 0, (SurfaceR2 / 2) + (VideoW / 2), SurfaceH2), 1.0f, D2D1.BitmapInterpolationMode.Linear); } else { string reservedSong = "R".PadRight(2) + addSong; var stringSize = MeasureStringDX(reservedSong, SurfaceW2, m_textFormat15); // m_roundedRec = new D2D1.RoundedRectangle() { Rect = new RawRectangleF(SurfaceW2 - stringSize.Width, (stringSize.Height / 3), (SurfaceR2 - 10), stringSize.Height + (stringSize.Height / 3)), RadiusX = stringSize.Height / 8, RadiusY = stringSize.Height / 8 }; m_roundedRec = new D2D1.RoundedRectangle() { Rect = new RawRectangleF((SurfaceW2 - 10) - (stringSize.Width + 10), (SurfaceT2 + 10), (SurfaceR2 - 10), (stringSize.Height + 10)), RadiusX = stringSize.Height / 8, RadiusY = stringSize.Height / 8 }; m_D2DContext.d2dContext2.DrawRoundedRectangle(m_roundedRec, roundedRecOutColor, 10f); m_D2DContext.d2dContext2.FillRoundedRectangle(m_roundedRec, roundedRecInColor); // m_D2DContext.d2dContext2.DrawText(reservedSong, m_textFormat15, new RawRectangleF(m_roundedRec.Rect.Left + (stringSize.Width / 12), (stringSize.Height / 2.5f), SurfaceR2, stringSize.Height + (stringSize.Height / 3)), m_textBrush); m_D2DContext.d2dContext2.DrawText(reservedSong, m_textFormat15, new RawRectangleF((m_roundedRec.Rect.Left + 5), (m_roundedRec.Rect.Top - 2), SurfaceR2, m_roundedRec.Rect.Bottom), m_textBrush); if (PlayerControl.GetNextSong != "") { string nextSong = "Next song: " + PlayerControl.GetNextSong; var stringSize2 = MeasureStringDX(nextSong, SurfaceW2, m_textFormat10); m_roundedRecNextSong = new D2D1.RoundedRectangle() { Rect = new RawRectangleF((SurfaceL2 + 10), (SurfaceT2 + 10), (m_roundedRec.Rect.Left - 15), (stringSize.Height + 10)), RadiusX = stringSize2.Height / 8, RadiusY = stringSize2.Height / 8 }; m_D2DContext.d2dContext2.DrawRoundedRectangle(m_roundedRecNextSong, roundedRecOutColor, 10f); m_D2DContext.d2dContext2.FillRoundedRectangle(m_roundedRecNextSong, roundedRecInColor); // m_D2DContext.d2dContext2.DrawText(nextSong, m_textFormat10, new RawRectangleF((m_roundedRecNextSong.Rect.Right / 2) - (stringSize2.Width / 2), (m_roundedRecNextSong.Rect.Bottom / 2) - ((stringSize2.Height - 10) / 2), (SurfaceR2 - 10), m_roundedRecNextSong.Rect.Bottom), m_textBrush, D2D1.DrawTextOptions.Clip); m_D2DContext.d2dContext2.DrawText(nextSong, m_textFormat10, new RawRectangleF((SurfaceL2 + 10), (SurfaceT2 + 10), (m_roundedRec.Rect.Left - 15), (stringSize.Height + 10)), m_textBrush, D2D1.DrawTextOptions.Clip); } } m_D2DContext.d2dContext2.DrawImage(m_compositeEffect, new RawVector2(0, 0), D2D1.InterpolationMode.Linear, D2D1.CompositeMode.Xor); m_D2DContext.d2dContext2.DrawBitmap(m_Bitmap1FullScreen, 1f, D2D1.BitmapInterpolationMode.Linear); m_D2DContext.d2dContext2.EndDraw(); m_D2DContext.swapChain2.Present(0, DXGI.PresentFlags.None); UnloadResources(); } } catch (Exception ex) { Logger.LogFile(ex.Message, "", "DoRender", ex.LineNumber(), "GraphicUtilDX"); } }