c# - 保存图像文件和节省内存的最佳方式

标签 c# .net image byte compression

在我的程序中,我正在创建一些大图片(Image 对象),并将它们保存到磁盘。然后我将它们添加到列表 List<Image>但在保存 50 张图片并将它们添加为 Image 之后反对我的imageList它占用了大量内存。我尝试在 50 张图像上执行此操作并仅保存纯图像对象,我的程序在进程管理器中增加到 160 MB。所以我必须找到一种方法来保存图片并将它们添加到列表中,而不会让程序占用所有内存。

所以我有几个解决方案,我很想听听您对它们的看法,或者您是否有更好的解决方案。

  1. 压缩byte[]图像对象的数组。
  2. 压缩 memorysteam 对象的流。
  3. 转换 byte[]图像对象的数组到字符串,然后压缩字符串。

我在 C# 中执行此操作。

最佳答案

不要使用 .Net DeflateStream 等压缩图像(因为存在一个已知问题,在所有情况下它实际上都会增加数据的大小)- 将其直接保存为 .png 之类的文件。

Bitmap bmp;
// ...
bmp.Save("foo.png", System.Drawing.Imaging.ImageFormat.Png);

确保处置您创建的图像(在保存它们之后)。

您不能压缩内存中的图像 - 因为 Windows GDI(.Net 使用)要求图像本质上是未压缩的位图形式(因此当您加载压缩图像时,它会被解压缩)。

您应该考虑按需加载它们。这是一个类似于 ImageList 的类,您可能会发现它很有用:

public class DelayedImagedList : Component
{
    // Item1 = Dispose for the image.
    // Item2 = At creation: the method to load the image. After loading: the method to return the image.
    // Item3 = The original filename.
    private List<Tuple<Action, Func<Image>, string>> _images = new List<Tuple<Action,Func<Image>,string>>();
    private Dictionary<string, int> _imageKeyMap = new Dictionary<string, int>();

    // Access images.
    public Image this[string key] { get { return _images[_imageKeyMap[key]].Item2(); } }
    public Image this[int index] { get { return _images[index].Item2(); } }
    public int Count { get { return _images.Count; } }

    // Use this to add an image according to its filename.
    public void AddImage(string key, string filename) { _imageKeyMap.Add(key, AddImage(filename)); }
    public int AddImage(string filename)
    {
        var index = _images.Count;
        _images.Add(Tuple.Create<Action, Func<Image>, string>(
            () => {}, // Dispose
            () => // Load image.
            {
                var result = Image.FromFile(filename);
                // Replace the method to load the image with one to simply return it.
                _images[index] = Tuple.Create<Action, Func<Image>, string>(
                    result.Dispose, // We need to dispose it now.
                    () => result, // Just return the image.
                    filename);
                return result;
            }, 
            filename));
        return index;
    }

    // This will unload an image from memory.
    public void Reset(string key) { Reset(_imageKeyMap[key]); }
    public void Reset(int index)
    {
        _images[index].Item1(); // Dispose the old value.
        var filename = _images[index].Item3;

        _images[index] = Tuple.Create<Action, Func<Image>, string>(
            () => { }, 
            () =>
            {
                var result = Image.FromFile(filename);
                _images[index] = Tuple.Create<Action, Func<Image>, string>(
                    result.Dispose, 
                    () => result, 
                    filename);
                return result;
            }, 
            filename);
    }

    // These methods are available on ImageList.
    public void Draw(Graphics g, Point pt, int index) { g.DrawImage(this[index], pt); }
    public void Draw(Graphics g, int x, int y, int index) { g.DrawImage(this[index], x, y); }
    public void Draw(Graphics g, int x, int y, int width, int height, int index) { g.DrawImage(this[index], x, y, width, height); }

    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            foreach (var val in _images) { val.Item1(); }
            _images.Clear();
            _imageKeyMap.Clear();
        }
        base.Dispose(disposing);
    }
}

关于c# - 保存图像文件和节省内存的最佳方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7114198/

相关文章:

PHP imagecreate() PNG 来自任何常用的文件格式?

html - 带有 SVG Firefox 的元素 img

C# 拖放 : Show the dragged item while dragging

c# - 从另一个列表中删除一个对象列表

c# - 从派生类在 powershell 中构建自定义类型

C# MEF 不能做类型绑定(bind)?

c# - Win 7 在没有 qtcore4.dll 的情况下 Winforms 崩溃

c# - 无法在 VS 2017 15.5.7 中添加服务引用

c# - 复制文件夹并保留权限

image - 从图像 URL 加载图像需要很长时间才能显示