0

CameraCaptureTask에서 이미지를 가져 오는 중입니다. 저장하기 전에 이미지를 훨씬 작게 만들 수 있습니다. 너비와 높이가 자동으로 가장 높은 해상도로 설정되어 필요한 것보다 훨씬 큽니다. 나는 이미지를 얻고 크기를 변경 한 다음 저장하려고 시도했지만 오류가 발생했습니다.이미지의 크기를 변경하는 방법

ORIGINAL

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; 
      Stream stream; 

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

      //Convert the wb to a stream for saving 
      stream = new MemoryStream(wb.ToByteArray()); 

      //Need to replace the following line with the new image stream for saving? 
      //var capturedPicture = new CapturedPicture(e.OriginalFileName, e.ChosenPhoto); 
      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; 
    } 

편집 : 새로운 구현

private void cameraTask_Completed(object sender, PhotoResult e) 
    { 
     if (e.TaskResult == TaskResult.OK) 
     { 
      BitmapImage bmi = new BitmapImage(); 
      bmi.SetSource(e.ChosenPhoto); 

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

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

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

      stream.Seek(0, SeekOrigin.Begin); 

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

답변

0

, 당신은했다 만 일을 다시 크기의 이미지를 생성하는 오버로드 된 비트 맵 생성자를 사용하여 누락 된 이미지 데이터 형식은 다시 캐스팅되었습니다.

public static Image resizeImage(Image imgToResize, Size size) 
{ 
    return (Image)(new Bitmap(imgToResize, size)); 
} 

yourImage = resizeImage(yourImage, new Size(50,50)); 
+0

새 이미지에서'var capturedPicture = new CapturedPicture (e.OriginalFileName, i);'스트림을 만드는 방법은? 주목할 것은,'CapturedPicture'는 이미지 이름과 결과 스트림이 이미지를'IsolatedStorage'에 저장하도록 요구하는 클래스입니다. – Matthew

+0

또한, 나는 .Net 4.5를 Windows Phone 용으로 사용하고 있는데,'Bitmap'은 존재하지 않습니다. 그래서'BitmapImage'와'WriteableBitmap'을 사용하고있었습니다. – Matthew

+0

원래의 스트림 결과'e.ChosenPhoto'에'WriteableBitmap'을 새로운 크기로 직접 할당하기 위해 구현을 약간 변경했지만'할당되지 않은 지역 변수 'wb'사용이 발생하고 있다는 새로운 오류가 발생 했습니까? – Matthew