2014-10-23 3 views
1

WriteableBitmap을 바이트 배열로 변환하는 방법이 있습니까? writeablebitmap을 System.Windows.Controls.Image 소스에 할당하는 방법도 있습니다. 나는 이것을 시도했지만 FromHBitmap에 일반적인 GDI 예외가있다.WPF WriteableBitmap을 바이트 배열

System.Drawing.Image img = System.Drawing.Image.FromHbitmap(wb.BackBuffer); 
MemoryStream ms = new MemoryStream(); 
img.Save(ms, System.Drawing.Imaging.ImageFormat.Png); 
myarray = ms.ToArray(); 
+0

코드의 WriteableBitmap은 어디에 있습니까? – thumbmunkeys

+0

클래스 범위에서 선언되었지만 더 이해하기 위해 편집했습니다. – shady

+0

데이터를 BackBuffer로 변경 했으므로 더 의미가 있습니다 – thumbmunkeys

답변

3

코드는 PNG 형식의 이미지 데이터를 인코딩하지만, 원시, 인코딩되지 않은 비트 맵 데이터는 FromHBitmap 기대하고있다.

이 시도 : bitmapSource

var width = bitmapSource.PixelWidth; 
var height = bitmapSource.PixelHeight; 
var stride = width * ((bitmapSource.Format.BitsPerPixel + 7)/8); 

var bitmapData = new byte[height * stride]; 

bitmapSource.CopyPixels(bitmapData, stride, 0); 

... 여기서 당신의 WriteableBitmap (또는 다른 BitmapSource).

+0

트릭을 주셔서 감사합니다. – shady

+1

UWP의 WriteableBitmap 객체에는 .Format.BitsPerPixel이 없습니다. 또한 CopyPixels도 존재하지 않습니다. WPF와 UWP에서 동일한 데이터 유형간에 큰 차이가있는 이유는 무엇입니까? –