2016-11-26 41 views
1

我試圖將RenderTargetBitmap轉換爲字節數組,然後將發送到外部單色OLED屏幕。我知道,位圖正確顯示位/字節對齊應該是LSB到MSB &從上至下:將RenderTargetBitmap轉換爲要在嵌入式屏幕上顯示的字節[]?

enter image description here

但我無法弄清楚如何獲得RenderTargetBitmap的PixelData取出該格式。

目前我已經有了:

 RenderTargetBitmap renderTargetBitmap; //This is already set higher up 
     DataReader reader = DataReader.FromBuffer(await renderTargetBitmap.GetPixelsAsync()); 

     // Placeholder for reading pixels 
     byte[] pixel = new byte[4]; // RGBA8 

     // Write out pixels 
     int index = 0; 
     byte[] array = new byte[renderTargetBitmap.PixelWidth*renderTargetBitmap.PixelHeight]; 
     using (reader) 
     { 
      //THIS IS WHERE I THINK I'M SCREWING UP 
      for (int x = 0; x < rHeight; x++) 
      { 
       for (int y = 0; x < rWidth; y++) 
       { 
        reader.ReadBytes(pixel); 
        if (pixel[2] == 255) 
         array[index] = 0xff; 
        else 
         array[index] = 0x00; 
        index++; 
       } 
      } 
     } 
     sh1106.ShowBitmap(buffer); //Send off the byte array 
+0

將其拷貝到堆棧時,看來你不是在所有使用x和y循環變量,而「索引」變量沒有更新任何地方,總是0 – Evk

+0

我只是刪除了'指數'++錯誤溢出。我修復了它。關於x和y循環,這就是我被阻止的地方。整個部分需要重寫,但我無法弄清楚。 – user2950509

回答

1

我面臨着同樣的問題,這是我做得到它的工作(這將在BGRA8輸出轉換成那麼可以使用1bpp映射輸出在單色顯示器上,SSD1306在我的情況下)

在1BPP輸出意味着8個像素存儲在1個字節。所以你需要將每4個字節轉換爲1位。

public async Task Draw() 
    { 
    ActiveCanvas.UpdateLayout(); 
    ActiveCanvas.Measure(ActiveCanvas.DesiredSize); 
    ActiveCanvas.Arrange(new Rect(new Point(0, 0), ActiveCanvas.DesiredSize)); 

    // Create a render bitmap and push the surface to it 
    RenderTargetBitmap renderBitmap = new RenderTargetBitmap(); 
    await renderBitmap.RenderAsync(ActiveCanvas, (int)ActiveCanvas.DesiredSize.Width, (int)ActiveCanvas.DesiredSize.Height); 

    DataReader bitmapStream = DataReader.FromBuffer(await renderBitmap.GetPixelsAsync()); 

    if (_device != null) 
    { 
     byte[] pixelBuffer_1BPP = new byte[(int)(renderBitmap.PixelWidth * renderBitmap.PixelHeight)/32]; 

     pixelBuffer_1BPP.Initialize(); 

     using (bitmapStream) 
     { 
      while (bitmapStream.UnconsumedBufferLength > 0) 
      { 
       uint index = (uint)(renderBitmap.PixelWidth * renderBitmap.PixelHeight * 4) - bitmapStream.UnconsumedBufferLength; 

       for (int bit = 0; bit < 8; bit++) 
       { 
       bitmapStream.ReadBytes(BGRA8); 

       byte value = (byte)(((BGRA8[0] & 0x80) | (BGRA8[1] & 0x80) | (BGRA8[2] & 0x80)) == 0x80 ? 1 : 0); 

       pixelBuffer_1BPP[index] |= (byte)(value << (7 - bit)); 
       } 
      } 

      _device.DrawBitmap(0, 0, pixelBuffer_1BPP, (short)renderBitmap.PixelWidth, (short)renderBitmap.PixelHeight, Colors.White); 
     } 
    } 
    }