如何使用C#代碼更改桌面牆紙?使用.NET中的代碼更改桌面壁紙
58
A
回答
87
下面是從一個應用程序拷貝的是一類,我寫了一兩年前:
public sealed class Wallpaper
{
Wallpaper() { }
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);
public enum Style : int
{
Tiled,
Centered,
Stretched
}
public static void Set(Uri uri, Style style)
{
System.IO.Stream s = new System.Net.WebClient().OpenRead(uri.ToString());
System.Drawing.Image img = System.Drawing.Image.FromStream(s);
string tempPath = Path.Combine(Path.GetTempPath(), "wallpaper.bmp");
img.Save(tempPath, System.Drawing.Imaging.ImageFormat.Bmp);
RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop", true);
if (style == Style.Stretched)
{
key.SetValue(@"WallpaperStyle", 2.ToString());
key.SetValue(@"TileWallpaper", 0.ToString());
}
if (style == Style.Centered)
{
key.SetValue(@"WallpaperStyle", 1.ToString());
key.SetValue(@"TileWallpaper", 0.ToString());
}
if (style == Style.Tiled)
{
key.SetValue(@"WallpaperStyle", 1.ToString());
key.SetValue(@"TileWallpaper", 1.ToString());
}
SystemParametersInfo(SPI_SETDESKWALLPAPER,
0,
tempPath,
SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);
}
}
我沒有測試過廣泛的,所以在你自己的風險。
-2
扭捏尼爾N爲答案於GIF:
private const int SPI_SETDESKWALLPAPER = 20;
private const int SPIF_UPDATEINIFILE = 0x01;
private const int SPIF_SENDWININICHANGE = 0x02;
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);
public enum Style : int
{
Tiled,
Centered,
Stretched
}
/// <summary>
/// Loops numFrames times, animating the desktop background as the given gif.
/// Remember this will sorta bog down your computer, and probably isn't best to be running 24/7.
/// If numFrames is negative this will loop forever
/// </summary>
/// <param name="gifPath">The gif to be animated</param>
/// <param name="transparencyReplace">If the gif has transparency, it will be "replaced" with this color.</param>
/// <param name="framesPerSecond">How many frames to play per second. This is a max: most likely it will be a little slower than this especially at first.</param>
/// <param name="style">Whether to tile, center, or stretch each gif frame as it's played.</param>
/// <param name="numFrames">The number of frames to play. If negative, this method will loop forever.</param>
public static void SetDesktopBackgroundAsGifAsync(string gifPath, System.Drawing.Color transparencyReplace, int framesPerSecond, Style style, int numFrames)
{
Thread workerThread = new Thread(() => SetDesktopBackgroundAsGif(gifPath, transparencyReplace, framesPerSecond, style, numFrames));
workerThread.Start();
}
/// <summary>
/// Loops numFrames times, animating the desktop background as the given gif.
/// Remember this will sorta bog down your computer, and probably isn't best to be running 24/7.
/// If num frames is negative this will loop forever.
//// <summary>
/// <param name="gifPath">The gif to be animated</param>
/// <param name="backgroundImage">Image to render the gif on top of (because of transparency)</param>
/// <param name="framesPerSecond">How many frames to play per second. This is a max: most likely it will be a little slower than this.</param>
/// <param name="style">Whether to tile, center, or stretch each gif frame as it's played.</param>
/// <param name="numFrames">The number of frames to play. If negative, this method will loop forever.</param>
public static void SetDesktopBackgroundAsGifAsync(string gifPath, System.Drawing.Image backgroundImage, int framesPerSecond, Style style, int numFrames)
{
Thread workerThread = new Thread(() => SetDesktopBackgroundAsGif(gifPath, backgroundImage, framesPerSecond, style, numFrames));
workerThread.Start();
}
/// <summary>
/// Loops numFrames times, animating the desktop background as the given gif.
/// Remember this will sorta bog down your computer, and probably isn't best to be running 24/7.
/// if numFrames is negative this will loop forever
/// </summary>
/// <param name="gifPath">The gif to be animated</param>
/// <param name="transparencyReplace">If the gif has transparency, it will be "replaced" with this color.</param>
/// <param name="framesPerSecond">How many frames to play per second. This is a max: most likely it will be a little slower than this.</param>
/// <param name="style">Whether to tile, center, or stretch each gif frame as it's played.</param>
/// <param name="numFrames">The number of frames to play. If negative, this method will loop forever.</param>
public static void SetDesktopBackgroundAsGif(string gifPath, System.Drawing.Color transparencyReplace, int framesPerSecond, Style style, int numFrames)
{
if (!File.Exists(gifPath))
throw new Exception("Given gif: '" + gifPath + "' not found");
FileStream gifFile = new FileStream(gifPath, FileMode.Open);
GifBitmapDecoder gifDecoder = new GifBitmapDecoder(gifFile, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
if (gifDecoder.Frames.Count == 0)
throw new Exception("No frames in given gif");
Bitmap backgroundImage = new Bitmap(gifDecoder.Frames[0].PixelWidth, gifDecoder.Frames[0].PixelHeight, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
using(Graphics g = Graphics.FromImage(backgroundImage))
{
g.FillRectangle(new System.Drawing.SolidBrush(transparencyReplace), 0, 0, gifDecoder.Frames[0].PixelWidth, gifDecoder.Frames[0].PixelHeight);
}
gifFile.Close();
SetDesktopBackgroundAsGif(gifPath, backgroundImage, framesPerSecond, style, numFrames);
}
/// <summary>
/// Loops infinitely, animating the desktop background as the given gif.
/// Remember this will sorta bog down your computer, and probably isn't best to be running 24/7.
/// </summary>
/// <param name="gifPath">The gif to be animated</param>
/// <param name="backgroundImage">Image to render the gif on top of (because of transparency)</param>
/// <param name="framesPerSecond">How many frames to play per second. This is a max: most likely it will be a little slower than this.</param>
/// <param name="style">Whether to tile, center, or stretch each gif frame as it's played.</param>
/// <param name="numFrames">The number of frames to play. If negative, this method will loop forever.</param>
private static void SetDesktopBackgroundAsGif(string gifPath, System.Drawing.Image backgroundImage, int framesPerSecond, Style style, int numFrames)
{
if (!File.Exists(gifPath))
throw new Exception("Given gif: '" + gifPath + "' not found");
FileStream gifFile = new FileStream(gifPath, FileMode.Open);
GifBitmapDecoder gifDecoder = new GifBitmapDecoder(gifFile, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
if (gifDecoder.Frames.Count == 0)
throw new Exception("No frames in given gif");
Console.WriteLine("Saving frames to temporary files:");
int numFramesSoFar = 0;
for (int i = 0; i < gifDecoder.Frames.Count; i++)
{
BitmapFrame gifFrame = gifDecoder.Frames[i];
PngBitmapEncoder pngEncoder = new PngBitmapEncoder();
pngEncoder.Frames.Add(gifFrame);
MemoryStream pngStream = new MemoryStream();
pngEncoder.Save(pngStream);
Image frameImage = Image.FromStream(pngStream);
Bitmap bmp = new Bitmap(frameImage.Width, frameImage.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
using (Graphics g = Graphics.FromImage(bmp))
{
g.DrawImage(backgroundImage, 0, 0);
g.DrawImageUnscaled(frameImage, 0, 0);
}
string tempPath = Path.Combine(Path.GetTempPath(), gifPath + i + ".bmp");
bmp.Save(tempPath, System.Drawing.Imaging.ImageFormat.Bmp);
Console.WriteLine("Saved frame " + i);
numFramesSoFar++;
if (numFrames >= 0 && numFramesSoFar >= numFrames) break;
}
Console.WriteLine("Setting frames to desktop background at about " + framesPerSecond + " FPS");
// 1.0/... to convert to seconds per frame (instead of frames per second)
// * 1000 to convert to milliseconds per frame
// * 1000 to convert to microseconds per frame
// * 10 to convert to 0.1s of microseconds per frame = 100s of nanoseconds per frame
long ticksBetweenFrames = (long)Math.Round(1.0/framesPerSecond) * 1000*1000*10;
Stopwatch timer = new Stopwatch();
timer.Start();
numFramesSoFar = 0;
while(numFrames < 0 || numFramesSoFar < numFrames)
{
for (int i = 0; i < gifDecoder.Frames.Count; i++)
{
// Sleep until we're at the desired frame rate, if needed.
if(ticksBetweenFrames > timer.ElapsedTicks)
Thread.Sleep(new TimeSpan(Math.Max(0, ticksBetweenFrames - timer.ElapsedTicks)));
timer.Restart();
// From http://stackoverflow.com/a/1061682/2924421
string filePath = Path.Combine(Path.GetTempPath(), "wallpaper" + i + ".bmp");
RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop", true);
if (style == Style.Stretched)
{
key.SetValue(@"WallpaperStyle", 2.ToString());
key.SetValue(@"TileWallpaper", 0.ToString());
}
if (style == Style.Centered)
{
key.SetValue(@"WallpaperStyle", 1.ToString());
key.SetValue(@"TileWallpaper", 0.ToString());
}
if (style == Style.Tiled)
{
key.SetValue(@"WallpaperStyle", 1.ToString());
key.SetValue(@"TileWallpaper", 1.ToString());
}
SystemParametersInfo(SPI_SETDESKWALLPAPER,
0,
filePath,
SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);
numFramesSoFar++;
if (numFrames >= 0 && numFramesSoFar >= numFrames) break;
}
}
gifFile.Close();
}
另外請注意,您需要使用:
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Media;
using System.Windows.Media.Imaging;
最後,右鍵點擊你的項目,添加引用,(在組件和框架)添加Presentation Core,System.Xaml和WindowsBase。
然後右鍵單擊您的項目並進入屬性,並確保目標框架是.Net Framework 4.5。如果你改變這個,你可能需要重新啓動Visual Studio。
7
基於this useful answer,我也讓my own app設置壁紙匹配屏幕分辨率。
但是註冊表設置是錯誤的。這裏是正確的值(在Win 7,Win 8.1,Win 10上測試)。
if (style == Style.Fill)
{
key.SetValue(@"WallpaperStyle", 10.ToString());
key.SetValue(@"TileWallpaper", 0.ToString());
}
if (style == Style.Fit)
{
key.SetValue(@"WallpaperStyle", 6.ToString());
key.SetValue(@"TileWallpaper", 0.ToString());
}
if (style == Style.Span) // Windows 8 or newer only!
{
key.SetValue(@"WallpaperStyle", 22.ToString());
key.SetValue(@"TileWallpaper", 0.ToString());
}
if (style == Style.Stretch)
{
key.SetValue(@"WallpaperStyle", 2.ToString());
key.SetValue(@"TileWallpaper", 0.ToString());
}
if (style == Style.Tile)
{
key.SetValue(@"WallpaperStyle", 0.ToString());
key.SetValue(@"TileWallpaper", 1.ToString());
}
if (style == Style.Center)
{
key.SetValue(@"WallpaperStyle", 0.ToString());
key.SetValue(@"TileWallpaper", 0.ToString());
}
相關問題
- 1. 如何更改桌面壁紙?
- 2. 使用Jpeg文件更改桌面壁紙
- 3. 使用Mono設置桌面壁紙
- 4. 更改OS X 10.7 Lion中所有桌面上的壁紙?
- 5. 定期更換桌面壁紙
- 6. Shell腳本更換桌面壁紙
- 7. 是否可以從Windows8.1應用程序更改桌面壁紙?
- 8. 如何在Ubuntu 14.04使用Python(使用Unity)使用更改桌面壁紙
- 9. 更改Android壁紙
- 10. 以編程方式定期更改桌面壁紙
- 11. 如何更改活動桌面壁紙在C++
- 12. 更改使用c的壁紙樣式#
- 13. 更改壁紙的PowerShell
- 14. 使用代碼調整壁紙大小?
- 15. 如何使用Xamarin更改IOS壁紙
- 16. html文件爲win7/linux桌面壁紙
- 17. 更改代碼中的桌面圖標
- 18. 如何在shell/bash腳本中更改linux上的桌面壁紙
- 19. android壁紙已更改
- 20. 更改主頁壁紙
- 21. 如何更改Android的代碼鎖屏壁紙?
- 22. 使用SystemParametersInfo設置桌面壁紙的問題功能
- 23. 更改所有用戶的壁紙
- 24. 如何用win 8中的批處理代碼更換壁紙?
- 25. 使用C#改變桌面壁紙當前顯示的圖像很小
- 26. 壁紙更改代碼無法正常工作
- 27. Chrome擴展程序改變桌面壁紙
- 28. VB腳本啓動時改變桌面壁紙
- 29. Windows XP中更改壁紙網頁
- 30. 在Windows 7中更改壁紙
這將只設置BMP文件。如果我想設置一個JPEG,該方法是什麼? – Sauron 2009-06-30 04:19:29
不知道如果Windows支持jpeg作爲壁紙......它將其他圖像轉換成bmp設置爲壁紙之前(糾正我,如果我錯了)如果是這種情況,你必須在你的代碼做轉換 – 2009-06-30 04:43:37
我一直在使用這對於jpeg來說很好。僅僅因爲它在上面的代碼中稱爲bmp並不限制它。它*可能會*爲png和gif以及其他人工作,但我沒有證實。 – 2009-06-30 05:07:54