GetImage() public method

public GetImage ( string label ) : System.Windows.Media.Imaging.BitmapSource
label string
return System.Windows.Media.Imaging.BitmapSource
示例#1
0
        /// <summary>
        /// Load all previously saved resources from the sbres file.
        /// ImageList and sounds are all auto loaded ready for use.
        /// Sound files are re-created in your %temp% folder since they are required by Sound.Play.
        /// </summary>
        /// <returns>"SUCCESS" or "FAILED".</returns>
        public static Primitive Load()
        {
            try
            {
                if (!System.IO.File.Exists(resourcesFile))
                {
                    return("FAILED");
                }
                Stream          fs         = System.IO.File.Open(resourcesFile, FileMode.Open, FileAccess.Read);
                BinaryFormatter bformatter = new BinaryFormatter();
                resources = (Resources)bformatter.Deserialize(fs);
                fs.Close();

                Type GraphicsWindowType = typeof(GraphicsWindow);
                Type ShapesType         = typeof(Shapes);
                Type ImageListType      = typeof(ImageList);
                Type SoundType          = typeof(Sound);
                Dictionary <string, BitmapSource> _savedImages;
                Dictionary <Uri, MediaPlayer>     _mediaPlayerMap;
                int i;

                _savedImages    = (Dictionary <string, BitmapSource>)ImageListType.GetField("_savedImages", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.IgnoreCase).GetValue(null);
                _mediaPlayerMap = (Dictionary <Uri, MediaPlayer>)SoundType.GetField("_mediaPlayerMap", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.IgnoreCase).GetValue(null);

                texts = "";
                i     = 1;
                foreach (string text in resources.texts)
                {
                    texts[i++] = text;
                }

                files = "";
                i     = 1;
                foreach (KeyValuePair <string, byte[]> kvp in resources.files)
                {
                    files[i++] = kvp.Key;
                }

                InvokeHelper ret = new InvokeHelper(delegate
                {
                    try
                    {
                        images = "";
                        i      = 1;
                        foreach (KeyValuePair <string, byte[]> kvp in resources.images)
                        {
                            BitmapSource bitmapSource = resources.GetImage(kvp.Key);
                            if (null != bitmapSource)
                            {
                                _savedImages[kvp.Key] = bitmapSource;
                                images[i++]           = kvp.Key;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Utilities.OnError(Utilities.GetCurrentMethod(), ex);
                    }
                });
                FastThread.Invoke(ret);

                sounds = "";
                i      = 1;
                foreach (KeyValuePair <string, byte[]> kvp in resources.sounds)
                {
                    string fileName = resources.GetSound(kvp.Key);
                    if (null != fileName)
                    {
                        Uri         uri         = new Uri(fileName);
                        MediaPlayer mediaPlayer = new MediaPlayer();
                        mediaPlayer.Open(uri);
                        _mediaPlayerMap[uri] = mediaPlayer;
                        sounds[i++]          = fileName;
                    }
                }
                return("SUCCESS");
            }
            catch (Exception ex)
            {
                Utilities.OnError(Utilities.GetCurrentMethod(), ex);
                return("FAILED");
            }
        }
示例#2
0
        /// <summary>
        /// Load all previously saved resources from the sbres file.
        /// ImageList and sounds are all auto loaded ready for use.
        /// Sound files are re-created in your %temp% folder since they are required by Sound.Play.
        /// </summary>
        /// <returns>"SUCCESS" or "FAILED".</returns>
        public static Primitive Load()
        {
            try
            {
                if (!System.IO.File.Exists(resourcesFile)) return "FAILED";
                Stream fs = System.IO.File.Open(resourcesFile, FileMode.Open, FileAccess.Read);
                BinaryFormatter bformatter = new BinaryFormatter();
                resources = (Resources)bformatter.Deserialize(fs);
                fs.Close();

                Type GraphicsWindowType = typeof(GraphicsWindow);
                Type ShapesType = typeof(Shapes);
                Type ImageListType = typeof(ImageList);
                Type SoundType = typeof(Sound);
                Dictionary<string, BitmapSource> _savedImages;
                Dictionary<Uri, MediaPlayer> _mediaPlayerMap;
                int i;

                _savedImages = (Dictionary<string, BitmapSource>)ImageListType.GetField("_savedImages", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.IgnoreCase).GetValue(null);
                _mediaPlayerMap = (Dictionary<Uri, MediaPlayer>)SoundType.GetField("_mediaPlayerMap", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.IgnoreCase).GetValue(null);

                texts = "";
                i = 1;
                foreach (string text in resources.texts)
                {
                    texts[i++] = text;
                }

                files = "";
                i = 1;
                foreach (KeyValuePair<string, byte[]> kvp in resources.files)
                {
                    files[i++] = kvp.Key;
                }

                InvokeHelper ret = new InvokeHelper(delegate
                {
                    try
                    {
                        images = "";
                        i = 1;
                        foreach (KeyValuePair<string, byte[]> kvp in resources.images)
                        {
                            BitmapSource bitmapSource = resources.GetImage(kvp.Key);
                            if (null != bitmapSource)
                            {
                                _savedImages[kvp.Key] = bitmapSource;
                                images[i++] = kvp.Key;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Utilities.OnError(Utilities.GetCurrentMethod(), ex);
                    }
                });
                FastThread.Invoke(ret);

                sounds = "";
                i = 1;
                foreach (KeyValuePair<string, byte[]> kvp in resources.sounds)
                {
                    string fileName = resources.GetSound(kvp.Key);
                    if (null != fileName)
                    {
                        Uri uri = new Uri(fileName);
                        MediaPlayer mediaPlayer = new MediaPlayer();
                        mediaPlayer.Open(uri);
                        _mediaPlayerMap[uri] = mediaPlayer;
                        sounds[i++] = fileName;
                    }
                }
                return "SUCCESS";
            }
            catch (Exception ex)
            {
                Utilities.OnError(Utilities.GetCurrentMethod(), ex);
                return "FAILED";
            }
        }