1. 程式人生 > >C# WIN7電腦雙屏設置不同內容的桌面

C# WIN7電腦雙屏設置不同內容的桌面

windows bubuko code 張新 9.png from bound 設置 雙屏

WIN7電腦連接雙屏時,設置壁紙會有兩個比較麻煩的問題:

1. 如果兩個屏幕的分辨率不同,在設置桌面時會導致左右屏幕顯示效果不一樣。

2. 無法將左右兩個屏幕的壁紙設置為不同的圖片。

解決辦法是:將兩張圖片合成為一張新的圖片,再將這張新的圖片設置為壁紙即可。

假設屏幕關系如圖:

技術分享圖片

步驟如下:

1. 新建一張圖片,寬度是 屏幕1的寬度加上屏幕2的寬度,高度是較高的那個屏幕的高度。

2. 圖片1縮放為屏幕1的大小,左側放到最左,頂部放置到最上

3. 圖片2縮放為屏幕2的大小,左側放到屏幕1的最右側位置,頂部放置到最上

理論上使用windows自帶的畫圖工具也可以做出,新圖片的實際效果如下:

技術分享圖片

左下角的黑色部分在設置壁紙後並不會顯示,不會影響顯示效果。

圖片1和圖片2可以使用同一張圖片。

上面是原理,以下是代碼,可以自動根據屏幕分辨率合成適合的壁紙。

   int width1 = Screen.PrimaryScreen.Bounds.Width;
   int height1 = Screen.PrimaryScreen.Bounds.Height;

   int width2 = Screen.AllScreens[1].Bounds.Width;
   int height2 = Screen.AllScreens[1].Bounds.Height;

   using
(Bitmap leftfileImage = new Bitmap(leftFile), rightfileImage = new Bitmap(rightFile), newimage = new Bitmap(width1 + width2, height1 > height2 ? height1 : height2)) { using (Graphics newimagegp = Graphics.FromImage(newimage)) { newimagegp.DrawImage(leftfileImage, new RectangleF(0
, 0, width1, height1), new RectangleF(0, 0, leftfileImage.Width, leftfileImage.Height), GraphicsUnit.Pixel); newimagegp.DrawImage(rightfileImage, new RectangleF(width1, 0, width2, height2), new RectangleF(0, 0, rightfileImage.Width, rightfileImage.Height), GraphicsUnit.Pixel); newimage.Save("新圖片.jpg", ImageFormat.Jpeg); } }

leftFile和rightFile是圖片地址,分別用於左邊屏幕及右邊屏幕。

配合上一篇文章的設置桌面的方法,再加上定時器,即可實現雙屏壁紙隨機生成定時更換的功能。

C# WIN7電腦雙屏設置不同內容的桌面