Example #1
0
        public ConnectPage()
        {
            InitializeComponent();

            // Sauvegarde de l'avatar dans la mémoire du téléphone
            _webClient = new WebClient();
            _webClient.OpenReadCompleted += (s1, e1) =>
            {
                if (e1.Error == null)
                {
                    try
                    {
                        string fileName         = store["userAvatarFile"] as string;
                        bool   isSpaceAvailable = IsSpaceIsAvailable(e1.Result.Length);
                        if (isSpaceAvailable)
                        {
                            if (isoStore.FileExists(fileName))
                            {
                                isoStore.DeleteFile(fileName);
                            }
                            using (var isfs = new IsolatedStorageFileStream(fileName, FileMode.CreateNew, isoStore))
                            {
                                long   fileLen = e1.Result.Length;
                                byte[] b       = new byte[fileLen];
                                e1.Result.Read(b, 0, b.Length);
                                if (b[0] == 71 && b[1] == 73 && b[2] == 70)
                                {
                                    // File is a GIF, we need to convert it!
                                    var image      = new ExtendedImage();
                                    var gifDecoder = new ImageTools.IO.Gif.GifDecoder();
                                    gifDecoder.Decode(image, new MemoryStream(b));
                                    image.WriteToStream(isfs, "avatar.png");

                                    Dispatcher.BeginInvoke(() =>
                                    {
                                        store.Remove("isConnected");
                                        store.Add("isConnected", "");
                                        NavigationService.Navigate(new Uri("/WelcomePage.xaml?from=connect", UriKind.Relative));
                                    });
                                }
                                else
                                {
                                    isfs.Write(b, 0, b.Length);
                                    isfs.Flush();
                                    Dispatcher.BeginInvoke(() =>
                                    {
                                        store.Remove("isConnected");
                                        store.Add("isConnected", "");
                                        NavigationService.Navigate(new Uri("/WelcomePage.xaml?from=connect", UriKind.Relative));
                                    });
                                }
                            }
                        }
                        else
                        {
                            Dispatcher.BeginInvoke(() => { MessageBox.Show("Erreur lors de la première connexion. La mémoire de votre terminal semble pleine."); });
                        }
                    }
                    catch (Exception ex)
                    {
                        Dispatcher.BeginInvoke(() => { MessageBox.Show("Erreur n°1 dans la sauvegarde de l'avatar : " + ex.Message); });
                    }
                }
                else
                {
                    Dispatcher.BeginInvoke(() => { MessageBox.Show("Erreur n°2 dans la sauvegarde de l'avatar : " + e1.Error.Message); });
                }
            };
        }
Example #2
0
        /// <summary>
        /// Gets the traffic map.
        /// </summary>
        /// <returns></returns>
        public IObservable<ExtendedImage> GetTrafficMap()
        {
            return new AnonymousObservable<ExtendedImage>(observer =>
            {
                IObservable<MemoryStream> observable = ObservableHelper.StreamFromUri(new Uri(DfwTranstarUri.TrafficMapUri));

                observable.Subscribe(stream =>
                {
                    if (stream != null)
                    {
                        var decoder = new ImageTools.IO.Gif.GifDecoder();
                        var image = new ExtendedImage();
                        decoder.Decode(image, stream);

                        observer.OnNext(image);
                        observer.OnCompleted();
                    }
                }, ex =>
                {
                    observer.OnError(new Exception("Unable to retrieve traffic data.", ex));
                });

                return Disposable.Empty;
            });
        }
Example #3
0
        private void ParseImg(HtmlNode htmlNode, ref List <Elements.BaseElement> elements)
        {
            string path = _ePubViewer.Book.GetFilePath(HttpUtility.UrlDecode(htmlNode.Attributes["src"].Value.Replace("../", "")));

            if (path == null) // img src file does not exists (don't render)
            {
                Debug.WriteLine(String.Format(CultureInfo.InvariantCulture, "Warning: EPubReader.Parseer.ParseImg - Image \"{0}\" does not exists.", HttpUtility.UrlDecode(htmlNode.Attributes["src"].Value.Replace("../", ""))));
                return;
            }

            Stream s = IsolatedStorageHelper.ReadZipStream(path);

            string fileName = Guid.NewGuid().ToString();
            Size   size     = Size.Empty;

            // detect image type
            if (
                path.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase)
                ||
                path.EndsWith(".jpeg", StringComparison.OrdinalIgnoreCase)
                ||
                path.EndsWith(".jpe", StringComparison.OrdinalIgnoreCase)
                ||
                path.EndsWith(".jif", StringComparison.OrdinalIgnoreCase)
                ||
                path.EndsWith(".jfif", StringComparison.OrdinalIgnoreCase)
                ||
                path.EndsWith(".jfi", StringComparison.OrdinalIgnoreCase)
                )
            {
                BitmapSource bs = new BitmapImage();
                bs.SetSource(s);

                size = new Size(bs.PixelWidth, bs.PixelHeight);

                // save image
                fileName += ".jpg";
                string isfPath = Path.Combine(_ePubViewer.Book.GetImagesDirectory(), fileName);
                try
                {
                    using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        if (!isf.DirectoryExists(Path.GetDirectoryName(isfPath)))
                        {
                            isf.CreateDirectory(Path.GetDirectoryName(isfPath));
                        }

                        using (IsolatedStorageFileStream isfs = isf.CreateFile(isfPath))
                        {
                            WriteableBitmap wb = new WriteableBitmap(bs);
                            wb.SaveJpeg(isfs, bs.PixelWidth, bs.PixelHeight, 0, 100);
                            isfs.Close();
                        }
                    }
                }
                catch
                {
                    Debug.WriteLine(String.Format(CultureInfo.InvariantCulture, "Warning: EPubReader.Parseer.ParseImg - Error saving image \"{0}\".", isfPath));
                    return;
                }
            }
            else if (
                path.EndsWith(".bmp", StringComparison.OrdinalIgnoreCase)
                ||
                path.EndsWith(".dib", StringComparison.OrdinalIgnoreCase)
                ||
                path.EndsWith(".gif", StringComparison.OrdinalIgnoreCase)
                ||
                path.EndsWith(".png", StringComparison.OrdinalIgnoreCase)
                )
            {
                ImageTools.ExtendedImage    extendedImage = new ImageTools.ExtendedImage();
                ImageTools.IO.IImageDecoder decoder;

                if (
                    path.EndsWith(".bmp", StringComparison.OrdinalIgnoreCase)
                    ||
                    path.EndsWith(".dib", StringComparison.OrdinalIgnoreCase)
                    )
                {
                    decoder = new ImageTools.IO.Bmp.BmpDecoder();
                }
                else if (path.EndsWith(".gif", StringComparison.OrdinalIgnoreCase))
                {
                    decoder = new ImageTools.IO.Gif.GifDecoder();
                }
                else // if (path.EndsWith(".png", StringComparison.OrdinalIgnoreCase))
                {
                    decoder = new ImageTools.IO.Png.PngDecoder();
                }

                decoder.Decode(extendedImage, s);

                size = new Size(extendedImage.PixelWidth, extendedImage.PixelHeight);

                ImageTools.IO.Png.PngEncoder pngEncoder = new ImageTools.IO.Png.PngEncoder();

                // save image
                fileName += ".png";
                string isfPath = Path.Combine(_ePubViewer.Book.GetImagesDirectory(), fileName);
                try
                {
                    using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        if (!isf.DirectoryExists(Path.GetDirectoryName(isfPath)))
                        {
                            isf.CreateDirectory(Path.GetDirectoryName(isfPath));
                        }

                        using (IsolatedStorageFileStream isfs = isf.CreateFile(isfPath))
                        {
                            pngEncoder.Encode(extendedImage, isfs);
                            isfs.Close();
                        }
                    }
                }
                catch
                {
                    Debug.WriteLine(String.Format(CultureInfo.InvariantCulture, "Warning: EPubReader.Parseer.ParseImg - Error saving image \"{0}\".", isfPath));
                    return;
                }
            }
            else
            {
                s.Close();
                s.Dispose();

                Debug.WriteLine(String.Format(CultureInfo.InvariantCulture, "Warning: EPubReader.Parseer.ParseImg - Not recognizable image type \"{0}\".", path));
                return;
            }

            RemoveLastBreak(ref elements);

            // add image
            Elements.BaseElement element = new Elements.Image()
            {
                FileName = fileName,
                Width    = (int)size.Width,
                Height   = (int)size.Height
            };

            ExtractId(htmlNode, ref element);

            elements.Add(element);
        }