private void LoadResources() { m_textBrush = new D2D1.SolidColorBrush(m_D2DContext.d2dContext2, new SharpDX.Color(128, 0, 255)); roundedRecOutColor = new D2D1.SolidColorBrush(m_D2DContext.d2dContext2, new SharpDX.Color(32, 117, 81)); roundedRecInColor = new D2D1.SolidColorBrush(m_D2DContext.d2dContext2, new SharpDX.Color(234, 137, 6)); m_textFormat15 = new TextFormat(m_D2DContext.dw_Factory, "Arial", FontWeight.UltraBold, FontStyle.Normal, fontSize15); m_textFormat10 = new TextFormat(m_D2DContext.dw_Factory, "Arial", FontWeight.Bold, FontStyle.Normal, fontSize10); //m_textFormat10.ParagraphAlignment = ParagraphAlignment.Center; //m_textFormat10.TextAlignment = TextAlignment.Center; m_textFormat10.SetParagraphAlignment(ParagraphAlignment.Center); m_textFormat10.SetTextAlignment(TextAlignment.Center); fontFamily = new System.Drawing.FontFamily("Arial"); m_Bitmap1 = new D2D1.Bitmap1(m_D2DContext.d2dContextCdgText, new Size2(SurfaceW, SurfaceH), BitmapProps1); m_Bitmap1FullScreen = new D2D1.Bitmap1(m_D2DContext.d2dContextCdgText2, new Size2(SurfaceW2, SurfaceH2), BitmapProps1); m_Shadow = new D2D1.Effects.Shadow(m_D2DContext.d2dContextCdgText); m_ShadowFullScreen = new D2D1.Effects.Shadow(m_D2DContext.d2dContextCdgText2); m_affineTransformEffect = new D2D1.Effects.AffineTransform2D(m_D2DContext.d2dContextCdgText2); m_compositeEffect = new D2D1.Effects.Composite(m_D2DContext.d2dContextCdgText2); }
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); }
/// <summary> /// Now that we have a CoreWindow object, the DirectX device/context can be created. /// </summary> /// <param name="entryPoint"></param> public void Load(string entryPoint) { // Get the default hardware device and enable debugging. Don't care about the available feature level. // DeviceCreationFlags.BgraSupport must be enabled to allow Direct2D interop. SharpDX.Direct3D11.Device defaultDevice = new SharpDX.Direct3D11.Device(DriverType.Hardware, DeviceCreationFlags.Debug | DeviceCreationFlags.BgraSupport); // Query the default device for the supported device and context interfaces. device = defaultDevice.QueryInterface<SharpDX.Direct3D11.Device1>(); d3dContext = device.ImmediateContext.QueryInterface<SharpDX.Direct3D11.DeviceContext1>(); // Query for the adapter and more advanced DXGI objects. SharpDX.DXGI.Device2 dxgiDevice2 = device.QueryInterface<SharpDX.DXGI.Device2>(); SharpDX.DXGI.Adapter dxgiAdapter = dxgiDevice2.Adapter; SharpDX.DXGI.Factory2 dxgiFactory2 = dxgiAdapter.GetParent<SharpDX.DXGI.Factory2>(); // Description for our swap chain settings. SwapChainDescription1 description = new SwapChainDescription1() { // 0 means to use automatic buffer sizing. Width = 0, Height = 0, // 32 bit RGBA color. Format = Format.B8G8R8A8_UNorm, // No stereo (3D) display. Stereo = false, // No multisampling. SampleDescription = new SampleDescription(1, 0), // Use the swap chain as a render target. Usage = Usage.RenderTargetOutput, // Enable double buffering to prevent flickering. BufferCount = 2, // No scaling. Scaling = Scaling.None, // Flip between both buffers. SwapEffect = SwapEffect.FlipSequential, }; // Generate a swap chain for our window based on the specified description. swapChain = new SwapChain1(dxgiFactory2, device, new ComObject(window), ref description); // Get the default Direct2D device and create a context. SharpDX.Direct2D1.Device d2dDevice = new SharpDX.Direct2D1.Device(dxgiDevice2); d2dContext = new SharpDX.Direct2D1.DeviceContext(d2dDevice, SharpDX.Direct2D1.DeviceContextOptions.None); // Specify the properties for the bitmap that we will use as the target of our Direct2D operations. // We want a 32-bit BGRA surface with premultiplied alpha. BitmapProperties1 properties = new BitmapProperties1(new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied), DisplayProperties.LogicalDpi, DisplayProperties.LogicalDpi, BitmapOptions.Target | BitmapOptions.CannotDraw); // Get the default surface as a backbuffer and create the Bitmap1 that will hold the Direct2D drawing target. Surface backBuffer = swapChain.GetBackBuffer<Surface>(0); d2dTarget = new Bitmap1(d2dContext, backBuffer, properties); // Load bitmap images playerBitmap = this.LoadBitmapFromContentFile("/Assets/Bitmaps/player.png"); terrainBitmap = this.LoadBitmapFromContentFile("/Assets/Bitmaps/terrain.png"); // Create hue rotation effect hueRotationEffect = new SharpDX.Direct2D1.Effects.HueRotation(d2dContext); // Create image shadow effect shadowEffect = new SharpDX.Direct2D1.Effects.Shadow(d2dContext); // Create image transform effect affineTransformEffect = new SharpDX.Direct2D1.Effects.AffineTransform2D(d2dContext); affineTransformEffect.SetInputEffect(0, shadowEffect); affineTransformEffect.TransformMatrix = Matrix3x2.Translation(terrainBitmap.PixelSize.Width * 0.25f, terrainBitmap.PixelSize.Height * 0.25f); // Create composite effect compositeEffect = new SharpDX.Direct2D1.Effects.Composite(d2dContext); compositeEffect.InputCount = 2; compositeEffect.SetInputEffect(0, affineTransformEffect); // Create tiling brush for terrain bitmap terrainBrush = new ImageBrush(d2dContext, terrainBitmap, new ImageBrushProperties() { ExtendModeX = ExtendMode.Wrap, ExtendModeY = ExtendMode.Wrap, SourceRectangle = new RectangleF(0, 0, terrainBitmap.Size.Width, terrainBitmap.Size.Height), }); // Create rendertarget for drawing the tiling brush brushTarget = new Bitmap1(d2dContext, new Size2((int)(terrainBitmap.Size.Width * 10), (int)terrainBitmap.Size.Height), new BitmapProperties1() { PixelFormat = new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied), BitmapOptions = BitmapOptions.Target }); }