Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何设置多个桌面背景(双显示器)_C#_Windows_Monitor_Wallpaper - Fatal编程技术网

C# 如何设置多个桌面背景(双显示器)

C# 如何设置多个桌面背景(双显示器),c#,windows,monitor,wallpaper,C#,Windows,Monitor,Wallpaper,我有一个代码片段可以在两个显示器上设置一个单独的图像,但我最近为我的笔记本电脑安装了第二个显示器,我想修改我的代码,以便为每个显示器设置一个不同的图像 有什么想法吗 (单显示器的此代码snipet为: [DllImport("user32.dll", CharSet = CharSet.Auto)] private static extern Int32 SystemParametersInfo(UInt32 action, UInt32 uParam, String vPara

我有一个代码片段可以在两个显示器上设置一个单独的图像,但我最近为我的笔记本电脑安装了第二个显示器,我想修改我的代码,以便为每个显示器设置一个不同的图像

有什么想法吗

(单显示器的此代码snipet为:

[DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern Int32 SystemParametersInfo(UInt32 action, UInt32 uParam, String vParam, UInt32 winIni);
        private static readonly UInt32 SPI_SETDESKWALLPAPER = 0x14;
        private static readonly UInt32 SPIF_UPDATEINIFILE = 0x01;
        private static readonly UInt32 SPIF_SENDWININICHANGE = 0x02;

        private void SetWallpaper(string path)
        {
            if (path != null)
            {

                string savepath = Settings.Default.SavePath;

                Image imgInFile = Image.FromFile(path);
                imgInFile.Save(savepath, ImageFormat.Bmp);
                SystemParametersInfo(SPI_SETDESKWALLPAPER, 3, savepath, SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);
            }
            else
            {
                MessageBox.Show("Path Null");
            }
        }

)实际上没有区别

可以将多个显示器看作是使墙纸空间更大

因此,你要做的是创建一个壁纸图像的多个图像,以填补在每个显示器的矩形区域

这个问题看起来也像一个使用系统的

;
使用System.Collections.Generic;
使用系统图;
使用系统、绘图、成像;
使用System.Runtime.InteropServices;
名称空间设置壁纸
{
内部课程计划
{
[DllImport(“user32.dll”,CharSet=CharSet.Auto)]
私有静态外部Int32系统参数INFO(UInt32 uiAction、UInt32 uiParam、字符串pvParam、UInt32 fWinIni);
专用静态UInt32 SPI_SETDESKWALLPAPER=20;
私有静态UInt32 SPIF_UpdateInFile=0x1;
public void SetImage(字符串文件名)
{
系统参数sinfo(SPI_SETDESKWALLPAPER,0,文件名,SPIF_UPDATEINIFILE);
}
私有静态void Main(字符串[]args)
{
var images=新字典
{
{@“\\.\DISPLAY1”,Image.FromFile(@“C:\Users\Public\Pictures\Sample Pictures\Lighthouse.jpg”),
{@“\\.\DISPLAY2”,Image.FromFile(@“C:\Users\Public\Pictures\Sample Pictures\Jellyfish.jpg”),
{@“\\.\DISPLAY3”,Image.FromFile(@“C:\Users\Public\Pictures\Sample Pictures\Koala.jpg”)}
};
CreateBackgroundImage(图像);
}
私有静态void CreateBackgroundImage(字典图像文件)
{
常量字符串defaultBackgroundFile=@“C:\Users\Public\Pictures\Sample Pictures\DefaultBackground.bmp”;
使用(var virtualScreenBitmap=新位图((int)System.Windows.SystemParameters.VirtualScreenWidth,(int)System.Windows.SystemParameters.VirtualScreenHeight))
{
使用(var virtualScreenGraphic=Graphics.FromImage(virtualScreenBitmap))
{
foreach(System.Windows.Forms.screen.AllScreens中的变量屏幕)
{
var image=(imageFiles.ContainsKey(screen.DeviceName))?imageFiles[screen.DeviceName]:空;
变量monitorDimensions=屏幕边界;
变量宽度=监视器尺寸。宽度;
var monitorBitmap=新位图(宽度,monitorDimensions.Height);
var fromImage=Graphics.fromImage(监视器位图);
fromImage.FillRectangle(SystemBrusks.Desktop,0,0,monitorBitmap.Width,monitorBitmap.Height);
如果(图像!=null)
DrawImageCenter(fromImage,image,新矩形(0,0,monitorBitmap.Width,monitorBitmap.Height));
矩形;
if(monitorDimensions.Top==0&&monitorDimensions.Left==0)
{
矩形=监视器尺寸;
}
其他的
{
if((monitorDimensions.Left<0&&monitorDimensions.Width>-monitorDimensions.Left)||
(monitorDimensions.Top<0&&monitorDimensions.Height>-monitorDimensions.Top))
{
var isMain=(monitorDimensions.Top<0&&monitorDimensions.Bottom>0);
var left=(monitorDimensions.left<0)
?(int)System.Windows.SystemParameters.VirtualScreenWidth+监视器维度。左侧
:监视器尺寸。左;
变量top=(monitorDimensions.top<0)
?(int)System.Windows.SystemParameters.VirtualScreenhight+监视器尺寸。顶部
:监视器尺寸。顶部;
矩形srrect;
如果(伊斯曼)
{
矩形=新矩形(左,0,监视器尺寸。宽度,监视器尺寸。底部);
srrect=新矩形(0,-monitorDimensions.Top,monitorDimensions.Width,monitorDimensions.Height+monitorDimensions.Top);
}
其他的
{
矩形=新矩形(0,顶部,监视器尺寸。右侧,监视器尺寸。高度);
srrect=新矩形(-monitorDimensions.Left,0,monitorDimensions.Width+monitorDimensions.Left,
监视器(尺寸、高度);
}
VirtualScreengGraphic.DrawImage(监视器位图、矩形、srcRect、GraphicsUnit.Pixel);
矩形=新矩形(左、上、监视器尺寸.宽度、监视器尺寸.高度);
}
其他的
{
var left=(monitorDimensions.left<0)
?(int)System.Windows.SystemParameters.VirtualScreenWidth+监视器维度。左侧
:监视器尺寸。左;
变量top=(monitorDimensions.top<0)
?(int)System.Windows.SystemParameters.VirtualScreenhight+监视器
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

namespace SetWallpapers
{
    internal class Program
    {
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern Int32 SystemParametersInfo(UInt32 uiAction, UInt32 uiParam, String pvParam, UInt32 fWinIni);

        private static UInt32 SPI_SETDESKWALLPAPER = 20;
        private static UInt32 SPIF_UPDATEINIFILE = 0x1;

        public void SetImage(string filename)
        {
            SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, filename, SPIF_UPDATEINIFILE);
        }

        private static void Main(string[] args)
        {
            var images = new Dictionary<string, Image>
            {
                { @"\\.\DISPLAY1", Image.FromFile(@"C:\Users\Public\Pictures\Sample Pictures\Lighthouse.jpg") },
                { @"\\.\DISPLAY2", Image.FromFile(@"C:\Users\Public\Pictures\Sample Pictures\Jellyfish.jpg") },
                { @"\\.\DISPLAY3", Image.FromFile(@"C:\Users\Public\Pictures\Sample Pictures\Koala.jpg") }
            };

            CreateBackgroundImage(images);

        }

        private static void CreateBackgroundImage(Dictionary<string, Image> imageFiles)
        {
            const string defaultBackgroundFile = @"C:\Users\Public\Pictures\Sample Pictures\DefaultBackground.bmp";

            using (var virtualScreenBitmap = new Bitmap((int)System.Windows.SystemParameters.VirtualScreenWidth, (int)System.Windows.SystemParameters.VirtualScreenHeight))
            {
                using (var virtualScreenGraphic = Graphics.FromImage(virtualScreenBitmap))
                {
                    foreach (var screen in System.Windows.Forms.Screen.AllScreens)
                    {
                        var image = (imageFiles.ContainsKey(screen.DeviceName)) ? imageFiles[screen.DeviceName] : null;

                        var monitorDimensions = screen.Bounds;
                        var width = monitorDimensions.Width;
                        var monitorBitmap = new Bitmap(width, monitorDimensions.Height);
                        var fromImage = Graphics.FromImage(monitorBitmap);
                        fromImage.FillRectangle(SystemBrushes.Desktop, 0, 0, monitorBitmap.Width, monitorBitmap.Height);

                        if (image != null)
                            DrawImageCentered(fromImage, image, new Rectangle(0, 0, monitorBitmap.Width, monitorBitmap.Height));

                        Rectangle rectangle;
                        if (monitorDimensions.Top == 0 && monitorDimensions.Left == 0)
                        {
                            rectangle = monitorDimensions;
                        }
                        else
                        {
                            if ((monitorDimensions.Left < 0 && monitorDimensions.Width > -monitorDimensions.Left) ||
                                (monitorDimensions.Top < 0 && monitorDimensions.Height > -monitorDimensions.Top))
                            {
                                var isMain = (monitorDimensions.Top < 0 && monitorDimensions.Bottom > 0);

                                var left = (monitorDimensions.Left < 0)
                                               ? (int) System.Windows.SystemParameters.VirtualScreenWidth + monitorDimensions.Left
                                               : monitorDimensions.Left;

                                var top = (monitorDimensions.Top < 0)
                                              ? (int) System.Windows.SystemParameters.VirtualScreenHeight + monitorDimensions.Top
                                              : monitorDimensions.Top;

                                Rectangle srcRect;
                                if (isMain)
                                {
                                    rectangle = new Rectangle(left, 0, monitorDimensions.Width, monitorDimensions.Bottom);
                                    srcRect = new Rectangle(0, -monitorDimensions.Top, monitorDimensions.Width, monitorDimensions.Height + monitorDimensions.Top);
                                }
                                else
                                {
                                    rectangle = new Rectangle(0, top, monitorDimensions.Right, monitorDimensions.Height);
                                    srcRect = new Rectangle(-monitorDimensions.Left, 0, monitorDimensions.Width + monitorDimensions.Left,
                                                            monitorDimensions.Height);
                                }

                                virtualScreenGraphic.DrawImage(monitorBitmap, rectangle, srcRect, GraphicsUnit.Pixel);
                                rectangle = new Rectangle(left, top, monitorDimensions.Width, monitorDimensions.Height);
                            }
                            else
                            {
                                var left = (monitorDimensions.Left < 0)
                                               ? (int) System.Windows.SystemParameters.VirtualScreenWidth + monitorDimensions.Left
                                               : monitorDimensions.Left;
                                var top = (monitorDimensions.Top < 0)
                                              ? (int) System.Windows.SystemParameters.VirtualScreenHeight + monitorDimensions.Top
                                              : monitorDimensions.Top;
                                rectangle = new Rectangle(left, top, monitorDimensions.Width, monitorDimensions.Height);
                            }
                        }
                        virtualScreenGraphic.DrawImage(monitorBitmap, rectangle);
                    }

                    virtualScreenBitmap.Save(defaultBackgroundFile, ImageFormat.Bmp);
                }
            }

            SystemParametersInfo(SPI_SETDESKWALLPAPER, 0u, defaultBackgroundFile, SPIF_UPDATEINIFILE);
        }


        private static void DrawImageCentered(Graphics g, Image img, Rectangle monitorRect)
        {
            float heightRatio = (float)monitorRect.Height / (float)img.Height;
            float widthRatio = (float)monitorRect.Width / (float)img.Width;
            int height = monitorRect.Height;
            int width = monitorRect.Width;
            int x = 0;
            int y = 0;

            if (heightRatio > 1f && widthRatio > 1f)
            {
                height = img.Height;
                width = img.Width;
                x = (int)((float)(monitorRect.Width - width) / 2f);
                y = (int)((float)(monitorRect.Height - height) / 2f);
            }
            else
            {
                if (heightRatio < widthRatio)
                {
                    width = (int)((float)img.Width * heightRatio);
                    height = (int)((float)img.Height * heightRatio);
                    x = (int)((float)(monitorRect.Width - width) / 2f);
                }
                else
                {
                    width = (int)((float)img.Width * widthRatio);
                    height = (int)((float)img.Height * widthRatio);
                    y = (int)((float)(monitorRect.Height - height) / 2f);
                }
            }

            Rectangle rect = new Rectangle(x, y, width, height);
            g.DrawImage(img, rect);
        }
    }
}
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using Microsoft.Win32;
using System.Windows.Forms;
using System.IO;


namespace toolbox.Wallpaper
{
    public class CustomWally
    {
        const int SetDeskWallpaper = 20;
        const int UpdateIniFile = 0x01;
        const int SendWinIniChange = 0x02;
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);




        Point primaryMonitorPoint = new Point(0, 0);
        const string defaultBackgroundFile = @"C:\Users\Public\Pictures\Sample Pictures\DefaultBackground.jpg";
        Dictionary<string, Image> images = new Dictionary<string, Image>();

        public CustomWally()
        {
            //figure out where the main monitor is in relation to the virtualScreenBitmap
            foreach (Screen scr in Screen.AllScreens)
            {
                images.Add(scr.DeviceName, null);
                screenos.Add(scr.DeviceName);
                if (scr.Bounds.Left < primaryMonitorPoint.X)
                    primaryMonitorPoint.X = scr.Bounds.Left;
                if (scr.Bounds.Top < primaryMonitorPoint.Y)
                    primaryMonitorPoint.Y = scr.Bounds.Top;
            }
            primaryMonitorPoint.X *= -1;
            primaryMonitorPoint.Y *= -1;

            //Image for multiple screens
            images.Add("all", null);

            //set Images in Dictionary in case there are previous Images
            if (File.Exists(defaultBackgroundFile))
            {
                using (var old = new Bitmap(defaultBackgroundFile))
                {
                    foreach (Screen scr in Screen.AllScreens)
                    {
                        Rectangle rectangle = new Rectangle(primaryMonitorPoint.X + scr.Bounds.Left, primaryMonitorPoint.Y + scr.Bounds.Top, scr.Bounds.Width, scr.Bounds.Height);
                        if (old.Width >= (rectangle.X + rectangle.Width) &&
                            old.Height >= (rectangle.Y + rectangle.Height))
                            images[scr.DeviceName] = (Bitmap)old.Clone(rectangle, old.PixelFormat);
                    }
                }
            }
        }



        List<string> screenos = new List<string>();
        int index = 0;


        public void setAlternatingWalls(string file)
        {
            images[screenos[index]] = Image.FromFile(file);
            index++;
            if (index == screenos.Count)
                index = 0;

            CreateBackgroundImage(Method.multiple);
            GC.Collect();
        }

        public void setWallforScreen(Screen screen, string file)
        {
            images[screen.DeviceName] = Image.FromFile(file);
            CreateBackgroundImage(Method.multiple);
            GC.Collect();
        }

        public void setWallforAllScreen(string file)
        {
            images["all"] = Image.FromFile(file);
            CreateBackgroundImage(Method.single);
            GC.Collect();
        }


        private enum Method
        {
            multiple,
            single
        }
        private void CreateBackgroundImage(Method method)
        {

            using (var virtualScreenBitmap = new Bitmap((int)System.Windows.Forms.SystemInformation.VirtualScreen.Width, (int)System.Windows.Forms.SystemInformation.VirtualScreen.Height))
            {
                using (var virtualScreenGraphic = Graphics.FromImage(virtualScreenBitmap))
                {

                    switch (method)
                    {
                        // alternated Screen Images
                        case Method.multiple:
                            foreach (var screen in System.Windows.Forms.Screen.AllScreens)
                            {
                                // gets the image which we want to place in virtualScreenGraphic
                                var image = (images.ContainsKey(screen.DeviceName)) ? images[screen.DeviceName] : null;

                                //sets the position and size where the images will go
                                Rectangle rectangle = new Rectangle(primaryMonitorPoint.X + screen.Bounds.Left, primaryMonitorPoint.Y + screen.Bounds.Top, screen.Bounds.Width, screen.Bounds.Height);

                                // produce a image for the screen and fill it with the desired image... centered
                                var monitorBitmap = new Bitmap(rectangle.Width, rectangle.Height);
                                if (image != null)
                                    DrawImageCentered(Graphics.FromImage(monitorBitmap), image, rectangle);

                                //draws the picture at the right place in virtualScreenGraphic
                                virtualScreenGraphic.DrawImage(monitorBitmap, rectangle);
                            }
                            break;

                        //Single screen Image
                        case Method.single:
                            // gets the image which we want to place in virtualScreenGraphic
                            var image2 = images["all"];

                            //sets the position and size where the images will go
                            Rectangle rectangle2 = new Rectangle(0, 0, virtualScreenBitmap.Width, virtualScreenBitmap.Height);

                            // fill with the desired image... centered                            
                            if (image2 != null)
                                DrawImageCentered(virtualScreenGraphic, image2, rectangle2);

                            //draws the picture at the right place in virtualScreenGraphic
                            virtualScreenGraphic.DrawImage(virtualScreenBitmap, rectangle2);
                            break;
                    }

                    virtualScreenBitmap.Save(defaultBackgroundFile, ImageFormat.Jpeg);
                }
            }

            RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop", true);
            key.SetValue(@"WallpaperStyle", 0.ToString());
            key.SetValue(@"TileWallpaper", 1.ToString());
            SystemParametersInfo(SetDeskWallpaper, 0, defaultBackgroundFile, UpdateIniFile | SendWinIniChange);
        }


        private void DrawImageCentered(Graphics g, Image img, Rectangle monitorRect)
        {
            double ratiodev = (1.0 * monitorRect.Width / monitorRect.Height) - (1.0 * img.Width / img.Height);
            if (((1.0 * monitorRect.Width / monitorRect.Height > 1) && ratiodev > -0.25 && ratiodev < 0.25))
            {
                img = getsnappedIMG(img, monitorRect);
            }


            float heightRatio = (float)monitorRect.Height / (float)img.Height;
            float widthRatio = (float)monitorRect.Width / (float)img.Width;
            int height = monitorRect.Height;
            int width = monitorRect.Width;
            int x = 0;
            int y = 0;

            if (heightRatio < widthRatio)
            {
                width = (int)((float)img.Width * heightRatio);
                height = (int)((float)img.Height * heightRatio);
                x = (int)((float)(monitorRect.Width - width) / 2f);
            }
            else
            {
                width = (int)((float)img.Width * widthRatio);
                height = (int)((float)img.Height * widthRatio);
                y = (int)((float)(monitorRect.Height - height) / 2f);
            }
            Rectangle rect = new Rectangle(x, y, width, height);
            g.DrawImage(img, rect);
        }

        private Image getsnappedIMG(Image img, Rectangle monitorRect)
        {
            double ratiodev = (1.0 * monitorRect.Width / monitorRect.Height) - (1.0 * img.Width / img.Height);
            int height = img.Height;
            int width = img.Width;

            Rectangle rect;
            if (ratiodev < 0)
            {
                rect = new Rectangle(0, 0, (int)((1.0 * monitorRect.Width / monitorRect.Height) * height), height);                
                rect.X = (width - rect.Width) / 2;                
            }
            else
            {
                rect = new Rectangle(0, 0, width, (int)(1.0 * width / (1.0 * monitorRect.Width / monitorRect.Height)));
                rect.Y = (height - rect.Height) / 2;
            }


            var img2 = (Bitmap)img;
            return (Bitmap)img2.Clone(rect, img.PixelFormat);

        }
    }
}