Exemplo n.º 1
0
        public static Windows.Foundation.IAsyncAction RunOnce()
        {
            return(Task.Run(async() =>
            {
                System.Diagnostics.Debug.WriteLine("Lockscreen Change Task Runned");

                if ((bool)SettingHelper.GetValue("wpt_enable") && (bool)SettingHelper.GetValue("wpt_lsenable"))
                {
                    RequestURL = BackgroundChangeTasksNetworkHelper.BuildURL(BackgroundChangeTasksNetworkHelper.BCTType.Lockscreen);
                    BackgroundTaskPicInfo picInfo = JsonConvert.DeserializeObject <List <BackgroundTaskPicInfo> >(await BackgroundChangeTasksNetworkHelper.SendGETAsyncAsString(RequestURL))[0];
                    if (picInfo != null)
                    {
                        HttpResponseMessage res = await BackgroundChangeTasksNetworkHelper.SendGETAsyncAsBinary(picInfo.file_url);
                        StorageFile picFile = await Helper.Methods.CreateFileInMyFolderWithID(picInfo.id.ToString(), picInfo.file_url.Substring(picInfo.file_url.Length - 4));
                        Windows.Storage.Streams.IRandomAccessStream writeStream = await picFile.OpenAsync(FileAccessMode.ReadWrite);
                        await writeStream.WriteAsync(await res.Content.ReadAsBufferAsync());
                        writeStream.Dispose();
                        await Methods.SetPicAsWallPapaer(picFile, true);
                    }
                }
            }).AsAsyncAction());
        }
        public static async System.Threading.Tasks.Task SavePathImageAsFile(int Width, int Height, string fileName, FrameworkElement element, bool UseRenderTarget = true)
        {
            double oldWidth     = element.Width;
            double oldHeight    = element.Height;
            double actOldWidth  = element.ActualWidth;
            double actOldHeight = element.ActualHeight;

            //if (!UseRenderTarget)
            {
                //engine takes the Ceiling so make sure its below or sometimes off by 1 rounding up from ActualWidth/Height
                element.Width = !UseRenderTarget?Math.Floor((float)Math.Min(Window.Current.Bounds.Width, Width)) : (float)Math.Min(Window.Current.Bounds.Width, Width);

                element.Height = !UseRenderTarget?Math.Floor((float)Math.Min(Window.Current.Bounds.Height, Height)) : (float)Math.Min(Window.Current.Bounds.Height, Height);

                //bool bHasCalledUpdateLayout = false;
                //should wrap into another event handler and check a bHasCalledUpdateLayout to ignore early calls and race condition
                //object lockVar = new object();
                //EventHandler<object> eventHandler = null;
                //System.Threading.Tasks.TaskCompletionSource<object> t = new System.Threading.Tasks.TaskCompletionSource<object>();
                //eventHandler = (sender, e) => { lock (lockVar) { if (bHasCalledUpdateLayout && Math.Abs(element.ActualWidth - element.Width) <= 1 && Math.Abs(element.ActualHeight - element.Height) <= 1) { lock (lockVar) { if (bHasCalledUpdateLayout) { bHasCalledUpdateLayout = false; t.SetResult(e); } } } } };
                //element.LayoutUpdated += eventHandler;
                //lock (lockVar) {
                //    element.UpdateLayout();
                //    bHasCalledUpdateLayout = true;
                //}
                ////await element.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, new Windows.UI.Core.DispatchedHandler(() => element.Dispatcher.ProcessEvents(Windows.UI.Core.CoreProcessEventsOption.ProcessAllIfPresent)));
                //await t.Task;
                //element.LayoutUpdated -= eventHandler;
                await element.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Low, () => { });

                if (!UseRenderTarget && (element.ActualWidth > element.Width || element.ActualHeight > element.Height))
                {
                    if (element.ActualWidth > element.Width)
                    {
                        element.Width -= 1;
                    }
                    if (element.ActualHeight > element.Height)
                    {
                        element.Height -= 1;
                    }
                    //bHasCalledUpdateLayout = false;
                    //t = new System.Threading.Tasks.TaskCompletionSource<object>();
                    //eventHandler = (sender, e) => { lock (lockVar) { if (bHasCalledUpdateLayout && Math.Abs(element.ActualWidth - element.Width) <= 1 && Math.Abs(element.ActualHeight - element.Height) <= 1) { lock (lockVar) { if (bHasCalledUpdateLayout) { bHasCalledUpdateLayout = false; t.SetResult(e); } } } } };
                    //element.LayoutUpdated += eventHandler;
                    //lock (lockVar)
                    //{
                    //    element.UpdateLayout();
                    //    bHasCalledUpdateLayout = true;
                    //}
                    //await t.Task;
                    //element.LayoutUpdated -= eventHandler;
                    await element.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Low, () => { });
                }
            }
#if WINDOWS_APP && STORETOOLKIT
            if (!UseRenderTarget)
            {
                System.IO.MemoryStream memstream = await WinRTXamlToolkit.Composition.WriteableBitmapRenderExtensions.RenderToPngStream(element);

                Windows.Storage.StorageFile file = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync(fileName + ".png", Windows.Storage.CreationCollisionOption.ReplaceExisting);

                Windows.Storage.Streams.IRandomAccessStream stream = await file.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite);

                await stream.WriteAsync(memstream.GetWindowsRuntimeBuffer());

                stream.Dispose();
            }
            else
            {
#endif
            //Canvas cvs = new Canvas();
            //cvs.Width = Width;
            //cvs.Height = Height;
            //Windows.UI.Xaml.Shapes.Path path = new Windows.UI.Xaml.Shapes.Path();
            //object val;
            //Resources.TryGetValue((object)"PathString", out val);
            //Binding b = new Binding
            //{
            //    Source = (string)val
            //};
            //BindingOperations.SetBinding(path, Windows.UI.Xaml.Shapes.Path.DataProperty, b);
            //cvs.Children.Add(path);
            float dpi = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().LogicalDpi;
            Windows.UI.Xaml.Media.Imaging.RenderTargetBitmap wb = new Windows.UI.Xaml.Media.Imaging.RenderTargetBitmap();
            await wb.RenderAsync(element, (int)((float)Width * 96 / dpi), (int)((float)Height * 96 / dpi));

            Windows.Storage.Streams.IBuffer buf = await wb.GetPixelsAsync();

            Windows.Storage.StorageFile file = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync(fileName + ".png", Windows.Storage.CreationCollisionOption.ReplaceExisting);

            Windows.Storage.Streams.IRandomAccessStream stream = await file.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite);

            //Windows.Graphics.Imaging.BitmapPropertySet propertySet = new Windows.Graphics.Imaging.BitmapPropertySet();
            //propertySet.Add("ImageQuality", new Windows.Graphics.Imaging.BitmapTypedValue(1.0, Windows.Foundation.PropertyType.Single)); // Maximum quality
            Windows.Graphics.Imaging.BitmapEncoder be = await Windows.Graphics.Imaging.BitmapEncoder.CreateAsync(Windows.Graphics.Imaging.BitmapEncoder.PngEncoderId, stream);    //, propertySet);

            be.SetPixelData(Windows.Graphics.Imaging.BitmapPixelFormat.Bgra8, Windows.Graphics.Imaging.BitmapAlphaMode.Premultiplied, (uint)wb.PixelWidth, (uint)wb.PixelHeight, dpi, dpi, buf.ToArray());
            await be.FlushAsync();

            await stream.GetOutputStreamAt(0).FlushAsync();

            stream.Dispose();
#if WINDOWS_APP && STORETOOLKIT
        }
#endif
            //if (!UseRenderTarget)
            {
                element.Width  = oldWidth;
                element.Height = oldHeight;
                //bHasCalledUpdateLayout = false;
                //t = new System.Threading.Tasks.TaskCompletionSource<object>();
                //eventHandler = (sender, e) => { lock (lockVar) { if (bHasCalledUpdateLayout && Math.Abs(element.ActualWidth - actOldWidth) <= 1 && Math.Abs(element.ActualHeight - actOldHeight) <= 1) { lock (lockVar) { if (bHasCalledUpdateLayout) { bHasCalledUpdateLayout = false; t.SetResult(e); } } } } };
                //element.LayoutUpdated += eventHandler;
                //lock (lockVar)
                //{
                //    element.UpdateLayout();
                //    bHasCalledUpdateLayout = true;
                //}
                //await t.Task;
                //element.LayoutUpdated -= eventHandler;
                await element.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Low, () => { });
            }
        }