在 C# 中复制原始像素数据
我正在尝试将位图的像素复制到 DirectX 纹理中。从两者获取 IntPtr 都很简单,但如何有效地复制像素数据?
var data = FBitmap.LockBits(..)
var rect = texture.LockRectangle(0, LockFlags.None);
IntPtr from = data.Scan0;
IntPtr to = rect.Data.DataPointer;
//copy data
texture.UnlockRectangle(0);
FBitmap.UnlockBits(data);
我尝试使用 Marshal.Copy 但它需要像素作为数组,当然我想避免另一个副本。
i am trying to copy the pixels of a Bitmap into a DirectX texture. its simple to get the IntPtr's from both, but how do i copy the pixel data efficiently?
var data = FBitmap.LockBits(..)
var rect = texture.LockRectangle(0, LockFlags.None);
IntPtr from = data.Scan0;
IntPtr to = rect.Data.DataPointer;
//copy data
texture.UnlockRectangle(0);
FBitmap.UnlockBits(data);
i tried to use Marshal.Copy but it need the pixels as an array and i would like to avoid another copy of course.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 Windows API CopyMemory - 别名“RtlMoveMemory”。比所有 LockBits 的东西快得多...
http://www.pinvoke。 net/default.aspx/urlmon/CopyMemory.html
You could use the Windows API CopyMemory - Alias "RtlMoveMemory". Much faster than all of that LockBits stuff...
http://www.pinvoke.net/default.aspx/urlmon/CopyMemory.html
如果您谈论的是每个 CPU 周期都很重要的前沿效率,那么您最好直接将数据指针与
不安全
代码一起使用。没有真正快速的方法可以将平面数组映射到像
Array
这样的实际托管对象上,您必须逐字节复制像素数据。If you're talking about the kind of bleeding edge efficiency where every CPU cycle counts, you're better off just using the data pointer directly with
unsafe
code.There's no real fast way to map a flat array over an actual managed object like
Array
, you'd have to copy the pixel data byte by byte pretty much.