0

Windows Forms에서 이미지의 크기를 조정하는 방법에 대한 예제가 많이 있지만이 경우 Windows Store 응용 프로그램에서 바이트 배열을 사용하고 있습니다. 이것은 내가 사용하고있는 스 니펫 코드입니다.바이트 배열을 통해 이미지를 반 크기로 스케일하는 방법은 무엇입니까?

// Now that you have the raw bytes, create a Image Decoder 
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(fileStream); 

// Get the first frame from the decoder because we are picking an image 
BitmapFrame frame = await decoder.GetFrameAsync(0); 

// Convert the frame into pixels 
PixelDataProvider pixelProvider = await frame.GetPixelDataAsync(); 

// Convert pixels into byte array 
srcPixels = pixelProvider.DetachPixelData(); 
wid = (int)frame.PixelWidth; 
hgt =(int)frame.PixelHeight; 

// Create an in memory WriteableBitmap of the same size 
bitmap = new WriteableBitmap(wid, hgt); 
Stream pixelStream = bitmap.PixelBuffer.AsStream(); 
pixelStream.Seek(0, SeekOrigin.Begin); 

// Push the pixels from the original file into the in-memory bitmap 
pixelStream.Write(srcPixels, 0, (int)srcPixels.Length); 
bitmap.Invalidate(); 

이 경우 스트림의 복사본을 만드는 것입니다. 바이트 배열을 조작하여 폭과 높이의 절반을 줄이는 방법을 모르겠습니다.

답변

0

GetPixelDataAsync에 대해 the MSDN documentation을 보면 과부하가 발생하여 작동 중에 적용 할 BitmapTransform을 지정할 수 있습니다. 당신이 당신의 원본 코드와 같이 DetachPixelData를 호출 할 수 있습니다, 지금

// decode a frame (as you do now) 
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(fileStream); 
BitmapFrame frame = await decoder.GetFrameAsync(0); 

// calculate required scaled size 
uint newWidth = frame.PixelWidth/2; 
uint newHeight = frame.PixelHeight/2; 

// convert (and resize) the frame into pixels 
PixelDataProvider pixelProvider = 
    await frame.GetPixelDataAsync(
     BitmapPixelFormat.Rgba8, 
     BitmapAlphaMode.Straight, 
     new BitmapTransform() { ScaledWidth = newWidth, ScaledHeight = newHeight}, 
     ExifOrientationMode.RespectExifOrientation, 
     ColorManagementMode.DoNotColorManage); 

, 그러나 이것은 당신에게 대신 크기의 전체의 크기가 조정 된 이미지를 줄 것이다 :

그래서 당신은 다음과 같이 귀하의 예제 코드에서 뭔가를이 작업을 수행 할 수 있습니다 영상.

srcPixels = pixelProvider.DetachPixelData(); 

// create an in memory WriteableBitmap of the scaled size 
bitmap = new WriteableBitmap(newWidth, newHeight); 
Stream pixelStream = bitmap.PixelBuffer.AsStream(); 
pixelStream.Seek(0, SeekOrigin.Begin); 

// push the pixels from the original file into the in-memory bitmap 
pixelStream.Write(srcPixels, 0, (int)srcPixels.Length); 
bitmap.Invalidate();