2009-11-08 4 views
0

OpenFileDialog 컨트롤을 통해 사용자가 제출 한 Silverlight 3의 이미지의 크기를 조정하려고합니다. 파일 내용을 잡고 WriteableBitmap 개체에 넣은 다음 화면에 정확히 Image 컨트롤에 표시 할 수 있습니다. Image 컨트롤은 나를 위해 이미지 컨트롤의 크기에 맞게 크기를 조정합니다.Silverlight 3의 업로드 된 이미지 크기 조정

문제는 메모리 이미지가 원래 전체 해상도 이미지이므로, 픽셀 단위로 수행해야하는 값 비싼 작업이 많기 때문에 메모리 크기를 조정해야합니다. 지금까지 나는

public partial class MainPage : UserControl 
{ 
    public MainPage() 
    { 
     InitializeComponent(); 

     btnUploadPhoto.Click += new RoutedEventHandler(UploadPhoto_Click); 
    } 

    private void UploadPhoto_Click(object sender, RoutedEventArgs e) 
    { 
     OpenFileDialog dialog = new OpenFileDialog(); 
     dialog.Filter = "Image files (*.png;*.jpg;*.gif;*.bmp)|*.png;*.jpg;*.gif;*.bmp"; 

     if (dialog.ShowDialog() == true) 
     { 
      WriteableBitmap bitmap = new WriteableBitmap(500, 500); 
      bitmap.SetSource(dialog.File.OpenRead()); 

      imgMainImage.Source = bitmap; 

      txtMessage.Text = "Image size: " + bitmap.PixelWidth + " x " + bitmap.PixelHeight; 
     } 
    } 
} 

문제는 WriteableBitmap 클래스는 그 위에 크기 조정 방법이없는, 그리고 생성자의 높이와 너비를 설정하면 어떤 영향을 미칠 것 같지 않습니다 ... 다음과 같은 코드가 있습니다.

답변

2

당신이 할 수있는 일은 새로운 이미지 요소를 만들고 그 소스를 스트림에서 생성 된 쓰기 가능 비트 맵으로 설정하는 것입니다. 이 이미지 요소를 시각적 트리에 추가하지 마십시오. 또 다른 원하는 크기의 WriteableBitmap을 만듭니다. 그런 다음이 WriteableBitmap에서 Render를 호출하여 Image 요소와 ScaleTransform을 전달하여 이미지를 적절한 크기로 조정합니다. 그런 다음 두 번째 WriteableBitmap을 두 번째 Image 요소의 원본으로 사용하여 시각적 트리에 추가 할 수 있습니다. 그런 다음 첫 번째 Image 및 WriteableBitmap 객체를 GCed로 가져와 메모리를 다시 확보 할 수 있습니다.

+0

감사합니다. 이미지 리사이징/조작 기능이 .ResizeBitmap (int widget, int height) 형식의 상자에 기본적으로 포함되어 있지 않은 이유는 나를 놀라게합니다. 메서드가 WritableBitmap 클래스에 추가되었습니다. 상당히 일반적인 요구 사항처럼 보입니다. –

1

나는 약간의 성공과 함께 FJCore을 사용했다. 이것은 후방 출신의 오픈 소스 C# 이미징 툴킷이다. 메모리 내 크기 조정 기능을 포함합니다.

ImageMagick도 확인하십시오.

2

WriteableBitmapEx project을 보셨습니까? WriteableBitmap 클래스의 확장 메소드가있는 오픈 소스 프로젝트입니다. 다음은 크기를 조정하는 방법입니다.

BitmapImage image = new BitmapImage(); 
image.SetSource(dialog.File.OpenRead()); 

WriteableBitmap bitmap = new WriteableBitmap(image); 
WriteableBitmap resizedBitmap = bitmap.Resize(500, 500, WriteableBitmapExtensions.Interpolation.Bilinear); 

// For uploading 
byte[] data = resizedBitmap.ToByteArray(); 
관련 문제