我是要用c#來實現,如今已經知道了rgb數組,那麼如何快速生成一張圖片呢?c#
其實這個話題並不侷限因而rgb字節數組的順序,只要你能對於上表示紅、綠、藍的值,就能夠生成圖片。知道了原理,作什麼都簡單了。數組
rgb分別只是表明一個顏色的值,若是你真是rgb,那你就是要按位置用setpiex一個個畫顏色。以下:函數
Bitmap bmp = new Bitmap(w,h); for(int x = 0;x < w;x++){ for(int y = 0;y < h;y++){ bmp.SetPixel(x,y,Color.FromArgb(255,r,g,b)); } }
這個是最簡單的了 前提是你能正肯定位你的rgb的座標,效率一點的 用 Bitmap.LookBit 來鎖定內存操做。spa
下面再給兩個RGB 和 Bitmap 互轉的函數,僅供參考:.net
public Bitmap BGR24ToBitmap(byte[] imgBGR) { int p = 0; Bitmap bmp = new Bitmap(vW, vH, System.Drawing.Imaging.PixelFormat.Format24bppRgb); if (imgBGR != null) { //構造一個位圖數組進行數據存儲 byte[] rgbvalues = new byte[imgBGR.Length]; //對每個像素的顏色進行轉化 for (int i = 0; i < rgbvalues.Length; i += 3) { rgbvalues[i] = _imgData[i + 2]; rgbvalues[i + 1] = _imgData[i + 1]; rgbvalues[i + 2] = _imgData[i]; } //位圖矩形 Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height); //以可讀寫的方式將圖像數據鎖定 System.Drawing.Imaging.BitmapData bmpdata = bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, bmp.PixelFormat); //獲得圖形在內存中的首地址 IntPtr ptr = bmpdata.Scan0; //將被鎖定的位圖數據複製到該數組內 //System.Runtime.InteropServices.Marshal.Copy(ptr, rgbvalues, 0, imgBGR.Length); //把處理後的圖像數組複製回圖像 System.Runtime.InteropServices.Marshal.Copy(rgbvalues, 0, ptr, imgBGR.Length); //解鎖位圖像素 bmp.UnlockBits(bmpdata); } return bmp; } public byte[] bitmap2BGR24(Bitmap img) { byte[] bgrBytes = new byte[0]; Bitmap bmp = (Bitmap)img; if (bmp != null) { //位圖矩形 Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height); //以可讀寫的方式將圖像數據鎖定 System.Drawing.Imaging.BitmapData bmpdata = bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly, bmp.PixelFormat); //構造一個位圖數組進行數據存儲 int bLength = bmp.Width * bmp.Height * 3; byte[] rgbVal = new byte[bLength]; bgrBytes = new byte[bLength]; //獲得圖形在內存中的首地址 IntPtr ptr = bmpdata.Scan0; //將被鎖定的位圖數據複製到該數組內 System.Runtime.InteropServices.Marshal.Copy(bmpdata.Scan0, rgbVal, 0, bLength); //把處理後的圖像數組複製回圖像 //System.Runtime.InteropServices.Marshal.Copy(rgbVal, 0, ptr, bytes); //解鎖位圖像素 bmp.UnlockBits(bmpdata); //對每個像素的rgb to bgr的轉換 for (int i = 0; i < rgbVal.Length; i += 3) { bgrBytes[i] = rgbVal[i + 2]; bgrBytes[i + 1] = rgbVal[i + 1]; bgrBytes[i + 2] = rgbVal[i]; } } return bgrBytes; }
參考:https://bbs.csdn.net/topics/391862868code