SW개발/c#

Byte[] To Bitmap

YellowThroat 2023. 1. 26. 10:13
	private Bitmap ByteToBitmap(byte[] array, int width, int height)
	{
		int size = width * height * 3;
		Bitmap bmp = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
		BitmapData data = bmp.LockBits(
			new Rectangle(0, 0, width, height),
			ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
		IntPtr ptr = data.Scan0;
		Marshal.Copy(array, 0, ptr, size);
		bmp.UnlockBits(data);
		return bmp;
	}

속도 테스트를 정확하게 해보진 못했지만 이게 빠른편인 듯 하다.

ImageFormat은 byte[] 형식인데 24bppRgb는 1픽셀의 데이터 rgb를 각각 8bit씩 총 24비트(=3바이트)

그래서 Size는 픽셀 수 x 3

 

ImageForamt.Format32bppRgb 형식이 있는데 이는 rgb 각각 8bit에 쓰레기값 8bit가 더해져서 총 32비트(=4바이트) 쓰임.

알파값이나 그 외 기타 정보를 넣었을때 활용할 수 있을 것으로 보인다.