C#_字节数组分段操作__Buffer.BlockCopy()与BinaryReader.ReadBytes() 的区别

下面例子是关于YUV420P的操作,需要把一帧YUV420P需要拆分成三段 Y ,U, V
提供两种方法.关于效率请自行测试

一: BlockCopy()

        byte[] data = new byte[Width * Height * 3 / 2];

        byte[] dataY = new byte[Width * Height];
        byte[] dataU = new byte[Width * Height / 4];
        byte[] dataV = new byte[Width * Height / 4];

        Buffer.BlockCopy(data, 0, dataY, 0, Width * Height);
        Buffer.BlockCopy(data, Width * Height, dataU, 0, Width * Height / 4);
        Buffer.BlockCopy(data, Width * Height * 5 / 4, dataV, 0, Width * Height / 4);

 

二: ReadBytes()

        byte[] data = new byte[Width * Height * 3 / 2];
        MemoryStream ms = new MemoryStream(data);
        BinaryReader reader = new BinaryReader(ms);

        byte[] dataY = reader.ReadBytes(Width * Height);
        byte[] dataU = reader.ReadBytes(Width * Height / 4);
        byte[] dataV = reader.ReadBytes(Width * Height / 4);
        

 

ps:

已知BlockCopy()方法需要指明偏移量,否则可能会被覆盖
而ReadBytes()每次写入偏移量都会跟着递进。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值