2012-03-28 2 views
1

잃고 있어요 내가 뭘 잘못하고 있니?컴파일러가 WP7에서 JpegBitmapEncoder 클래스를 찾을 수 없습니다

Error 3 The name 'BitmapFrame' does not exist in the current context

Error 2 The type or namespace name 'JpegBitmapEncoder' could not be found (are you missing a using directive or an assembly reference?)

코드 : WP7에

namespace Microsoft.Samples.CRUDSqlAzure.Phone.Converters 
{ 
using System; 
using System.Windows.Data; 
using System.IO; 
using System.Windows.Media.Imaging; 

public class ImageConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     byte[] rawImageBytes = (byte[])value; 
     BitmapImage imageSource = null; 

     try 
     { 
      using (MemoryStream stream = new MemoryStream(rawImageBytes)) 
      { 
       stream.Seek(0, SeekOrigin.Begin); 
       BitmapImage b = new BitmapImage(); 
       b.SetSource(stream); 
       imageSource = b; 
      } 
      return imageSource; 
     } 
     catch 
     { 
      return null; 
     } 


    } 

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     try 
     { 
      BitmapImage bitmapImage = (BitmapImage)value; 
      byte[] data; 
      JpegBitmapEncoder encoder = new JpegBitmapEncoder(); 
      encoder.Frames.Add(BitmapFrame.Create(bitmapImage)); 
      using (MemoryStream ms = new MemoryStream()) 
      { 
       encoder.Save(ms); 
       data = ms.ToArray(); 
      } 
      return data; 

     } 
     catch 
     { 
      return null; 
     } 
    } 
} 

}

답변

3

실버 라이트 JpegBitmapEncoder이 없습니다. 당신은 당신이 할 수있는 바이트 배열에 BitmapSource는 변환 할 경우 그 WriteableBitmap의 SaveJpeg 방법 사용 : 당신은 PNG와 같은 다른 파일 형식으로 BitmapSource는 변환하거나 타사 라이브러리를 사용할 필요가 GIF하려면

try 
{ 
    BitmapImage bitmapImage = (BitmapImage)value; 
    byte[] data; 
    WriteableBitmap wb = new WriteableBitmap(bitmapImage); 
    using (MemoryStream ms = new MemoryStream()) 
    { 
     wb.SaveJpeg(ms, bitmapImage.PixelHeight, bitmapImage.PixelWidth, 0, 100); 
     data = ms.ToArray(); 
    } 
    return data; 
} 
catch 
{ 
    return null; 
} 

.NET image tools입니다.

하지만 변환기에서 이미지를 앞뒤로 변환하는 것은 좋지 않습니다. 나는 네가 정말로 필요하다고 생각조차하지 않는다. 어떤 컨트롤을 사용하여 BitmapSource를 수정합니까? : \

+0

감사합니다. 내 xaml 페이지의 ''요소에 바인딩하려고합니다. 나는 바이트 []에 데이터를 저장 으로이 변환기를 사용해야합니다 – Michael

+0

난 아직도 당신이 뭘 하려는지 완전히 이해가 안 돼요. 그래서 당신은 당신의 이미지를 바이트 배열로 저장해야합니다 ... 글쎄요, 모든 것이 바이트 배열로 저장되므로 괜찮습니다. 당신은 이미지 컨트롤에 바인딩하고 싶습니다. 바이트 배열을 ImageSource로 변환하려면 변환기를 사용해야합니다. 그런데 왜 다시 변환해야합니까? –

+0

좋은 점은 실제로 생각하면, 데이터베이스에서 앱을 읽을 때 바이트 배열을 imagesource로 변환해야 할 때, 전화 카메라로 사진을 찍고 db로 저장하면 imagesource에서 이미지를 변환해야합니다. 바이트 배열. – Michael

관련 문제