Esempio n. 1
0
        public static T DeserializeUrl <T>(string url) where T : class
        {
            using (WebClientBase wc = new WebClientBase())
                using (Stream stream = wc.DownloadToStream(url))
                {
                    if (stream == null)
                    {
                        return(null);
                    }

                    using (StreamReader streamReader = new StreamReader(stream))
                        using (JsonReader reader = new JsonTextReader(streamReader))
                        {
                            JsonSerializer serializer = new JsonSerializer();
                            try
                            {
                                return(serializer.Deserialize <T>(reader));
                            }
                            catch
                            {
                                Log.WriteError("Deserializing of {0} from url {1} failed", typeof(T).ToString(), url);
                                return(null);
                            }
                        }
                }
        }
Esempio n. 2
0
        public static void GetWebImageDimensions(string url, Action <ImageInfo> callback)
        {
            webClient.KeepAlive = true;
            var fileSize = webClient.GetContentLength(url);

            if (fileSize > 0)
            {
                using (var stream = webClient.DownloadPartial(url, 0, 30))
                    using (var binaryReader = new BinaryReader(stream))
                    {
                        if (callback != null)
                        {
                            ImageInfo imageInfo = null;
                            try
                            {
                                imageInfo          = GetDimensions(binaryReader);
                                imageInfo.FileSize = fileSize;
                            }
                            catch
                            {
                                Log.WriteError("measuring image {0}", url);
                            }
                            callback(imageInfo ?? new ImageInfo());
                        }
                    }
            }
            else
            {
                using (Stream stream = webClient.DownloadToStream(url))
                {
                    Image testImage = Image.FromStream(stream);
                    var   imageInfo = new ImageInfo()
                    {
                        Format     = testImage.RawFormat,
                        Dimensions = testImage.Size,
                    };
                    callback(imageInfo);
                }
            }
        }