Region requires much memory !?

  • Thread starter Thread starter Martin Wolff
  • Start date Start date
M

Martin Wolff

Hi !!

To draw multiple graphics objects which have different shapes, I’m using
the region class to exclude/union the drawing area of these Objects.

The rectangle around a single object is of the size of 80x80 pixels. So
looping through these pixels and excluding the area of every transparent
pixel, the region will be built.

Afterwards, such an object has a memory usage of approx. 700Kb (before
modifying the region it was nearly 300kb).

So can anybody tell me, how to provide Objects with transparent
background and also keeping the memory usage low.

A weblink to more information about this region class would also be
helpful.

Thanks in advance,

Martin.


PS: Special thanks to Tom Overbay who provided me with the first code to
create user icons.



Here is a code snippet to show, what the program does:

For y = 0 To MyBitmap.Height - 1
For x = 0 To MyBitmap.Width - 1
' Exclude alpha pixels
If MyBitmap.GetPixel(x, y).A = 0 Then
MyRegion.Exclude(New Rectangle(x, y, 1, 1))
End If
Next
Next
..
..
 
Hello,

Martin Wolff said:
The rectangle around a single object is of the size
of 80x80 pixels. So looping through these pixels and excluding
the area of every transparent pixel, the region will be built.

Afterwards, such an object has a memory usage of approx.
700Kb (before modifying the region it was nearly 300kb).

You are right -- "complex" regions need a lot of memory.
So can anybody tell me, how to provide Objects
with transparent background and also keeping the
memory usage low.

Sorry, I don't have a sample, but why not drawing everything onto one
control? You can safe parts of the image in separate buffers and restore
them when moving objects.
 
Herfried,

thank you for your fast answer and your suggestion. But I think that
won't be the fine OOP way.
I prefer to have the objects doing their stuff themself. The handling
will be much easier when copying or deleting such a symbol, if they have
all the information close to them.

Will the memory usage go down, if I combine empty pixels to a bigger
rectangle before excluding them ?

Thanks in advance,

regards,
Martin.
 
Martin,
In addition to Herfried's comments.

Would it be easier to define the 'shape' in terms of a GraphicsPath object?

Rather then checking bits on the bitmap, draw the shape onto the
GraphicsPath to begin with?

Or are you actually reading the bitmaps from files rather than creating
them?

According to Charles Petzold in "Programming Microsoft Windows with
Microsoft Visual Basic.NET"

"With the introduction of paths, regions have become much
less important in Windows graphics programming.
They might even be ignored altogether if not for the role
they play in clipping"

I normally start with a GraphicsPath then convert it into a Region, only
when I need it as a Region. For the clipping region for example.

Does the Clone method of the Region return a more compact Region?

Hope this helps
Jay
 
Hi Martin,

I'm confused by your numbers. An 80 x 80 x 4 (true colour) image is only
25600 bytes. How do you get to the 300K region, let alone 700K?? Was 80x80 a
type?

One thing is certain however, your current method is very costly per
pixel. It creates a 1x1 Rectangle (16 bytes) and adds that to a Region.
Assuming that the Region doesn't merge its rectangles, it needs to store its
Rectangles in some data structure. This would need memory allocation requiring
at least another 8 bytes, probably more. All this is per pixel!

Jay's idea of using a GraphicsPath would make it much more efficient
(assuming, of course, that much of these excluded pixels are contiguous).

If you are starting with a bitmap, there are a number of methods for
creating larger regions, and for creating completely enclosed areas, suitable
for representation using a GraphicsPath.

At the very least, rather than adding pixels to the region separately, you
could do horizontal scans and collect runs of pixels. A further step would be
to take a series of such runs (line 1, line 2, etc), and determine whether and
how they overlap vertically. These could then be converted to blocks plus bits
of runs, or, better, into a single GraphicsPath region.

Come back if you need more help on this. :-)

Regards,
Fergus
 
Thank you Jay and Fergus,

I will try to use GraphicsPath instead of the Region, if this will
provide me a transparency too.

Regards,

Martin.
 
Back
Top