开发者

Converting Bitmap to Binary Array performance

I do have a Bitmap Array that contains more than 500 Bitmaps object. I need to convert each single 开发者_开发知识库Bitmap object within the Array into a binary Array. I'm using the MemoryStream class to achieve this:

using (MemoryStream ms = new MemoryStream())
 {
    images[0].Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
    byte[] byteData = ms.ToArray();
}

I would like to know if there is other way to achieve this. I'm not sure how expensive is this process.

Thanks


I've done some speed tests and converting to ImageFormat.Bmp is the fastest. It doesn't need to do any compression. Though the best format will also depend on what you plan to do with the data after that.

It's also worth considering where the Bitmaps came from in the first place. If you're loading them in from a file it may be worth it to switch things around and read the file data in first, then create your Bitmap objects from it after that.


By choosing Gif you are making a CPU/memory trade off which you most likely don't want. Specifically the Gif is going to be smaller but is going to take some time to compress (unless the images are already in the format) relative to using a BMP.

If you are copying these around enough that you have memory bandwidth issues (and can't fix that) this is a good idea, but otherwise you should stick with BMP. Really though, for 500 images I would expect this to take 1-2 seconds at the most so you probably don't need to worry about this sort of micro optimization. If its taking to long you can move to unmanaged code which will likely perform better because you will have finer control over memory allocations and copies.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜