private string CreateBackgroundImage(Dictionary <string, Image> imageFiles, SCALEMODE scaling) { using (var virtualScreenBitmap = new Bitmap((int)SystemInformation.VirtualScreen.Width, (int)SystemInformation.VirtualScreen.Height)) { using (var virtualScreenGraphic = Graphics.FromImage(virtualScreenBitmap)) { Dictionary <string, Image> test = new Dictionary <string, Image>(); var screens = Screen.AllScreens; int addToCompLeft = 0; int addToCompTop = 0; //Can't draw into negative on bitmap -> move everything thats negative to 0 and everything else by that movement foreach (var item in Screen.AllScreens) { if (item.Bounds.Top < 0) { addToCompTop = item.Bounds.Top * -1; } if (item.Bounds.Left < 0) { addToCompLeft = item.Bounds.Left * -1; } } foreach (var screen in screens) { 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) //This Monitor has an Image -> Draw it as wished { switch (scaling) { case SCALEMODE.CENTERED: DrawImageCentered(fromImage, image, new Rectangle(0, 0, monitorBitmap.Width, monitorBitmap.Height)); break; case SCALEMODE.STRETCHED: DrawImageStretched(fromImage, image, new Rectangle(0, 0, monitorBitmap.Width, monitorBitmap.Height)); break; case SCALEMODE.OVERSCALED: DrawImageOverScaled(fromImage, image, new Rectangle(0, 0, monitorBitmap.Width, monitorBitmap.Height)); break; case SCALEMODE.OVERSCALEDANDCENTERED: DrawImageOverScaledAndCentered(fromImage, image, new Rectangle(0, 0, monitorBitmap.Width, monitorBitmap.Height)); break; } } //Can't draw into negative on bitmap -> move everything thats negative to 0 and everything else by that movement var left = monitorDimensions.Left + addToCompLeft; var top = monitorDimensions.Top + addToCompTop; Rectangle rectangle = new Rectangle(left, top, monitorDimensions.Width, monitorDimensions.Height); virtualScreenGraphic.DrawImage(monitorBitmap, rectangle); virtualScreenGraphic.Save(); } virtualScreenBitmap.Save(finalImageFullPath, ImageFormat.Png); } } return(this.finalImageFullPath); }
public string MergeImagesAccordingToMonitors(Dictionary <string, Image> images, SCALEMODE scaling) { return(CreateBackgroundImage(images, scaling)); }