问题
I have to do a program that read and image and puts it into a byte array
var Imagenoriginal = File.ReadAllBytes("10M.bmp");
And Divide That byte Array into 3 Diferent Arrays in order to send each one of this new arrays to other computer ( Using Pipes ) to process them there and finally take them back to the origial computer and finally give the result. But my question Is how do I do an Algorithm able to divide the byte array in three different bytes arrays if the image selected can have diferent size. Thanks for your help, have a nice day. =)
回答1:
You can divide length of array, so you have three integers n1
, n2
and n3
with all of them summing up to array.Length
. Then, this snippet, using LINQ should be of help:
var arr1 = sourceArray.Take(n1).ToArray();
var arr2 = sourceArray.Skip(n1).Take(n2).ToArray();
var arr3 = sourceArray.Skip(n1+n2).Take(n3).ToArray();
Now, in arr1
,arr2
and arr3
you will have three parts of your source array. You need to use LINQ, so in the beginning of the code don't forget using System.Linq;
.
回答2:
You can try like this:
public static IEnumerable<IEnumerable<T>> DivideArray<T>(this T[] array, int size)
{
for (var i = 0; i < (float)array.Length / size; i++)
{
yield return array.Skip(i * size).Take(size);
}
}
The call like this:
var arr = new byte[] {1, 2, 3, 4, 5,6};
var dividedArray = arr.DivideArray(3);
Here is a LINQ approach:
public List<List<byte>> DivideArray(List<byte> arr)
{
return arr
.Select((x, i) => new { Index = i, Value = x })
.GroupBy(x => x.Index / 100)
.Select(x => x.Select(v => v.Value).ToList())
.ToList();
}
回答3:
Have you considered using Streams
? You could extend the Stream
class to provide the desired behavior, as follows:
public static class Utilities
{
public static IEnumerable<byte[]> ReadBytes(this Stream input, long bufferSize)
{
byte[] buffer = new byte[bufferSize];
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
using (MemoryStream tempStream = new MemoryStream())
{
tempStream.Write(buffer, 0, read);
yield return tempStream.ToArray();
}
}
}
public static IEnumerable<byte[]> ReadBlocks(this Stream input, int nblocks)
{
long bufferSize = (long)Math.Ceiling((double)input.Length / nblocks);
return input.ReadBytes(bufferSize);
}
}
The
ReadBytes
extension method reads theinput
Stream and returns its data as a sequence of byte arrays, using the specifiedbufferSize
.The
ReadBlocks
extension method callsReadBytes
with the appropriate buffer size, so that the number of elements in the sequence equalsnblocks
.
You could then use ReadBlocks
to achieve what you want:
public class Program
{
static void Main()
{
FileStream inputStream = File.Open(@"E:\10M.bmp", FileMode.Open);
const int nblocks = 3;
foreach (byte[] block in inputStream.ReadBlocks(nblocks))
{
Console.WriteLine("{0} bytes", block.Length);
}
}
}
Note how ReadBytes
uses tempStream
and read
to write in memory the bytes read from the input stream before converting them into an array of bytes, it solves the problem with the leftover bytes mentioned in the comments.
来源:https://stackoverflow.com/questions/29593048/how-to-divide-an-array-on-c