简体   繁体   English

C#-如何从较大的数组的一部分创建新的字节数组

[英]C# - How do I create a new byte array from a portion of a larger one

I'm rather new to coding so I'm not sure if this is a really obvious question or not. 我是编码的新手,所以不确定这是否是一个很明显的问题。 What I have is an ordered list of bytes that represent pixels and depth data related to those pixels, so when returned into a box they create an image. 我所拥有的是一个有序的字节列表,它们代表像素和与这些像素有关的深度数据,因此当它们返回到盒子中时,它们会创建图像。 What I'm trying to do is isolate a smaller rectangle of those bytes into a new byte array. 我正在尝试将这些字节的较小矩形隔离到一个新的字节数组中。

So basically I want to skip a large amount of the bytes at the start of the array (the ones completely above the smaller rectangle section), as well as the first lot on the left of it, then add one row of the length of the smaller box to the new array, then skip the ones on the right of the box, skip the left side on the next line down, add the length, skip the right and repeat it all until I reach the end of the box. 所以基本上我想跳过数组开头的字节(完全在较小的矩形部分上方的字节),以及它左边的第一批字节,然后添加长度的一行较小的框到新数组,然后跳过框右侧的框,在下一行的左侧跳过框,添加长度,跳过右侧,然后重复所有操作,直到到达框的末尾。

I really hope that bad explanation makes sense to someone. 我真的希望错误的解释对某人有意义。 I have no idea how to go about doing this. 我不知道该怎么做。 Any help would be really appreciated! 任何帮助将非常感激!

Thanks! 谢谢!

The simplest option is probably to create a byte array of the right size, then use Array.Copy or Buffer.BlockCopy - multiple times, by the sounds of it. 最简单的选择可能是创建一个大小合适的字节数组,然后根据其声音多次使用Array.CopyBuffer.BlockCopy

I suspect you'll need to call the copy method once per row, working out where in the source data the relevant part of the row starts, where in the target data the relevant part of the row starts. 我怀疑您需要每行调用一次copy方法,计算出该行的相关部分在源数据中的起始位置,在目标数据中该行的相关部分的起始位置。 I'll leave the rest to you for the moment now you've got the basic idea - but feel free to ask for more clarification. 现在,您已经掌握了基本的想法后,我将暂时将其余部分留给您-但随时可以要求进一步的澄清。 Don't forget (in your calculations) that the "target" row number won't be equal to the "source" row number! 不要忘记(在您的计算中)“目标”行号将不等于“源”行号! (I suspect it's easiest to loop over the target row numbers, and add an offset...) (我怀疑最简单的方法是遍历目标行号并添加偏移量...)

I think it would look something like this: 我认为它看起来像这样:

const int numberOfBytesPerPixel = ...;

// input: original data
int originalWidth = ...;
int originalHeight = ...;
byte[] original = ...; // should have the correct size and contain the data

// input: desired position and size for cropping rectangle
int cropOffsetToTheRight = ...;
int cropOffsetDown = ...;
int cropWidth = ...;
int cropHeight = ...;

// get the rectangle:
byte[] crop = new byte[numberOfBytesPerPixel * cropWidth * cropHeight];
for (int rowNumber = 0; rowNumber < cropHeight; ++rowNumber)
{
    Array.Copy(
        original,
        numberOfBytesPerPixel * ((cropOffsetDown + rowNumber) * originalWidth + cropOffsetToTheRight),
        crop,
        numberOfBytesPerPixel * rowNumber,
        numberOfBytesPerPixel * cropWidth
        );
}

Here's the doc for the overload of Array.Copy used . Array.Copy使用的重载文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM