public override bool Save(System.IO.Stream stream, PureImage image) { var ret = (GMapImage)image; if(ret.Img != null) { try { var e = new PngBitmapEncoder(); e.Frames.Add(BitmapFrame.Create(ret.Img as BitmapSource)); e.Save(stream); e = null; } catch { return false; } } else { return false; } return true; }
public override bool Save(Stream stream, GMap.NET.PureImage image) { GMapImage ret = image as GMapImage; bool ok = true; if (ret.Img != null) { // try png try { ret.Img.Save(stream, System.Drawing.Imaging.ImageFormat.Png); } catch { // try jpeg try { stream.Seek(0, SeekOrigin.Begin); ret.Img.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg); } catch { ok = false; } } } else { ok = false; } return(ok); }
public override bool Save(Stream stream, PureImage image) { if (image.Data != null) { image.Data.WriteTo(stream); image.Data.Position = 0; return(true); } return(false); }
/// <summary> /// gets image from tile server /// </summary> /// <param name="provider"></param> /// <param name="pos"></param> /// <param name="zoom"></param> /// <returns></returns> public PureImage GetImageFrom(GMapProvider provider, GPoint pos, int zoom, out Exception result) { PureImage ret = null; result = null; try { var rtile = new RawTile(provider.DbId, pos, zoom); // let't check memmory first if (UseMemoryCache) { var m = MemoryCache.GetTileFromMemoryCache(rtile); if (m != null) { if (GMapProvider.TileImageProxy != null) { ret = GMapProvider.TileImageProxy.FromArray(m); if (ret == null) { #if DEBUG Debug.WriteLine("Image disposed in MemoryCache o.O, should never happen ;} " + new RawTile(provider.DbId, pos, zoom)); if (Debugger.IsAttached) { Debugger.Break(); } #endif m = null; } } } } if (ret == null) { if (Mode != AccessMode.ServerOnly && !provider.BypassCache) { if (PrimaryCache != null) { // hold writer for 5s if (cacheOnIdleRead) { Interlocked.Exchange(ref readingCache, 5); } ret = PrimaryCache.GetImageFromCache(provider.DbId, pos, zoom); if (ret != null) { if (UseMemoryCache) { MemoryCache.AddTileToMemoryCache(rtile, ret.Data.GetBuffer()); } return(ret); } } if (SecondaryCache != null) { // hold writer for 5s if (cacheOnIdleRead) { Interlocked.Exchange(ref readingCache, 5); } ret = SecondaryCache.GetImageFromCache(provider.DbId, pos, zoom); if (ret != null) { if (UseMemoryCache) { MemoryCache.AddTileToMemoryCache(rtile, ret.Data.GetBuffer()); } EnqueueCacheTask(new CacheQueueItem(rtile, ret.Data.GetBuffer(), CacheUsage.First)); return(ret); } } } if (Mode != AccessMode.CacheOnly) { ret = provider.GetTileImage(pos, zoom); { // Enqueue Cache if (ret != null) { if (UseMemoryCache) { MemoryCache.AddTileToMemoryCache(rtile, ret.Data.GetBuffer()); } if (Mode != AccessMode.ServerOnly && !provider.BypassCache) { EnqueueCacheTask(new CacheQueueItem(rtile, ret.Data.GetBuffer(), CacheUsage.Both)); } } } } else { result = noDataException; } } } catch (Exception ex) { result = ex; ret = null; Debug.WriteLine("GetImageFrom: " + ex.ToString()); } return(ret); }
abstract public bool Save(Stream stream, PureImage image);
public abstract bool Save(Stream stream, PureImage image);
/// <summary> /// gets image from tile server /// </summary> /// <param name="type"></param> /// <param name="pos"></param> /// <param name="zoom"></param> /// <returns></returns> public PureImage GetImageFrom(MapType type, Point pos, int zoom, out Exception result) { PureImage ret = null; result = null; try { // let't check memmory first MemoryStream m = GetTileFromMemoryCache(new RawTile(type, pos, zoom)); if (m != null) { if (GMaps.Instance.ImageProxy != null) { ret = GMaps.Instance.ImageProxy.FromStream(m); if (ret == null) { m.Dispose(); } } } if (ret == null) { string url = MakeImageUrl(type, pos, zoom, LanguageStr); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Proxy = WebRequest.DefaultWebProxy; request.UserAgent = UserAgent; request.Timeout = Timeout; request.ReadWriteTimeout = Timeout * 6; request.Accept = "*/*"; request.Referer = "http://maps.google.com/"; Debug.WriteLine("Starting GetResponse: " + pos); using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { Debug.WriteLine("GetResponse OK: " + pos); Debug.WriteLine("Starting GetResponseStream: " + pos); MemoryStream responseStream = Stuff.CopyStream(response.GetResponseStream(), false); { Debug.WriteLine("GetResponseStream OK: " + pos); if (GMaps.Instance.ImageProxy != null) { ret = GMaps.Instance.ImageProxy.FromStream(responseStream); // Enqueue Cache if (ret != null) { AddTileToMemoryCache(new RawTile(type, pos, zoom), responseStream); } } } response.Close(); } } } catch (Exception ex) { result = ex; ret = null; Debug.WriteLine("GetImageFrom: " + ex.ToString()); } return(ret); }