とりあえず、出来た版
メインとサブの位置関係とかの考慮がないので、自宅の場合、左右逆に表示されてしまう。
public partial class MainWindow : Window { const int SPI_SETDESKWALLPAPER = 20; const int SPIF_UPDATEINIFILE = 0x01; const int SPIF_SENDWININICHANGE = 0x02; [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern int SystemParametersInfo( int uAction, int uParam, string lpvParam, int fuWinIni); private string image1filename = ""; private string image2filename = ""; void setImage1filename(string filename) { image1filename = filename; } void setImage2filename(string filename) { image2filename = filename; } string getImage1filename() { return image1filename; } string getImage2filename() { return image2filename; } public MainWindow() { InitializeComponent(); } private void button1_Click(object sender, RoutedEventArgs e) { // デスクトップサイズを調べる。 foreach (Screen s in Screen.AllScreens) { textBlock1.Text = textBlock1.Text + s.Bounds + "\n"; } // 何台モニタが接続されているか調べる。 //textBlock1.Text = SystemParameters.VirtualScreenHeight + ", " + SystemParameters.VirtualScreenWidth; } private void button2_Click(object sender, RoutedEventArgs e) { // 画像を選択する Microsoft.Win32.OpenFileDialog dlg =new Microsoft.Win32.OpenFileDialog(); Nullable<bool> result = dlg.ShowDialog(); // Process open file dialog box results if (result == true) { // Open document string filename = dlg.FileName; textBlock1.Text = textBlock1.Text + filename + "\n"; image2.Source =new BitmapImage( new Uri(filename)); setImage1filename(filename); } } private void button3_Click(object sender, RoutedEventArgs e) { // 画像を選択する Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog(); Nullable<bool> result = dlg.ShowDialog(); // Process open file dialog box results if (result == true) { // Open document string filename = dlg.FileName; textBlock1.Text = textBlock1.Text + filename + "\n"; image3.Source = new BitmapImage(new Uri(filename)); setImage2filename(filename); } } private void button4_Click(object sender, RoutedEventArgs e) { //壁紙を生成し、壁紙を変更する。 // 1枚目の画像をバッファに読み込む。 Image myImage1 = new Image(); myImage1.Width = 1680; BitmapImage myBitmapImage1 = new BitmapImage(); myBitmapImage1.BeginInit(); myBitmapImage1.UriSource = new Uri(getImage1filename()); myBitmapImage1.DecodePixelWidth = 1680; myBitmapImage1.EndInit(); myImage1.Source = myBitmapImage1; // 2枚目の画像を読み込む。 Image myImage2 = new Image(); myImage1.Width = 1680; BitmapImage myBitmapImage2 = new BitmapImage(); myBitmapImage2.BeginInit(); myBitmapImage2.UriSource = new Uri(getImage2filename()); myBitmapImage2.DecodePixelWidth = 1680; myBitmapImage2.EndInit(); myImage2.Source = myBitmapImage2; int stride1 = (myBitmapImage1.PixelWidth * myBitmapImage1.Format.BitsPerPixel + 7) / 8; byte[] data1 = new byte[stride1 * myBitmapImage1.PixelHeight]; myBitmapImage1.CopyPixels(data1, stride1, 0); int stride2 = (myBitmapImage2.PixelWidth * myBitmapImage2.Format.BitsPerPixel + 7) / 8; byte[] data2 = new byte[stride2 * myBitmapImage2.PixelHeight]; myBitmapImage2.CopyPixels(data2, stride2, 0); //2枚を1枚の画像に読み込む。 int newWidth = myBitmapImage1.PixelWidth + myBitmapImage2.PixelWidth; int newHeight = myBitmapImage1.PixelHeight; // Define parameters used to create the BitmapSource. PixelFormat pf = PixelFormats.Bgr32; int width = newWidth; int height = newHeight; int rawStride = (width * pf.BitsPerPixel + 7) / 8; byte[] rawImage = new byte[rawStride * height]; for (int y = 0; y < height; y++) { for (int x = 0; x < stride1+stride2; x++) { if (x < stride1) { // 1枚目の画像からピクセル値を取得する。 rawImage[x + y * rawStride] = data1[x + stride1 * y]; } else { // 2枚目の rawImage[x + y * rawStride] = data2[(x-stride1) + stride1 * y]; } } } // Create a BitmapSource. BitmapSource bitmap = BitmapSource.Create(width, height, 96, 96, pf, null, rawImage, rawStride); // Create an image element; // Set image source. image1.Source = bitmap; //BMP形式でファイルに保存する // 一時ファイルディレクトリの取得 string bmpFileName = System.IO.Path.GetTempPath() +"wallpaper.bmp"; BitmapFrame bmpFrame = BitmapFrame.Create(bitmap); FileStream stream = new FileStream(bmpFileName, FileMode.Create); BmpBitmapEncoder enc = new BmpBitmapEncoder(); enc.Frames.Add(bmpFrame); enc.Save(stream); stream.Close(); // 壁紙を変更する。 RegistryKey key = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", true); key.SetValue(@"WallpaperStyle", "1"); key.SetValue(@"TileWallpaper", "1"); SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, bmpFileName, SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE); } }