問題描述
我正在做一些大學(xué)項目,但遇到了內(nèi)存問題.我加載了一個位圖,它在硬盤上占用了大約 1.5GB 的空間,代碼如下:
I'm working on some university project and got stuck with memory issue. I load a bitmap which takes about 1,5GB on HDD with code below:
Bitmap bmp = new Bitmap(pathToFile);
問題是新創(chuàng)建的 Bitmap 對象使用了大約 3.5GB 的 RAM,這是我無法理解的(這真是大包裝:E).我需要訪問像素數(shù)組,Bitmap 類的使用真的很有幫助(我稍后使用 LockBits() 方法,并處理每個字節(jié)的數(shù)組字節(jié))但在這種情況下它完全是阻塞器.所以這是我的問題:
The issue is that the newly created Bitmap object uses about 3,5GB of RAM which is something I can't understand (that's really BIG wrapper :E). I need to get to the pixel array, and the use of Bitmap class is really helpful (I use LockBits() method later, and process the array byte per byte) but in this case it's total blocker. So here is my question:
有沒有什么簡單的方法可以在不借出額外 2gb 的情況下提取像素陣列?
Is there any easy way to extract the pixel array without lending additional 2gb?
我使用 c# 只是為了提取所需的數(shù)組,稍后在 c++ 中處理 - 也許我可以在 c++ 中提取所有需要的數(shù)據(jù)(但這里出現(xiàn)轉(zhuǎn)換問題 - 我專注于 24bgr 格式)?
I'm using c# just to extract the needed array, which is later processed in c++ - maybe I can extract all needed data in c++ (but conversion issue appears here - I'm concentrating on 24bgr format)?
PS:我需要將整個位圖保留在內(nèi)存中,因此將其拆分為多個部分不是解決方案.
PS: I need to keep the whole bitmap in memory so splitting it into parts is no solution.
PS2:只是澄清一些問題:我知道文件擴展名和文件格式之間的區(qū)別.加載的文件是未壓縮的位圖,每像素 3 個字節(jié),大小為 ~1.42GB(16k x 32k 像素),那么為什么 Bitmap 對象要大兩倍以上?沒有發(fā)生任何解壓問題和轉(zhuǎn)換為其他格式的問題.
PS2: Just to clarify some issues: I know the difference between file extension and file format. The loaded file is uncompressed bitmap 3 bytes per pixel of size ~1.42GB (16k x 32k pixels), so why Bitmap object is more than two times bigger? Any decompressing issues and converting into other format aren't taking place.
推薦答案
考慮使用內(nèi)存映射文件來訪問您的巨大數(shù)據(jù):).可以在此處找到專注于您需要的示例:http:///visualstudiomagazine.com/articles/2010/06/23/memory-mapped-files.aspx它在托管代碼中,但您也可以從等效的本機代碼中使用它.
Consider using Memory Mapped Files to access your HUGE data :). An example focused on what you need can be found here: http://visualstudiomagazine.com/articles/2010/06/23/memory-mapped-files.aspx It's in managed code but you might as well use it from equivalent native code.
如果您需要更多詳細信息,請告訴我.
Let me know if you need more details.
這篇關(guān)于處理大型位圖(最大 3GB)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!