Home All Groups Group Topic Archive Search About

MemCpy revisited (in Unsafe code)

Author
25 Dec 2005 4:50 AM
Johannes Bialek
Hi,
Buffer.BlockCopy() and Array.Copy() seem do be fine for Arrays, but what to
do about copying memory in unsafe code?
The goal is simple, I just want to scroll the content of an Image vertically
for some lines.. (using the CompactFramework, but seems to be of general
interrest). So I moved byte-per-byte which is awfully slow :(

Code:
System.Drawing.Imaging.BitmapData bd = buffer.LockBits(all,
System.Drawing.Imaging.ImageLockMode.ReadWrite,
System.Drawing.Imaging.PixelFormat.Format16bppRgb555);
int cnt = ((buffer.Height - Math.Abs(lines)) * bd.Stride);
byte* src = (byte*)bd.Scan0.ToInt32() + lines * bd.Stride;
byte* dst = (byte*)bd.Scan0.ToInt32();

for (int i = 0; i < cnt; i++)
{
    *dst = *src;
    dst++;
    src++;
}

I really would like to see this for-loop removed by some "native" code...
any suggestions?!

Thanks,
  Johannes

Author
7 Jan 2006 9:34 PM
Mattias Sjögren
Johannes,

>I really would like to see this for-loop removed by some "native" code...
>any suggestions?!

There's an IL instruction called "cpblk" that does a memcopy, but
unfortunately there's no way to generate that from C#. But assuming
it's supported on CF (which I know little about) you could write a
small helper library in IL assembler that uses it.

Other options on the desktop is to call RtlMoveMemory in kernel32.dll
or memcpy exported from some C++ CRT library through P/Invoke. I don't
know if either of those are available from CF.


Mattias

--
Mattias Sjögren [C# MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

AddThis Social Bookmark Button