C#使用字典处理和重新搜索/加载位图



我编写了一个程序,使用一些图片资源并从中创建一个文件。作为客户预览,在使用图像之前会显示这些图像。因此,我需要先处理它们,然后才能使用它们。但我想在之后再次加载预览,因为这可能是大量的图片,我想动态地进行,所以我失败了。

我使用一个文件名为String:的字典

            //Creating a Bitmap so we can preview the Picture.
        try
        {
            mainDrawingBackgroundBitmap = new Bitmap(filePathBackgroundBackgroundImage);
            if(mainAllBitmapsDictionary.ContainsKey(mainDrawingBackgroundBitmap))
            {
                mainAllBitmapsDictionary.Remove(mainDrawingBackgroundBitmap);
            }     
            mainAllBitmapsDictionary.Add(mainDrawingBackgroundBitmap,filePathBackgroundBackgroundImage);
        }

现在我的位图和路径都在字典里了,以后我用这种方式处理它们:

        private void DisposeAllFiles()
    {
        foreach (var tempBitmap in mainAllBitmapsDictionary.Keys)
        {
            tempBitmap.Dispose();
        }
    }

这很好用。现在,当我尝试重新创建位图时:

        private void RessurrectAllFiles()
    {
        foreach (var tempAllBitmap in mainAllBitmapsDictionary)
        {
            try
            {
                var tempBitmap = tempAllBitmap.Key;
                tempBitmap = new Bitmap(tempAllBitmap.Value);
            }
            catch (ArgumentException ae)
            {
            }
        }
    }

他没有失败或抛出错误,字典甚至充满了正确的位图和字符串,但这些似乎不再影响原始对象,所以字典是原样的,但当我检查像:mainDrawingBackgroundBitmap这样的位图时,我只看到ArgumentExceptions。

坦率地说,我在哪里失败?

将图像的路径作为键,将位图数据作为值,这样您就可以很容易地操作数据,搜索字典也会执行得很快。

Dictionary<string, Bitmap>

最新更新