2013-09-24 84 views
0

我使用Windows Phone CameraCaptureTask來收集圖像,然後調整大小以便在應用程序中更好地使用它。出於某種原因,無論我嘗試過什麼,我都無法調整圖像大小。使用WriteableBitmap調整圖像大小不起作用

MainPage.xaml.cs中

private void cameraTask_Completed(object sender, PhotoResult e) 
{ 
    if (e.TaskResult == TaskResult.OK) 
    { 
     BitmapImage bmi = new BitmapImage(); 
     bmi.SetSource(e.ChosenPhoto); 
     //MessageBox.Show(bmi.PixelWidth.ToString() + "x" + bmi.PixelHeight.ToString()); 

     var gcd = GCD(bmi.PixelWidth, bmi.PixelHeight); 
     var result = string.Format("{0}:{1}", bmi.PixelWidth/gcd, bmi.PixelHeight/gcd); 

     WriteableBitmap wb = new WriteableBitmap(bmi); 
     //WriteableBitmap wb; 
     //Stream stream = new MemoryStream(); 
     var stream = new MemoryStream(); 


     switch (result) 
     { 
      case "3:4": 
       //wb = new WriteableBitmap(480, 640); 
       //wb.SetSource(e.ChosenPhoto); 
       wb.SaveJpeg(stream, 480, 640, 0, 100); 
       break; 
      case "4:3": 
       //wb = new WriteableBitmap(640, 480); 
       //wb.SetSource(e.ChosenPhoto); 
       wb.SaveJpeg(stream, 640, 480, 0, 100); 
       break; 
      case "9:16": 
       //wb = new WriteableBitmap(448, 800); 
       //wb.SetSource(e.ChosenPhoto); 
       wb.SaveJpeg(stream, 448, 800, 0, 100); 
       break; 
      case "16:9": 
       //wb = new WriteableBitmap(800, 448); 
       //wb.SetSource(e.ChosenPhoto); 
       wb.SaveJpeg(stream, 800, 448, 0, 100); 
       break; 
      default: 
       wb = null; 
       return; 
     } 

     stream.Seek(0, SeekOrigin.Begin); 
     //stream.Dispose(); 

     MessageBox.Show(wb.PixelWidth.ToString() + " x " + wb.PixelHeight.ToString()); 

     var capturedPicture = new CapturedPicture(e.OriginalFileName, stream); 
    } 
} 

public int GCD(int a, int b) 
{ 
    while (a != 0 && b != 0) 
    { 
     if (a > b) 
      a %= b; 
     else 
      b %= a; 
    } 
    if (a == 0) 
     return b; 
    else 
     return a; 
} 

不管我做什麼,設定WriteableBitmap要麼e.ChosenPhotobmi強制產生wb是原來的大小。有任何想法嗎?

+0

你沒有調整WriteableBitmap的,你救了它的調整拷貝到流。保存後嘗試使用SetSource方法或LoadJpeg方法。 –

+0

我有點困惑。由於我的'CapturedPicture'類需要一個文件名和圖像流作爲參數,我以爲將SaveableBitmap的一個調整大小的副本保存到流中並以這種方式使用它是正確的?我將如何調整WriteableBitmap的大小併爲我的CapturedPicture類使用流?我無法弄清楚如何最好地實施您的建議。 – Matthew

+0

在引用http://msdn.microsoft.com/EN-US/library/windowsphone/develop/system.windows.media.imaging.extensions.savejpeg(v=vs.105).aspx時我使用了'SaveJpeg'對圖像保存在應用程序時所需的jpeg流進行編碼。我在我的應用程序中的其他地方使用了這種方法,這很好,但在這種情況下,我必須首先調整圖像的大小,這是我卡住的地方。 – Matthew

回答

3

嘗試使用WriteableBitmapExWinPhone.dll

wb = wb.Resize(Convert.ToInt32(pass value of width here), Convert.ToInt32(pass value of height here), WriteableBitmapExtensions.Interpolation.Bilinear);