Example #1
0
        private void saveImage(CachedImage img, bool jpeg)
        {
            var saveFileDialog1 = new SaveFileDialog();
            var split = jpeg ? img.ImageData.jpeg_url.Split('/') : img.ImageData.file_url.Split('/');
            var filename = split[split.Length - 1];
            var suffixSplit = filename.Split('.');
            var suffix = suffixSplit[suffixSplit.Length - 1];

            saveFileDialog1.Filter = string.Format("%1 files (*.%2)|*.%3|All files (*.*)|*.*", suffix.ToUpper(), suffix, suffix);
            saveFileDialog1.FilterIndex = 1;
            saveFileDialog1.FileName = filename;
            saveFileDialog1.RestoreDirectory = true;

            if (saveFileDialog1.ShowDialog().Value)
            {
                var myStream = saveFileDialog1.OpenFile();
                if (myStream != null && myStream.CanWrite)
                {
                    using (var client = new WebClient())
                    {
                        client.DownloadDataCompleted += (a, b) =>
                        {
                            if (b.Error == null)
                            {
                                myStream.Write(b.Result, 0, b.Result.Length);
                                myStream.Close();
                                Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() =>
                                        {
                                            ProgressBar.Value = 0;
                                            ProgressBar.Visibility = Visibility.Hidden;
                                        }));
                            }
                        };
                        client.DownloadProgressChanged += (a, b) => Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() =>
                                {
                                    ProgressBar.Value = b.ProgressPercentage;
                                }));

                        Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() =>
                                {
                                    ProgressBar.Value = 0;
                                    ProgressBar.Visibility = Visibility.Visible;
                                }));
                        var uri = new Uri(jpeg ? img.ImageData.jpeg_url : img.ImageData.file_url);
                        client.DownloadDataAsync(uri);
                    }
                }
            }
        }
Example #2
0
        private static void SetSource(CachedImage inst, String path)
        {
            try
            {
                var bitmapImage = new BitmapImage();
                var scale = new ScaleTransform(900 / inst.ImageData.jpeg_width, 600 / inst.ImageData.jpeg_height);

                bitmapImage.BeginInit();
                bitmapImage.CreateOptions = BitmapCreateOptions.IgnoreColorProfile;
                bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
                bitmapImage.UriSource = new Uri(path, UriKind.RelativeOrAbsolute);
                bitmapImage.EndInit();

                inst.Source = new TransformedBitmap(bitmapImage, scale);
                inst.onDownloadCompleted(inst, null);

            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine(e.ToString());
            }
        }
Example #3
0
        private void getImages()
        {
            if (!Directory.Exists("cache"))
                Directory.CreateDirectory("cache");

            Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() =>
                    {
                        ProgressBar.Visibility = Visibility.Hidden;
                        ProgressbarText.Text = "Contacting konachan...";
                        ProgressbarText.Visibility = Visibility.Visible;
                    }));

            var downloadString = "";
            var xml = "";
            using (var client = new WebClient())
            {
                try
                {
                    downloadString = "http://konachan.com/post.xml?limit=100&page=" + Page;
                    if (TagsManager.GetInstance().HasTags())
                        downloadString += "&tags=" + TagsManager.GetInstance().GetTagsAsString();
                    xml = client.DownloadString(downloadString);
                }
                catch (Exception)
                {
                    Logger.Log("Failed to download posts from " + downloadString, "ERROR");
                    return;
                }
            }

            var xmlDocument = new XmlDocument();
            xmlDocument.LoadXml(xml);

            XmlNodeList elements = xmlDocument.GetElementsByTagName("post");

            Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() =>
                    {
                        if (elements.Count > 0)
                        {
                            ProgressbarText.Text = "";
                            ProgressbarText.Visibility = Visibility.Hidden;
                            ProgressBar.Visibility = Visibility.Visible;
                        }
                        else
                        {
                            ProgressbarText.Text = "No images found.";
                            return;
                        }
                    }));

            foreach (XmlElement element in elements)
            {
                Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() =>
                        {
                            var post = new Post(element.Attributes);
                            var img = new CachedImage();

                            img.Init(post);
                            img.MouseDown += img_MouseDown;
                            Imgs.Add(post);
                            ImgListView.Items.Add(img);

                            ProgressBar.Value = (double)Imgs.Count - ((Page - 1) * 100.0);
                        }));
            }
            Page++;
            Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() =>
                    {
                        ProgressBar.Visibility = Visibility.Hidden;
                        ProgressBar.Value = 0;
                        SearchButton.IsEnabled = true;
                    }));
        }