Esempio n. 1
0
        private void DoWork(object sender, DoWorkEventArgs args)
        {
            string url = args.Argument.ToString();
            string name = string.Format("image-{0}.jpg", DateTime.Now.Ticks);

            if (url.Contains("/") && !url.Contains("attachment.php"))
            {
                var tokens = url.Split('/');
                name = tokens[tokens.Length - 1];
            }

            HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;
            Stream data = null;

            request.BeginGetResponse(state =>
                {
                    try
                    {
                        var response = request.EndGetResponse(state);
                        data = response.GetResponseStream();
                    }
                    catch (Exception ex) { Console.WriteLine(ex.Message); }
                    finally { signal.Set(); }

                }, request);

            signal.WaitOne(App.Settings.ThreadTimeout);

            if (worker.CancellationPending) { args.Cancel = true; return; }

            if (data == null)
            {
                args.Result = Awful.Core.Models.ActionResult.Failure;
                return;
            }

             if (worker.CancellationPending) { args.Cancel = true; return; }
             IsolatedStorageFileStream fileStream = null;

            try
            {
                MediaLibrary lib = new MediaLibrary();

                string tempFile = name;
                var dispatch = Deployment.Current.Dispatcher;
                var dispatchSignal = new AutoResetEvent(false);
                var store = IsolatedStorageFile.GetUserStoreForApplication();

                dispatch.BeginInvoke(() =>
                    {
                        try
                        {
                            if (store.FileExists(tempFile))
                                store.DeleteFile(tempFile);

                            fileStream = store.CreateFile(tempFile);
                            WriteableBitmap bitmap;

                            try
                            {
                                var decoder = new GifDecoder();
                                var image = new ExtendedImage();
                                decoder.Decode(image, data);
                                bitmap = image.ToBitmap();
                            }
                            catch (Exception ex)
                            {
                                var image = new BitmapImage();
                                image.SetSource(data);
                                bitmap = new WriteableBitmap(image);
                            }

                            bitmap.SaveJpeg(fileStream, bitmap.PixelWidth, bitmap.PixelHeight, 0, 100);
                            fileStream.Close();
                            dispatchSignal.Set();
                        }
                        catch (Exception ex) { dispatchSignal.Set(); }
                    });

                dispatchSignal.WaitOne();
                fileStream = store.OpenFile(tempFile, FileMode.Open, FileAccess.Read);
                lib.SavePicture(name, fileStream);
                fileStream.Close();
                store.DeleteFile(tempFile);

                args.Result = Awful.Core.Models.ActionResult.Success;
            }
            catch (Exception ex)
            {
                if (fileStream != null)
                    fileStream.Close();

                Console.WriteLine(ex.Message);
                args.Result = Awful.Core.Models.ActionResult.Failure;
            }
        }
Esempio n. 2
0
        private void ViewCurrentImageButton_Click(object sender, RoutedEventArgs e)
        {
            GoogleAnalytics.EasyTracker.GetTracker().SendEvent("splashchanger", "click", "View Current Image", 0);
            try
            {
            #if WP8
                CustomMessageBox cmb = new CustomMessageBox()
                {
                    IsLeftButtonEnabled = true,
                    IsRightButtonEnabled = true,
                    RightButtonContent = "Shutdown",
                    LeftButtonContent = "Startup",
                    Message = "Which boot screen you'd like to view?"

                };
                cmb.Dismissed += new EventHandler<DismissedEventArgs>((dsender, de) =>
                {
                    string screen = "Boot";
                    if (de.Result == CustomMessageBoxResult.RightButton)
                    {
                        screen = "Shutdown";
                    }
                    try {
                    using (var stream = System.IO.File.OpenRead(Registry.ReadString(RegistryHive.HKLM, @"SYSTEM\Shell\BootScreens", "WP" + screen + "ScreenOverride")))
                    {
            #else

                using (var store = System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication())
                {
                    if (!store.FileExists("mologo.bmp"))
                    {

                        string dir = @"\Applications\Data\abc1e9fe-b4ab-402c-ab21-11e97e3fde3a\Data\IsolatedStore";
                        WP7RootToolsSDK.FileSystem.CopyFile(@"\windows\mologo.bmp", dir + "\\mologo.bmp");
                    }
                    using (var stream = store.OpenFile("mologo.bmp", System.IO.FileMode.Open))
                    {
            #endif
            #if WP8
                        var iso = new ExtendedImage();
                        var dec = new ImageTools.IO.Bmp.BmpDecoder();
                        dec.Decode(iso, stream);
                        SplashImage.Source = iso.ToBitmap();
                        SplashImage.Visibility = Visibility.Visible;
                    }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Failed", MessageBoxButton.OK);
                    }
                });
                cmb.Show();
            #else
                        MessageBox.Show("Feature not implemented yet!");
                }
            #endif

            #if !WP8
            }
		public static WriteableBitmap GetBitmapSource(ExtendedImage image)
		{
			// Waits for the bitmap to be loaded if it needs so.
			if (!image.IsFilled)
			{
				if (!image.IsLoading)
				{
                    return null;
				}

				ManualResetEvent resetEvent = new ManualResetEvent(false);

				EventHandler onLoaded = new EventHandler((o, e) =>
				{
					resetEvent.Set();
				});

				image.LoadingCompleted += onLoaded;

				resetEvent.WaitOne(1000);

				image.LoadingCompleted -= onLoaded;
			}

			// Converts the bitmap.
            return image.ToBitmap();
		}
        private void invert_Click(object sender, EventArgs e)
        {
            ExtendedImage loadedImage;
            ExtendedImage filteredImage;
            Inverter filter;

            WriteableBitmap wb = new WriteableBitmap(image);
            loadedImage = new ExtendedImage();
            loadedImage.SetPixels(image.PixelWidth, image.PixelHeight, wb.ToByteArray());
            filter = new Inverter();
            filteredImage = new ExtendedImage(loadedImage);
            filter.Apply(filteredImage, loadedImage, new ImageTools.Rectangle(0, 0, loadedImage.PixelWidth, loadedImage.PixelHeight));
            var bitmap = filteredImage.ToBitmap();
            imageView.Source = bitmap;
        }