示例#1
0
        public static void GenerateIconImage(SoundPlayerInfo bgm, bool force)
        {
            string folder   = "playericon";
            string fileName = folder + @"\" + bgm.Id + ".png";

            System.Drawing.Bitmap bitmap;

            try
            {
                if (bgm.Name == "System Sounds")
                {
                    if (_iconContents.TryGetValue(@"mixer\windows.png", out bitmap) == false)
                    {
                        string tempFileName = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + @"\mixer\windows.png";
                        if (System.IO.File.Exists(tempFileName))
                        {
                            bitmap = (System.Drawing.Bitmap)System.Drawing.Bitmap.FromFile(tempFileName);
                            _iconContents[@"mixer\windows.png"] = bitmap;
                        }
                    }
                }
                else
                {
                    System.Drawing.Icon icon = null;
                    int x = 0;
                    if (bgm.IconPath != "")
                    {
                        x = x + 1;
                    }
                    string path = bgm.IconPath != "" ? bgm.IconPath : bgm.UrlOrCommandLine;
                    if ((bgm.IconPath == "") && (bgm.UrlOrCommandLine.StartsWith("http://")))
                    {
                        path = @"http://getfavicon.appspot.com/" + path;
                    }
                    if ((bgm.IconPath == "") && (bgm.UrlOrCommandLine.StartsWith("https://")))
                    {
                        path = @"https://getfavicon.appspot.com/" + path;
                    }

                    if (_iconContents.TryGetValue(path, out bitmap) == false)
                    {
                        if ((path.ToLower().StartsWith("http://")) || (path.ToLower().StartsWith("https://")))
                        {
                            string tempFile = System.IO.Path.GetTempFileName();
                            var    client   = new System.Net.WebClient();
                            client.DownloadFile(path, tempFile);
                            icon = System.Drawing.Icon.ExtractAssociatedIcon(tempFile);
                            System.IO.File.Delete(tempFile);
                        }
                        else
                        {
                            if (System.IO.File.Exists(path))
                            {
                                icon = GetIconOldSchool(path);
                            }
                            //icon = System.Drawing.Icon.ExtractAssociatedIcon(path);
                        }
                        if (icon != null)
                        {
                            bitmap = icon.ToBitmap();
                        }
                        _iconContents[path] = bitmap;
//                        bitmap.Save("C://foo.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
                    }
                }
                if ((force) || ((bitmap != null) && (WebServer.RetrieveFile(fileName) == null)))
                {
                    byte[] result;
                    using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
                    {
                        bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
                        result = stream.ToArray();
                    }
                    WebServer.StoreFile(fileName, result);
                }

//                if (bitmap == null)
//                    bgm.Enabled = false;
            }
            catch (Exception ex)
            {
                MuteFm.SmartVolManagerPackage.SoundEventLogger.LogMsg("Error generating icon file for bgmusic " + bgm.GetName());
                MuteFm.SmartVolManagerPackage.SoundEventLogger.LogException(ex);
//                bgm.Enabled = false;
            }
        }