픽셀 당 16 비트가있는 비트 맵을 만들고 싶지만 픽셀 형식을 지정할 때 :
PixelFormat.Format16bppArgb1555, PixelFormat.Format16bppGrayScale, PixelFormat.Format16bppRgb555, PixelFormat.Format16bppRgb565
그들은 모두 System.NotImplementedException
오류 메시지와 함께 발생합니다 "Not Implemented"
. Bitmap.SetPixel()
바이트를 알파, 빨강, 녹색 또는 파랑 으로 만 설정할 수 있기 때문에 픽셀 당 16 비트를 수동으로 할당 할 수 없습니다 (따라서 특정 픽셀 형식이 필요한 마샬링을 사용해야 함). 각 색상 값에 대해 바이트를 설정하는 대신 빨강, 녹색 및 파랑 사이에 16 비트를 배포하려고합니다. 예를 들어 빨간색은 5 비트, 녹색은 5 비트, 파란색은 5 비트입니다.
다음은 a를 발생시키는 픽셀 당 16 비트 픽셀 형식 중 하나의 예입니다 System.NotImplementedException
.
Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format16bppArgb1555);
내 시스템은 MacOS입니다.
System.Drawing
모든 플랫폼에서 모든 픽셀 형식을 지원하지는 않습니다. 나는 일단 수집 차이를합니다 (참조 다른 플랫폼에서 사용 가능한 픽셀 형식의 제한 부분). MacOS는 Linux 버전과 동일한 패키지를 사용한다고 생각합니다.
으로 Bitmap
인코더가 보존 이미지를 저장할 수 없습니다 Format16bppArgb1555
어쨌든 픽셀 형식을 난 당신이 작성하는 것이 좋습니다 Bitmap
(예를 들어, 가장 가까운 호환 형식을 사용하여 인스턴스를 Format32bppArgb
대신 Format16bppArgb1555
), 그리고 당신이 정말로에 색상을 변환하려는 경우 ARGB1555
색 공간, 당신은 사용할 수 Quantize
와 방법을 PredefinedColorsQuantizer.Argb1555
비교를 위해 링크 한 동일한 라이브러리를 사용합니다. NuGet 에서 다운로드 할 수 있습니다 .
면책 조항 : MacOS에서 테스트하지는 않았지만 Linux (Ubuntu)에서 작동합니다.
각 색상 값에 대해 바이트를 설정하는 대신 빨강, 녹색 및 파랑 사이에 16 비트를 배포하려고합니다. 예를 들어 빨간색은 5 비트, 녹색은 5 비트, 파란색은 5 비트입니다.
편집 : 나는 이미 다음 버전에서 작업 중이며 PixelFormat
모든 플랫폼에서 관리되는 비트 맵 데이터를 사용할 수 있습니다. 공개 BitmapDataFactory.CreateBitmapData 메서드를 참조하세요 (아직 공개 릴리스에서는 사용할 수 없지만 Development
브랜치 를 확인하면 이미 사용할 수 있음 ).
최신 정보:
이미지 바이트 (예 : 이미지 만 나타내는 바이트, 헤더 없음)가 있으며 이미지 바이트가 디코딩 할 이미지 형식이 픽셀 당 16 비트인지 테스트 중입니다.
이제 16 비트 원시 형식으로 픽셀을 설정해야하는 이유를 알았습니다. Linux / MacOS에서 System.Drawing.Bitmap
와 같이 위에서 언급 한대로 관리되는 비트 맵 데이터를 생성하는 경우 가장 쉬운 솔루션을 제한적으로 지원합니다.
참고 : 개발 브랜치를 확인하지 않고도 메서드를 사용할 수 있는 시험판을 게시했습니다 BitmapDataFactory.CreateBitmapData
. 이제 원시 바이트 PixelFormat
를 다음 Bitmap
과 같이 바꿀 수 있습니다 .
(물론 pfGuess
실제 형식이 아니면 결과가 지저분해질 수 있습니다.)
public Bitmap ToBitmap(byte[] rawData, PixelFormat pfGuess, Size size)
{
// the result bitmap will be ARGB32, which works on every platform
Bitmap result = new Bitmap(size.Width, size.Height);
// we create an accessor for the result so we can quickly copy the translated pixels
using var bitmapDataDst = result.GetWritableBitmapData();
// For the managed source any pixel format will work.
// Please note though that unlike System.Drawing.Bitmap images,
// managed bitmap data never has padding bytes at the end of each line.
// So if your rawData has padding (eg. when size has odd width and pf is 16bpp),
// then maybe the best if you include the padding in width.
// See https://docs.microsoft.com/en-us/windows/win32/medfound/image-stride for details
using var bitmapDataSrc = BitmapDataFactory.CreateBitmapData(size, pfGuess);
// Checking if rawData is long enough. RowSize is the width in bytes.
if (rawData.Length < bitmapDataSrc.RowSize * size.Height)
throw new ArgumentException("rawData is too short");
int currentByte = 0;
// iterating through rows
for (int y = 0; y < size.Height; y++)
{
var rowSrc = bitmapDataSrc[y]; // has the specified pixel format
var rowDst = bitmapDataDst[y]; // has ARGB32 pixel format
// writing raw pixels
for (int x = 0; x < bitmapDataSrc.RowSize; x++)
rowSrc.WriteRaw<byte>(x, rawData[currentByte++]);
// copying the pixels interpreted as pfGuess to result
for (int x = 0; x < size.Width; x++)
rowDst[x] = rowSrc[x];
}
// The result bitmap is now populated from the arbitrarily interpreted raw data.
// As it has ARGB32 format you can save it on any platform without any issues.
return result;
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다