Ejemplo n.º 1
0
        public static async Task <Uri> CacheIllustrationAsync(Uri internetUri, string fileName = null)
        {
            if (string.IsNullOrEmpty(fileName))
            {
                fileName = internetUri.LocalPath;
                fileName = Path.GetFileName(fileName);
            }

            if (IllustrationFolder == null)
            {
                IllustrationFolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("illustration", CreationCollisionOption.OpenIfExists);
            }

            StorageFile file = null;

            var localUri = new Uri(string.Format("ms-appdata:///local/illustration/{0}", fileName));

            try
            {
                file = await IllustrationFolder.GetFileAsync(fileName);

                return(localUri);
            }
            catch (FileNotFoundException)
            {
            }

            try
            {
                using (var response = await System.Net.HttpWebRequest.CreateHttp(internetUri).GetResponseAsync())
                {
                    using (var stream = response.GetResponseStream())
                    {
                        file = await IllustrationFolder.CreateFileAsync(fileName);

                        using (var filestream = await file.OpenStreamForWriteAsync())
                        {
                            await stream.CopyToAsync(filestream);

                            return(localUri);
                        }
                    }
                }
            }
            catch (Exception)
            {
            }

            return(null);
        }
Ejemplo n.º 2
0
        public static async Task <Uri> CreateTileImageAsync(Uri imageUri, string fileName = null, TileSize tileSize = TileSize.Square150x150)
        {
            //BitmapImage bitmap = new BitmapImage(imageUri);
            Size imgSize;

            if (string.IsNullOrEmpty(fileName))
            {
                fileName = imageUri.LocalPath;
                fileName = Path.GetFileName(fileName);
                string sizeSuffix = "-150";
                switch (tileSize)
                {
                default:
                case TileSize.Default:
                case TileSize.Square150x150:
                    sizeSuffix     = "-150";
                    imgSize.Height = 150;
                    imgSize.Width  = 150;
                    break;

                case TileSize.Square30x30:
                    sizeSuffix     = "-30";
                    imgSize.Width  = 30;
                    imgSize.Height = 30;
                    break;

                case TileSize.Square310x310:
                    sizeSuffix     = "-310";
                    imgSize.Width  = 310;
                    imgSize.Height = 310;
                    break;

                //case TileSize.Square70x70:
                //	sizeSuffix = "-70";
                //	imgSize.Width = 70;
                //	imgSize.Height = 70;
                //	break;
                case TileSize.Wide310x150:
                    sizeSuffix     = "-310x150";
                    imgSize.Width  = 310;
                    imgSize.Height = 150;
                    break;
                }
                fileName = Path.GetFileNameWithoutExtension(fileName) + sizeSuffix + Path.GetExtension(fileName);
            }
            var localUri = new Uri(string.Format("ms-appdata:///local/illustration/{0}", fileName));

            if (IllustrationFolder == null)
            {
                IllustrationFolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("illustration", CreationCollisionOption.OpenIfExists);
            }

            StorageFile file = null;

            try
            {
                file = await IllustrationFolder.GetFileAsync(fileName);

                return(localUri);
            }
            catch (FileNotFoundException)
            {
            }

            try
            {
                using (var client = new HttpClient())
                {
                    using (var stream = await client.GetInputStreamAsync(imageUri))
                    {
                        using (var memstream = new InMemoryRandomAccessStream())
                        {
                            await stream.AsStreamForRead().CopyToAsync(memstream.AsStreamForWrite());

                            BitmapDecoder decoder = await BitmapDecoder.CreateAsync(memstream);


                            file = await IllustrationFolder.CreateFileAsync(fileName);

                            using (var targetStream = await file.OpenAsync(FileAccessMode.ReadWrite))
                            {
                                BitmapEncoder encoder = await BitmapEncoder.CreateForTranscodingAsync(targetStream, decoder);

                                var transform = CreateUniformToFillTransform(new Size(decoder.PixelWidth, decoder.PixelHeight), imgSize);
                                encoder.BitmapTransform.InterpolationMode = BitmapInterpolationMode.Cubic;
                                encoder.BitmapTransform.ScaledHeight      = transform.ScaledHeight;
                                encoder.BitmapTransform.ScaledWidth       = transform.ScaledWidth;
                                encoder.BitmapTransform.Bounds            = transform.Bounds;
                                await encoder.FlushAsync();

                                //WriteableBitmap wbp = new WriteableBitmap(150,150);
                                //await wbp.SetSourceAsync(memstream);
                                //await wbp.SaveToFile(Current.IllustrationFolder, fileName);
                                return(localUri);
                            }
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                Debug.WriteLine(exception.Message);
            }
            return(null);
        }