HighTechTalks DotNet Forums  

key to speed - use byte array instead of writing to locked image bytes

Dotnet Framework (Drawing) microsoft.public.dotnet.framework.drawing


Discuss key to speed - use byte array instead of writing to locked image bytes in the Dotnet Framework (Drawing) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #11  
Old   
James Maeding
 
Posts: n/a

Default Re: key to speed - use byte array instead of writing to locked image bytes - 01-26-2007 , 06:41 PM






Bob, your site is so helpful, thanks for taking the time to organize and share all that stuff

"Bob Powell [MVP]" <bob (AT) _spamkiller_bobpowell (DOT) net>
Quote:
The VB language doesn't support the use of _unsafe pointers_ so the
fastest method of access is not available to VB programmers.

When using the Marshal class you don't use pointers, just indexes from a
given starting point.

If you check out the article on LockBits you'll find a fairly
comprehensive description of the various memory buffer layouts for each
of the major graphic formats. Whatever method of access you use the
physical layouts remain the same.

The apparent size of an image on disc may not have any correlation to
it's memory footprint. Images are decompressed to a rastar that holds
the whole image and so they will take up X*Y*bit-depth/8 bytes
regardless of the size on disc. A jpeg of a few hundred K may end up at
3-4-7 hundred megabytes after loading.

A 750 meg TIFF might be ok as long as it was a simple format but TIFF is
such a huge spec that the GDI+ readers can't handle every different
form. It would also depend on things like total available memory and
swap-file size.

There are no set rules i'm afraid.

Reply With Quote
  #12  
Old   
Frank Hileman
 
Posts: n/a

Default Re: key to speed - use byte array instead of writing to locked image bytes - 02-01-2007 , 06:30 PM






Hi James,

Here is the C# code, it works fine in VB since you don't need any pointers,
you just modify integers in the pixels array. Don't forget to Free the
handle when you are completely finished with the bitmap. The "pixels" array
is an array of uint (unsigned integer). You can use signed integer but it is
more tricky to deal with the top bit. You can access the blue channel with a
0xFF mask, green with 0xFF00, etc. Or you can use an array of bytes, but
that isn't as fast.

// 4 bytes per pixel, format: ARGB
pixels = new uint[width * height];

// if not pinned the GC can move around the array
handle = GCHandle.Alloc(pixels, GCHandleType.Pinned);
IntPtr pointer = Marshal.UnsafeAddrOfPinnedArrayElement(pixels, 0);
bitmap = new Bitmap(width , height, width * 4,
PixelFormat.Format32bppPArgb, pointer);

To access a pixel on the bitmap at x and y:

pixel at (x,y) = pixels[width * y + x]

Any modification to that array modifies the bitmap directly. No marshall or
copy needed.

Regards,
Frank Hileman

check out VG.net: http://www.vgdotnet.com
Animated vector graphics system
Integrated Visual Studio graphics editor

"James Maeding" <jmaeding (AT) nettaxi (DOT) com> wrote

Quote:
wow, can you point to any example code, I am intermediate at .net so that
will help me put things together.
I'll check out your site too.
thanks for the tip, this should be fun.

"Frank Hileman" <frankhil (AT) no (DOT) spamming.prodigesoftware.com
|>The fastest method is to avoid any copy of pixels. Since the Bitmap
|>constructor accepts a pointer to a pixel buffer, you only have to
convert a
|>managed array of uint or byte to an IntPtr using GCHandle.Alloc to
obtain a
|>pinned handle and Marshall.UnsafeAddrOfPinnedArrayElement to convert to
an
|>IntPtr. Modifying the array contents will modify the pixels/memory
within
|>the Bitmap directly. LockBits is not needed.
|
|>You don't need unsafe code or pointer arithmetic (pointers add no
speed),
|>and it is screaming fast, assuming your format is
|>PixelFormat.Format32bppPArgb.



Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.