Je suis nouveau à wpf pouvez-vous s'il vous plaît dites-moi comment changer le papier peint de bureau par le code.Comment changer les images de bureau en Wpf C#
J'ai lu quelques sujets sur ce sujet, mais je ne peux pas sembler trouver la solution dans WPF.
Le problème est le fond d'écran ne change pas lorsque j'appelle SetWallpaper.
Ci-dessous est de rendre le code:
public static ArrayList images;
const int SPI_SETDESKWALLPAPER = 20;
const int SPIF_UPDATEINIFILE = 0x01;
const int SPIF_SENDWININICHANGE = 0x02;
public enum StyleS_Wallpaper : int
{
Tiled, Centered, Stretched
}
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern int SystemParametersInfo(
int uAction, int uParam, string lpvParam, int fuWinIni);
private void OpenExecuted(object sender, ExecutedRoutedEventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.InitialDirectory = "c:\\";
ofd.Multiselect = true;
ofd.Filter = "Image Files (*.jpg)|*.jpg|Image Files (*.png)|*.png|Image File (*.gif)|*.gif|Image File (*.bmp)|*.bmp|Image Files (*.png)|*.png";
//ofd.RestoreDirectory = true;
Nullable<bool> result = ofd.ShowDialog();
if (result == true)
{
FileNames = ofd.FileNames;
if (images == null)
{
images = new ArrayList();
newlist = new List<string>();
}
for (int i = 0; i < FileNames.Length; i++)
{
BitmapImage bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.UriSource = new Uri(FileNames[i]);
bitmap.EndInit();
images.Add(bitmap);
newlist.Add(FileNames[i]);
NextCount++;
}
}
}
public void SetWallpaper(string path,StyleS_Wallpaper selected)
{
RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop", true);
if (selected == StyleS_Wallpaper.Stretched)
{
key.SetValue(@"WallpaperStyle", 2.ToString());
key.SetValue(@"TileWallpaper", 0.ToString());
}
if (selected == StyleS_Wallpaper.Centered)
{
key.SetValue(@"WallpaperStyle", 1.ToString());
key.SetValue(@"TileWallpaper", 0.ToString());
}
if (selected == StyleS_Wallpaper.Tiled)
{
key.SetValue(@"WallpaperStyle", 1.ToString());
key.SetValue(@"TileWallpaper", 1.ToString());
}
SystemParametersInfo(SPI_SETDESKWALLPAPER,
0,
path,
SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);
}
private void CenterImage_Click(object sender, RoutedEventArgs e)
{
BitmapImage img = (BitmapImage)images[currentPicture];
string Path = img.UriSource.ToString();
string name = "0";
// TrimingString Returns the string path as C:\Documents and Settings\ProZec\Desktop\WallPapers
TrimingString(Path, ref name, true);
SetWallpaper(name, StyleS_Wallpaper.Centered);
}
J'utilise bmp FICHIER .. vous pouvez le voir de Mon centreImage_Click Méthode .. Pouvez-vous spécifier le problème que je reçois –
Le fichier sur le disque est-il réellement un fichier BMP? Ou est-ce un JPG ou un autre format? La classe BitmapImage peut être utilisée pour plusieurs formats. Regardez aussi ce fil de discussion: http://www.xtremedotnettalk.com/showthread.php?t=78085 – Jason
Je travaille essentiellement sur WPF ImageControl donc dans wpf, le contrôle de l'image accepte uniquement les images de bitmap. donc je convertis d'autres formats en bmp ci-dessous est mon code, vous pouvez vérifier mon code ci-dessus je suis édité pour montrer à ma conversion d'image –