Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / January 2006

Tip: Looking for answers? Try searching our database.

MemCpy revisited (in Unsafe code)

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Johannes Bialek - 25 Dec 2005 04:50 GMT
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
Mattias Sjögren - 07 Jan 2006 21:34 GMT
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

Signature

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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.