/// <summary> /// 获取图片主颜色 /// </summary> /// <param name="b"></param> /// <param name="backgroundBrush"></param> /// <returns></returns> public static async Task <SolidColorBrush> GetMajorColorAndBlur(byte[] b, ImageBrush backgroundBrush) { WriteableBitmap wb = new WriteableBitmap(1000, 1500); using (IRandomAccessStream iras = b.AsBuffer().AsStream().AsRandomAccessStream()) { await wb.SetSourceAsync(iras); } //高斯模糊 BlurEffect be = new BlurEffect(wb); backgroundBrush.ImageSource = await be.ApplyFilter(10);//高斯模糊等级可以自己定义 //取主色调并应用到TagsTextBlock return(new SolidColorBrush(GetColor.GetMajorColor(wb))); }
public static async Task GetBingTheme() { try { var httpclient = new System.Net.Http.HttpClient(); var picbytes = await httpclient.GetByteArrayAsync("http://appserver.m.bing.net/BackgroundImageService/TodayImageService.svc/GetTodayImage?dateOffset=0&urlEncodeHeaders=true&osName=windowsPhone&osVersion=8.10&orientation=480x800&deviceName=WP8&mkt=en-US"); var picfile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Theme/Skin/Bing/background.jpg")); await FileIO.WriteBytesAsync(picfile, picbytes); WriteableBitmap wb = new WriteableBitmap(1000, 1500); await wb.SetSourceAsync(await picfile.OpenAsync(FileAccessMode.Read)); var bingtheme = new BingThemeData(); bingtheme.BackgroundColor = GetColor.GetMajorColor(wb); bingtheme.BackgroundColor1 = Color.FromArgb(190, bingtheme.BackgroundColor.R, bingtheme.BackgroundColor.G, bingtheme.BackgroundColor.B); var rgb = bingtheme.BackgroundColor.R * 0.299 + bingtheme.BackgroundColor.G * 0.587 + bingtheme.BackgroundColor.B * 0.114; bingtheme.BackgroundColor2 = bingtheme.BackgroundColor; if (rgb >= 192) { { bingtheme.Foreground = Colors.White; bingtheme.Front1 = Colors.White; bingtheme.Front2 = Color.FromArgb(255, 185, 185, 185); bingtheme.List_Background = Color.FromArgb(100, 103, 103, 103); } } else { bingtheme.Foreground = Colors.White; bingtheme.Front1 = Colors.Black; bingtheme.Front2 = Color.FromArgb(255, 124, 124, 124); bingtheme.List_Background = Color.FromArgb(180, 255, 255, 255); } string xamlstring = await PathIO.ReadTextAsync("ms-appx:///Theme/BingTemp.txt"); xamlstring = String.Format(xamlstring, bingtheme.BackgroundColor.ToString(), bingtheme.BackgroundColor1.ToString(), bingtheme.BackgroundColor2.ToString(), bingtheme.Foreground.ToString(), bingtheme.Front1.ToString(), bingtheme.Front2.ToString(), bingtheme.List_Background.ToString()); var xamlfile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Theme/BingTheme.xaml")); await FileIO.WriteTextAsync(xamlfile, xamlstring); } catch (Exception) { await new MessageDialog("无网络连接!").ShowAsync(); } }
/// <summary> /// 读取本地专辑图片修改背景图 /// </summary> async void ChangeImage() { //判断Local是否有文件 if (await ApplicationData.Current.LocalFolder.TryGetItemAsync(ConfigService.ImageFilename) is StorageFile localFile)//本地有专辑图片,读取 { WriteableBitmap writeableBitmap = await FileHelper.OpenWriteableBitmapFile(localFile); SolidColorBrush solidColorBrush = new SolidColorBrush(GetColor.GetMajorColor(writeableBitmap)); mainImageBrush.ImageSource = writeableBitmap; mainImageBrush.Stretch = Stretch.UniformToFill; mainGrid.Background = mainImageBrush; solidColorBrush.Color = OtherHelper.ChangeColor(solidColorBrush.Color, (float)0.4); backgroundBrush = solidColorBrush; } else//本地无专辑图片 { SolidColorBrush solidColorBrush = new SolidColorBrush(Colors.Gray); mainGrid.Background = solidColorBrush; backgroundBrush = solidColorBrush; } }