예제 #1
0
        /// <summary>
        /// Updates the band wallpaper
        /// </summary>
        /// <param name="file"></param>
        public async Task<bool> UpdateBandWallpaper(StorageFile file)
        {
            try
            {
                IBandInfo pairedBand = await GetPairedBand();
                if (pairedBand == null)
                {
                    // We don't have a band.
                    return true;
                }

                // Try to connect to the band.
                using (IBandClient bandClient = await BandClientManager.Instance.ConnectAsync(pairedBand))
                {
                    WriteableBitmap bitmap = null;
                    BandImage newBandImage = null;
                    //byte[] pixelArray = null;
                    using (AutoResetEvent are = new AutoResetEvent(false))
                    {
                        // Get the bitmap on the UI thread.
                        await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low, async () =>
                        {
                            try
                            {
                                // Create a bitmap for the Me Tile image. 
                                // The image must be 310x102 pixels for Microsoft Band 1 
                                // and 310x102 or 310x128 pixels for Microsoft Band 2.  
                                ImageProperties properties = await file.Properties.GetImagePropertiesAsync();
                                bitmap = new WriteableBitmap((int)properties.Width, (int)properties.Height);
                                bitmap.SetSource((await file.OpenReadAsync()));
                                newBandImage = bitmap.ToBandImage();
                                //pixelArray = bitmap.ToByteArray();
                            }
                            catch (Exception) { }
                            are.Set();
                        });
                        are.WaitOne(10000);
                    }

                    // Check if we failed
                    if(bitmap == null || newBandImage == null)
                    {
                        throw new Exception("Failed to make new bitmap image.");
                    }

                    // Try to set the image.
                    await bandClient.PersonalizationManager.SetMeTileImageAsync(newBandImage);

                    //// Compute the average color
                    //Color averageColor = new Color();
                    //uint bAvg = 0;
                    //uint rAvg = 0;
                    //uint gAvg = 0;
                    //uint count = 0;
                    //for (int i = 0; i < pixelArray.Length; i += 4)
                    //{
                    //    rAvg += pixelArray[i + 1];
                    //    gAvg += pixelArray[i + 2];
                    //    bAvg += pixelArray[i + 3];
                    //    count++;
                    //}
                    //averageColor.R = (byte)(rAvg / count);
                    //averageColor.B = (byte)(bAvg / count);
                    //averageColor.G = (byte)(gAvg / count);

                    //BandTheme theme = new BandTheme() { Base = averageColor.ToBandColor() };
                    //await bandClient.PersonalizationManager.SetThemeAsync(theme);
                }
            }
            catch(Exception e)
            {
                m_baconMan.MessageMan.DebugDia("failed to set band wallpaper", e);
                return false;
            }
            return true;
        }