public async Task<SquareShape> GetSquareInfoAsync (SquareShape input) { HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create (new Uri (WebServiceAPI + "patterns/random")); request.ContentType = "application/json"; request.Method = "GET"; using (WebResponse response = await request.GetResponseAsync ()) { using (StreamReader reader = new StreamReader (response.GetResponseStream ())) { try { var content = reader.ReadToEnd (); if (string.IsNullOrWhiteSpace (content)) { return input; } else { var doc = XDocument.Parse (content); return doc.Root.Descendants ("pattern") .Select (x => new SquareShape () { Id = input.Id, ImagePath = x.Element ("imageUrl").Value, Center_X = input.Center_X, Center_Y = input.Center_Y, Radius = input.Radius }).FirstOrDefault () ?? input; } } catch { return input; } } } }
public async Task<SquareShape> PutImageToCache (SquareShape squareShape) { SquareShape shapeInfo = null; if (string.IsNullOrEmpty (squareShape.ImagePath)) { shapeInfo = await _webServices.GetSquareInfoAsync (squareShape); } if (shapeInfo != null) { var bitmap = await GetImageBitmapFromUrl (shapeInfo.ImagePath); if (bitmap != null) _imageCache.Put (shapeInfo.Id.ToString (), bitmap); return shapeInfo; } else { return squareShape; } }
async Task PreloadSquareShapes () { if (_squareShapes == null) { _squareShapes = new Queue<SquareShape> (_imageService.ImageQueueCount); } while (_squareShapes.Count < _imageService.ImageQueueCount) { _shapeID++; var shape = new SquareShape (){ Id = _shapeID}; shape.Center_X = _randomSeed.Next (10, _maxWidth); shape.Center_Y = _randomSeed.Next (10, _maxHeight); shape.Radius = _randomSeed.Next (10, (_maxWidth + _maxHeight) / 8); var shapeInfo = await _imageService.PutImageToCache (shape); _squareShapes.Enqueue (shapeInfo); } }
public async Task<Bitmap> GetImageFromCache (SquareShape shape) { try { var bitmap = _imageCache.Get (shape.Id.ToString()) as Bitmap; if (bitmap == null) { bitmap = await GetImageBitmapFromUrl(shape.ImagePath); } return bitmap; } catch (Exception ex) { Debug.WriteLine (ex.ToString ()); } return null; }