Boxing/Deboxing Bitmaps Problem

G

Guest

I am using Microsoft Visual Basic .net 2002 Standard. I am coding an application which loads in data from a compressed file. One of the items in the file is a raw RGB565 bitmap, at 800x600 pixels. In a previous version of my app, I used Bitmap.SetPixel() but performance got to be an issue as it took in some cases nearly 30 additional seconds to load the file. I am trying to use Bitmap.LockBits() in combination with Marshall.PtrToStruct() to map a two-dimensional array of Short over the actual bits in the bitmap. Unfortunately, Marshall.PtrToStruct() requires a well-formatted structure, and an array of anything is not acceptable. Thus, I attempted to create a class with 480,000 Short variables and a Property to access them in a manner similar to that I'd use if I were using an array. However, when I tried to load it in the IDE, my swap file broke through 2GB and went on climbing, from 200MB. Attempts to compile the 40MB source code beast by hand have been thwarted by a stack overflow in VBC, and I can't seem to find any option to increase VBC's stack size.

So, how do I manually get access to the bits in a bitmap, given the above troubles?
 
B

Bob Powell [MVP]

The GDI+ FAQ has information on using LockBits.

I would suggest an iterative approach to mapping your data to the pixels in
the bitmap. Marshal.PtrTStructure will not be able to cope with the gaps in
the bitmap buffer that represent the stride padding in the BitmapData oject.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Image transition effects, snap-to-grid and Layered Windows are
all discussed in May's edition of Well Formed for C# or VB programmers
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

The GDI+ FAQ RSS feed: http://www.bobpowell.net/faqfeed.xml
Windows Forms Tips and Tricks RSS: http://www.bobpowell.net/tipstricks.xml
Bob's Blog: http://bobpowelldotnet.blogspot.com/atom.xml






Andrew Cook said:
I am using Microsoft Visual Basic .net 2002 Standard. I am coding an
application which loads in data from a compressed file. One of the items in
the file is a raw RGB565 bitmap, at 800x600 pixels. In a previous version of
my app, I used Bitmap.SetPixel() but performance got to be an issue as it
took in some cases nearly 30 additional seconds to load the file. I am
trying to use Bitmap.LockBits() in combination with Marshall.PtrToStruct()
to map a two-dimensional array of Short over the actual bits in the bitmap.
Unfortunately, Marshall.PtrToStruct() requires a well-formatted structure,
and an array of anything is not acceptable. Thus, I attempted to create a
class with 480,000 Short variables and a Property to access them in a manner
similar to that I'd use if I were using an array. However, when I tried to
load it in the IDE, my swap file broke through 2GB and went on climbing,
from 200MB. Attempts to compile the 40MB source code beast by hand have been
thwarted by a stack overflow in VBC, and I can't seem to find any option to
increase VBC's stack size.
So, how do I manually get access to the bits in a bitmap, given the above
troubles?
 
G

Guest

Thanks, Mr. Powell, the iterative approach works fine, as far as I can tell, and the load time for the files has gone down exponentially. A perfect solution which does you much well-deserved credit.

But now I have another bitmap-related problem.

After getting all the information from the input file, my program renders all the information to a bitmap, which is then displayed on the screen in a PictureBox control. However, when I try to create a Graphics obeject for the Bitmap, I get an OutOfMemoryException originating in system.drawing.dll, in the exact same place as I got it in the previous version of my program. I don't quite understand why; even though I'm holding on to two other very large bitmaps and several other amounts of data at the time, I've got over a gig free in my paging file with room to expand, and it fails there on other, more advanced machines as well. GC.GetTotalMemory reports 1196440, or about 1.14 megabytes, but it isn't dealing with native heap. Task Manager reports 29,778KB in use. Having no experience in unmanaged programming has left me clueless as to where to go next.
 
B

Bob Powell [MVP]

Does the bitmap use a colour palette? You can't get a graphics for a bitmap
with a palette.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Image transition effects, snap-to-grid and Layered Windows are
all discussed in May's edition of Well Formed for C# or VB programmers
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

The GDI+ FAQ RSS feed: http://www.bobpowell.net/faqfeed.xml
Windows Forms Tips and Tricks RSS: http://www.bobpowell.net/tipstricks.xml
Bob's Blog: http://bobpowelldotnet.blogspot.com/atom.xml






Andrew Cook said:
Thanks, Mr. Powell, the iterative approach works fine, as far as I can
tell, and the load time for the files has gone down exponentially. A perfect
solution which does you much well-deserved credit.
But now I have another bitmap-related problem.

After getting all the information from the input file, my program renders
all the information to a bitmap, which is then displayed on the screen in a
PictureBox control. However, when I try to create a Graphics obeject for the
Bitmap, I get an OutOfMemoryException originating in system.drawing.dll, in
the exact same place as I got it in the previous version of my program. I
don't quite understand why; even though I'm holding on to two other very
large bitmaps and several other amounts of data at the time, I've got over a
gig free in my paging file with room to expand, and it fails there on other,
more advanced machines as well. GC.GetTotalMemory reports 1196440, or about
1.14 megabytes, but it isn't dealing with native heap. Task Manager reports
29,778KB in use. Having no experience in unmanaged programming has left me
clueless as to where to go next.
 
B

Bob Powell [MVP]

I beleive that you can only obtain a Graphics object for 32 or 24 bit
images. I will verify this and get back in this thread.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Image transition effects, snap-to-grid and Layered Windows are
all discussed in May's edition of Well Formed for C# or VB programmers
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

The GDI+ FAQ RSS feed: http://www.bobpowell.net/faqfeed.xml
Windows Forms Tips and Tricks RSS: http://www.bobpowell.net/tipstricks.xml
Bob's Blog: http://bobpowelldotnet.blogspot.com/atom.xml






Andrew Cook said:
No, it doesn't use a colour palette. It's PixelFormat.Format16bppRgb565. I
get a different exception when I try to use a paletted bitmap.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Using bitmaps 3
Processing bitmaps 3
disappearing bitmaps in worksheet 3
Experience with WinCE6 and Bitmaps from file 0
bytes to io.stream 1
Bitmap drawing 6
Adding bitmaps to project 6
DLL,MFC,Bitmaps 1

Top