我需要将int []转换为byte []指针。 以上是必需的,以便能够像素一样逐像素地填充WriteableBitmap的条目:
//previewBuffer1 is of type byte[]
WriteableBitmap wb1 = new WriteableBitmap(nVidWidth, nVidHeight);
int k=0;
// wb1.Pixels is of type int[] by default
byte* data = (byte*) wb1.Pixels; // ****THIS DOESN'T WORK. THROWS ERROR. HOW CAN I ACCOMPLISH THIS***
for (int i=0; i<nVidHeight; i++){
for (int j=0; j<nVidWidth; j++){
byte grayscaleval = previewBuffer1[k];
data [4*k] = grayscaleval ;
data [4*k + 1] = grayscaleval ;
data [4*k + 2] = grayscaleval ;
k++;
}
}
如何为wb1.Pixels获取int []类型的字节*指针?
答案 0 :(得分:3)
听起来您希望将数组中的每个int
视为字节序列 - BitConverter.GetBytes
怎么样?
byte[] bytes = BitConverter.GetBytes(intValue);
如果要避免数组复制等,请使用允许指针的unsafe
(您需要勾选项目属性中的“允许不安全代码”复选框):
unsafe static void UnsafeConvert(int value)
{
byte* bytes = (byte*)&value;
byte first = bytes[0];
...
}
答案 1 :(得分:0)
我对你的循环感到困惑,因为我觉得它有一些问题......但看起来你正试图剥离RGBA数据的alpha分量。为什么不这样做:
假设你有: 1.一个字节[]你想存储没有Alpha组件的RGB数据 2.一个RGB []数据的int []来源
int offset=0; // offset into the 'dest' array of bytes
for (...) // loop through your source array of ints
{
// get this current int value as rgba_val
int rgba_val = (whatever your RGBA source is..is it wb1.Pixels?)
dest[offset] = (rgba_val & 0xff) >> 24;
dest[offset+1] = (rgba_val & 0x00ff) >> 16;
dest[offset+2] = (rgba_val & 0x0000ff) >> 8;
}
答案 2 :(得分:0)
使用它将int数组转换为bytes array
WriteableBitmap wb1 = new WriteableBitmap(100, 100);
int[] pixels = wb1.Pixels;
byte[] data = new byte[pixels.Length];
for (int i= 0; i < pixels.Length;i ++)
{
data[i] = (byte)pixels[i];
}
答案 3 :(得分:0)
我想以下内容对我有用:
//previewBuffer1 is of type byte[]
WriteableBitmap wb1 = new WriteableBitmap(nVidWidth, nVidHeight);
int k=0;
// wb1.Pixels is of type int[] by default
//byte* data = (byte*) wb1.Pixels; // ****NOT REQUIRED ANYMORE***
for (int i=0; i<nVidHeight; i++){
for (int j=0; j<nVidWidth; j++){
int grayscaleval = (int) previewBuffer1[k];
wb1.Pixels[k] = ((grayscaleval) | (grayscaleval<<8) | (grayscaleval<<16) | (0xFF<<24)); // 0xFF is for alpha blending value
k++;
}
}
Atleast逻辑上似乎很好。然而要尝试一下。