Reputation: 759
I have a byte[]
object which contains the bytes of an image. I need to pad the image adding black zones to the left and right.
My image is 512 height and 384 width and I need to make it 512X512, that is, I need to add 128 colums, 64 to the left and 64 to the right.
I think I need to first copy all image bytes to columns 65 to 448 (that makes my 384 width image), then add 64 columns to the left and 64 columns to the right.
I'm not quite sure how to do this, I would imagine a nested for will suffice but not sure.
I'm programming in C#
Upvotes: 0
Views: 63
Reputation: 38880
Have tested this with a raw image generated by Photoshop and it seems to work OK. Obviously it's designed to only work for your specific case as I'm not sure what you're trying to achieve but I'm sure you could improve upon it :)
public byte[] FixImage(byte[] imageData, int bitsPerPixel)
{
int bytesPerPixel = bitsPerPixel / 8;
List<byte> data = new List<byte>();
for (int i = 0; i < imageData.Length; i += 384 * bytesPerPixel)
{
data.AddRange(new byte[64*bytesPerPixel]);
data.AddRange(imageData.Skip(i).Take(384 * bytesPerPixel));
data.AddRange(new byte[64 * bytesPerPixel]);
}
return data.ToArray();
}
If you end up using more complicated formats than raw byte arrays, it may be worth investigating using the GDI functions in System.Drawing. Let me know if you want an example of that.
Upvotes: 1