Exemplo n.º 1
0
        public static void SaveImage(this UIElement uiElement)
        {
            var dialog = new SaveFileDialog
            {
                DefaultExt = ".png",
                Filter = "PNG | *.png | JPG | *.jpg",
            };
            var save = dialog.ShowDialog();
            if (save.HasValue && save.Value)
            {
                var saveStream = dialog.OpenFile();

                var bitmap = new WriteableBitmap(uiElement, new TranslateTransform());
                var image = bitmap.ToImage();
                if (dialog.SafeFileName.EndsWith(".png"))
                {
                    var encoder = new PngEncoder();
                    encoder.Encode(image, saveStream);
                }
                else if (dialog.SafeFileName.EndsWith(".jpg"))
                {
                    var encoder = new JpegEncoder();
                    encoder.Encode(image, saveStream);
                }

                saveStream.Close();
            }
        }
Exemplo n.º 2
0
        public void CreateTile(WriteableBitmap tileImage, string tileName, Uri pageUri)
        {
            string tileImageFile = System.IO.Path.Combine(ApplicationSettingsManager.SHAREDSHELLCONTENT_PATH, tileName + ".png");

            using(IsolatedStorageFileStream ifs = ApplicationSettingsManager.UserStoreForApplication.OpenFile(tileImageFile, FileMode.Create, FileAccess.ReadWrite))
            {
                ExtendedImage image = tileImage.ToImage();
                image.WriteToStream(ifs, tileImageFile);
            }

            StandardTileData tileData = new StandardTileData
            {
                Title = String.Format("uknit - {0}", tileName),
                BackgroundImage = new Uri("isostore:/" + tileImageFile, UriKind.Absolute)
            };

            ShellTile.Create(pageUri, tileData);
        }
        public void SaveCaptureImageToLocal(FrameworkElement saveElement)
        {

            WriteableBitmap writeBitmap = new WriteableBitmap(saveElement,null); 
            string thirdDicUrl = "Shared/ShellContent/titles";
            using (var store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (!store.DirectoryExists(thirdDicUrl))      
                    store.CreateDirectory(thirdDicUrl);

                using (var writer = new StreamWriter(new IsolatedStorageFileStream(SaveBackgroundImagePath, FileMode.Create, FileAccess.Write, store)))
                {
                    var pngEncoder = new ImageTools.IO.Png.PngEncoder();
                    pngEncoder.Encode(writeBitmap.ToImage(), writer.BaseStream);   
  
                    //MemoryStream controlStream = new MemoryStream();
                    //writeBitmap.SaveJpeg(controlStream,691,336, 0, 100);
                    //writeBitmap.SetSource(controlStream);
                    //writeBitmap.WritePNG(writer.BaseStream);
                }
            }
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.Dispatcher.BeginInvoke(() =>
                {
                    FrameBorder.Visibility = Visibility.Collapsed;
                    ResultBorder.Visibility = Visibility.Visible;
                });

            Uri serviceUri = new Uri(Defines.BaseUri + "/services/account/uploadphoto");

            WebClient client = new WebClient();

            client.WriteStreamClosed += new WriteStreamClosedEventHandler((o, args) =>
            {
                loadingTextBlock.Visibility = Visibility.Collapsed;
                resultPanel.Visibility = Visibility.Visible;

                Defines.MainPage.RefreshUserInfo();
            });

            client.OpenWriteCompleted += new OpenWriteCompletedEventHandler((o, args) =>
            {
                WriteableBitmap bmp = new WriteableBitmap(frame.Source as BitmapSource);
                Stream encoded = bmp.ToImage().ToStreamByExtension("png");
                encoded.Seek(0, SeekOrigin.Begin);

                byte[] data = new byte[encoded.Length];
                encoded.Read(data, 0, (int)encoded.Length);
                args.Result.Write(data, 0, (int)data.Length);
                args.Result.Flush();
                args.Result.Close();
            });

            client.OpenWriteAsync(serviceUri, "POST");

            resultPhoto.Source = frame.Source;
        }
Exemplo n.º 5
0
        /// <summary>
        /// Loads an image asynchronously. If the image is already cached, it will be loaded from the cache. Otherwise it will be added to the cache
        /// </summary>
        /// <param name="url">An absolute URI pointing to the image</param>
        /// <returns>A Bitmap with the image</returns>
        public async static Task<ExtendedImage> LoadCachedImageFromUrlAsync(Uri url)
        {
            try
            {
                using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    string file = Path.Combine(CacheFolder, getCachenameFromUri(url));

                    if (cacheContainsUri(url))
                    {
                        ExtendedImage cachedImage = new ExtendedImage();
                        using (IsolatedStorageFileStream cachedFile = storage.OpenFile(file, FileMode.Open, FileAccess.Read))
                        {
                            BitmapImage image = new BitmapImage();
                            image.SetSource(cachedFile);

                            WriteableBitmap tmp = new WriteableBitmap(image);
                            cachedImage = tmp.ToImage();

#if DEBUG
                            App.logger.log("Loaded image {0} from cached file {1}", url, file);
#endif
                            return cachedImage;
                        }
                    }
                    else
                    {
                        ExtendedImage loadedImage = await Helpers.LoadImageFromUrlAsync(url);

                        //GIF files don't support saving with imagetools
                        if (!url.ToString().EndsWith("gif", StringComparison.InvariantCultureIgnoreCase))
                            saveImageToCache(loadedImage, file, storage);

                        return loadedImage;
                    }
                }
            }
            catch (Exception e)
            {
                if (e is IOException)
                {
#if DEBUG
                    App.logger.log("Error loading from cache: {0}", url);
#endif
                }
#if DEBUG
                throw;
#endif
                return null;
            }
        }
Exemplo n.º 6
0
        private async Task SaveTheImage(WriteableBitmap bitmap)
        {
            if (await _storageService.FileExistsAsync(LockScreenFile))
            {
                await _storageService.DeleteFileAsync(LockScreenFile);
            }

            using (var fileStream = await _storageService.CreateFileAsync(LockScreenFile))
            {
                var encoder = new PngEncoder();
                var image = bitmap.ToImage();
                encoder.Encode(image, fileStream);
            }
        }
Exemplo n.º 7
0
        public void UpdateTile(WriteableBitmap tileImage, string tileName)
        {
            ShellTile tile = ShellTile.ActiveTiles.FirstOrDefault(x => x.NavigationUri.ToString().Contains(tileName));

            if(tile != null)
            {
                string tileImageFile = System.IO.Path.Combine(ApplicationSettingsManager.SHAREDSHELLCONTENT_PATH, tileName + ".png");

                ApplicationSettingsManager.UserStoreForApplication.DeleteFile(tileImageFile);

                using(IsolatedStorageFileStream ifs = ApplicationSettingsManager.UserStoreForApplication.OpenFile(tileImageFile, FileMode.Create, FileAccess.ReadWrite))
                {
                    ExtendedImage image = tileImage.ToImage();
                    image.WriteToStream(ifs, tileImageFile);
                }

                StandardTileData tileData = new StandardTileData
                {
                    Title = String.Format("uknit - {0}", tileName),
                    BackgroundImage = new Uri("isostore:/" + tileImageFile, UriKind.Absolute)
                };

                tile.Update(tileData);
            }
        }