Пример #1
0
        public override void GetImages(GraphicsDevice graphicsDevice, List <IImageSource[]> textureSetListToAddTo, ref bool stop)
        {
            string debugInfoPictureFileName = "";

            var tempTextures             = new List <Texture2D[]>();
            List <IImageSource> textures = new List <IImageSource>();

            WebRequest request = System.Net.HttpWebRequest.Create(_urlToOpen);

            WebRequest.DefaultWebProxy.Credentials = CredentialCache.DefaultNetworkCredentials;
            var response = (HttpWebResponse)request.GetResponse();

            using (ZipArchive archive = new ZipArchive(response.GetResponseStream()))
            {
                foreach (var entry in archive.Entries)
                {
                    if (stop)
                    {
                        return;
                    }
                    if (entry.Name.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase) ||
                        entry.Name.EndsWith(".png", StringComparison.OrdinalIgnoreCase) ||
                        entry.Name.EndsWith(".gif", StringComparison.OrdinalIgnoreCase))
                    {
                        try
                        {
                            debugInfoPictureFileName = entry.FullName;
                            using (Stream fileStream = entry.Open())
                            {
                                IImageSource source = ImageSourceFactory.CreateSourceFromStream(fileStream, entry.FullName);
                                textures.Add(source);
                                OnTextureLoaded(source.CurrentTexture);
                            }
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(string.Format("Error loading image '{0}' from zipfile at URL '{1}'. Error is: {2}", debugInfoPictureFileName, _urlToOpen, ex.Message));
                        }
                    }
                }
            }
            if (textures.Count > 0)
            {
                textureSetListToAddTo.Add(textures.ToArray());
            }
        }
Пример #2
0
        public override void GetImages(GraphicsDevice graphicsDevice, List <IImageSource[]> textureSetListToAddTo, ref bool stop)
        {
            List <string> zipFilesToOpen           = new List <string>();
            string        debugInfoZipFileName     = "";
            string        debugInfoPictureFileName = "";

            try
            {
                string imageFolder = null;
                if (!string.IsNullOrWhiteSpace(_itemToOpen))
                {
                    if (Path.GetExtension(_itemToOpen) == ".zip")
                    {
                        zipFilesToOpen.Add(_itemToOpen);
                    }
                    else if (Directory.Exists(_itemToOpen))
                    {
                        imageFolder    = _itemToOpen;
                        zipFilesToOpen = Directory.EnumerateFiles(imageFolder, "*.zip").ToList();
                    }
                }
                else
                {
                    var runningFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                    imageFolder = ConfigurationManager.AppSettings[AppSettingsKey] ?? runningFolder;
                    if (Directory.Exists(imageFolder))
                    {
                        zipFilesToOpen = Directory.EnumerateFiles(imageFolder, "*.zip").ToList();
                    }
                }
                var tempTextures = new List <Texture2D[]>();

                for (int i = 0; i < zipFilesToOpen.Count(); i++)
                {
                    List <IImageSource> textures = new List <IImageSource>();
                    debugInfoZipFileName = zipFilesToOpen[i];
                    using (ZipArchive archive = ZipFile.OpenRead(zipFilesToOpen[i]))
                    {
                        foreach (var entry in archive.Entries)
                        {
                            if (stop)
                            {
                                return;
                            }
                            if (entry.Name.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase) ||
                                entry.Name.EndsWith(".gng", StringComparison.OrdinalIgnoreCase) ||
                                entry.Name.EndsWith(".gif", StringComparison.OrdinalIgnoreCase))
                            {
                                try
                                {
                                    debugInfoPictureFileName = entry.FullName;
                                    using (Stream fileStream = entry.Open())
                                    {
                                        IImageSource source = ImageSourceFactory.CreateSourceFromStream(fileStream, entry.FullName);
                                        textures.Add(source);
                                        OnTextureLoaded(source.CurrentTexture);
                                    }
                                }
                                catch (Exception ex)
                                {
                                    Console.WriteLine(string.Format("Error loading image '{0}' from file '{1}'. Error is: {2}", debugInfoPictureFileName, debugInfoZipFileName, ex.Message));
                                }
                            }
                        }
                    }
                    if (textures.Count > 0)
                    {
                        textureSetListToAddTo.Add(textures.ToArray());
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("Error loading image '{0}' from file '{1}'. Error is: {2}", debugInfoPictureFileName, debugInfoZipFileName, ex.ToString()), ex);
            }
        }