2011-11-29 5 views
2

다음 문제가 있습니다. 바이트 배열을 WriteableBitmap 크기로 변환해야합니다. 나는 다음 코드를 쓴다. 통화 wb.Render(img, new ScaleTransform() { ScaleX = scale, ScaleY = scale });Silverlight의 이미지에서 WriteableBitmap 렌더링 문제가 있습니다.

private byte[] ResizeImage(byte[] array, double maxWidth, double maxHeight) 
{ 
    WriteableBitmap wb = null; 

    var stream = new MemoryStream(array); 
    stream.Seek(0, SeekOrigin.Begin); 
    var bmp = new WriteableBitmap(0, 0); 
    bmp.SetSource(stream); 
    stream.Close(); 
    var img = new Image(); 
    img.Source = bmp; 
    double scaleX = 1; 
    double scaleY = 1; 
    if (bmp.PixelHeight > maxHeight) 
    { 
     scaleY = maxHeight/bmp.PixelHeight; 
    } 
    if (bmp.PixelWidth > maxWidth) 
    { 
     scaleX = maxWidth/bmp.PixelWidth; 
    } 
    wb = new WriteableBitmap(0, 0); 
    var scale = Math.Min(scaleY, scaleX); 
    wb.Render(img, new ScaleTransform() { ScaleX = scale, ScaleY = scale }); 
    wb.Invalidate(); 
    return Utils.Encode(wb); 

} 

, wb 제로 픽셀을 갖는다.

도와주세요.

답변

0

변경해보십시오 :

wb = new WriteableBitmap(0, 0); 

사람 :

wb = new WriteableBitmap(maxWidth, maxHeight); 
+0

결과가 동일 ( –

1
private byte[] ResizeImage(byte[] array, int maxWidth, int maxHeight) 
{ 
    var stream = new MemoryStream(array); 
    stream.Seek(0, SeekOrigin.Begin); 

    var bmp = new BitmapImage(); 
    bmp.SetSource(stream); 
    stream.Close(); 
    var img = new Image(); 
    img.Source = new BitmapImage(); 

    double scaleX = 1; 
    double scaleY = 1; 
    if (bmp.PixelHeight > maxHeight) 
    { 
     scaleY = maxHeight/bmp.PixelHeight; 
    } 
    if (bmp.PixelWidth > maxWidth) 
    { 
     scaleX = maxWidth/bmp.PixelWidth; 
    } 

    WriteableBitmap wb = new WriteableBitmap(maxWidth, maxHeight); 
    var scale = Math.Min(scaleY, scaleX); 
    wb.Render(img, new ScaleTransform() { ScaleX = scale, ScaleY = scale }); 
    wb.Invalidate(); 

    return Utils.Encode(wb); 
} 
+0

를 나는 그것이 0 값이 이미지 dl.dropbox.com/를 볼 수있는 모든 픽셀을 도움이되지 말할으로 u/16517591/screen.png –

+0

그게 내가 바꾼 유일한 것만은 아니야. 코드를 시험해 봐. 테스트 해봤는데 나에게 맞는거야. – David

+0

나는 그것을 시도하고 상황은 같다. –