syn*_*cis 5 .net c# compression byte image
在我的程序中,我正在创建一些大图片(Image对象),并将它们保存到磁盘.然后我将它们添加到一个列表中List<Image>但是在保存了50张图片并将它们作为Image对象添加到我的文件后,imageList它会占用内存的loooooot.我尝试在50个图像上执行此操作,只保存纯图像对象,我的程序在进程管理器中上升到160 MB.所以我必须找到一种方法来保存图片并将它们添加到列表中,而不会占用所有内存.
所以我有几个解决方案,我很想听听你对它们的看法,或者你有更好的解决方案.
byte[]图像对象的数组.byte[]图像对象的数组转换为字符串,然后压缩字符串.我在c#中这样做.
Jon*_*son 10
不要使用.Net DeflateStream等压缩图像(因为在所有情况下都存在实际增加数据大小的已知问题) - 将其直接保存为类似.png的内容.
Bitmap bmp;
// ...
bmp.Save("foo.png", System.Drawing.Imaging.ImageFormat.Png);
Run Code Online (Sandbox Code Playgroud)
确保丢弃您创建的图像(保存后).
您无法在内存中压缩图像 - 因为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);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5684 次 |
| 最近记录: |