Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用不安全块将字节数组复制到位图_C#_Performance_Image Processing - Fatal编程技术网

C# 使用不安全块将字节数组复制到位图

C# 使用不安全块将字节数组复制到位图,c#,performance,image-processing,C#,Performance,Image Processing,我试图从一些图像代码中挤出最佳性能,但我遇到了麻烦 据我所知,使用指针应该可以加快进程,但我的经验非常有限,而且很难找到好的文档来阅读和理解 我说得对吗?有人能给我看一个经过注释的代码转换示例,帮助我理解这个过程吗 public void UpdatePixelIndexes(IEnumerable<byte[]> lineIndexes) { int width = this.Image.Width; int height = thi

我试图从一些图像代码中挤出最佳性能,但我遇到了麻烦

据我所知,使用指针应该可以加快进程,但我的经验非常有限,而且很难找到好的文档来阅读和理解

我说得对吗?有人能给我看一个经过注释的代码转换示例,帮助我理解这个过程吗

    public void UpdatePixelIndexes(IEnumerable<byte[]> lineIndexes)
    {
        int width = this.Image.Width;
        int height = this.Image.Height;

        IEnumerator<byte[]> indexesIterator = lineIndexes.GetEnumerator();
        for (int rowIndex = 0; rowIndex < height; rowIndex++)
        {
            indexesIterator.MoveNext();
            BitmapData data = this.Image.LockBits(Rectangle.FromLTRB(0, rowIndex, width, rowIndex + 1), ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);

            try
            {
                Marshal.Copy(indexesIterator.Current, 0, data.Scan0, width);
            }
            finally
            {
                this.Image.UnlockBits(data);
            }
        }
    }
public void UpdatePixelIndexes(IEnumerable lineIndexes)
{
int width=this.Image.width;
int height=this.Image.height;
IEnumerator indexesIterator=lineIndexes.GetEnumerator();
对于(int-rowIndex=0;rowIndex
您不太可能真正需要
不安全的
。正如建议的那样,您应该停止为每个扫描行锁定/解锁位图。相反,请执行以下操作:

public void UpdatePixelIndexes(IEnumerable<byte[]> lineIndexes)
{
    int width = this.Image.Width;
    int height = this.Image.Height;
    int rowIndex = 0;

    BitmapData data = this.Image.LockBits(Rectangle.FromLTRB(0, 0, width, height), ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
    try
    {
        foreach (byte[] scanLine in lineIndexes)
        {
            Marshal.Copy(scanLine, 0,
               IntPtr.Add(data.Scan0, data.Stride * rowIndex), width);

            if (++rowIndex >= height)
            {
                break;
            }
        }
    }
    finally
    {
        this.Image.UnlockBits(data);
    }
}
public void UpdatePixelIndexes(IEnumerable lineIndexes)
{
int width=this.Image.width;
int height=this.Image.height;
int rowIndex=0;
BitmapData data=this.Image.LockBits(矩形.FromLTRB(0,0,宽度,高度),ImageLockMode.WriteOnly,PixelFormat.Format8BPindexed);
尝试
{
foreach(行索引中的字节[]扫描行)
{
封送处理副本(扫描线,0,
IntPtr.Add(data.Scan0,data.Stride*rowIndex),宽度);
如果(++行索引>=高度)
{
打破
}
}
}
最后
{
此.Image.UnlockBits(数据);
}
}

为什么不锁定和解锁一次?您必须传递data.Scan0+data.Stride*rowIndex,但这不是真正的问题。@harold您能举个例子吗?谢谢。这当然是一个进步。我希望通过使用Marshal.Copy避免复制内存,因此,如果可能,我会寻找不安全的用法。
Marshal.Copy()
不应该复制数据。它应该只是将字节从一个地方复制到另一个地方。你有理由相信它是在复制数据吗?我只是觉得它就是这么做的。这一评论似乎证实了这一点。首先,该注释是断章取义的,可能是,也可能不是,注释者的意图是指数据的副本是由
Marshal.copy()
本身制作的(即,他们可能在谈论以新
位图
对象的形式分配内存的需要)。其次,即使评论者确实意味着
Marshal.Copy()
执行一些临时的中间缓冲区的分配,但这并不意味着评论者知道他们在谈论什么。这是真的。我很高兴将此作为答案。