2013-06-10 4 views
0

나는 비트 맵 래퍼를위한 코드를 가지고있다. 원본 이미지에서 사각형을 자르고 _wbmp 안에 넣는 오버로드 생성자를 만들어야합니다.Silverlight : WriteableBitmap/BitmapImage에서 영역을 복사하는 방법?

공개 비트 맵과 비슷한 Smth (문자열 fileName, 사각형 영역). 몇 가지 해결책을 공유하십시오.

public Bitmap(string fileName) 
    { 
     Uri uri = new Uri(fileName, UriKind.RelativeOrAbsolute); 

     StreamResourceInfo sri = null; 
     sri = Application.GetResourceStream(uri); 

     // Create a new WriteableBitmap object and set it to the JPEG stream. 
     BitmapImage bitmap = new BitmapImage(); 
     bitmap.CreateOptions = BitmapCreateOptions.None; 
     bitmap.SetSource(sri.Stream); 
     _wbmp = new WriteableBitmap(bitmap); 
    } 

답변

1

WritableBitmap 개체가있는 일부 변환을 추가 한 후 새로운 비트 맵을 렌더링하는 데 사용할 수있는 방법을 렌더링 감사드립니다. 귀하의 경우에는 올바른 새로운 크기로 새로운 WritableBitmap을 작성하여 단추 오른쪽 구석을 설정 한 다음 소스와 함께 임시 이미지를 추가하고 왼쪽으로 변환하여 왼쪽 위 모서리를 설정하십시오. 다음과 같은 내용 :

public static WriteableBitmap CropBitmap(string fileName, int newTop, int newRight, int newBottom, int newLeft) 
    { 
     Uri uri = new Uri(fileName, UriKind.RelativeOrAbsolute); 

     StreamResourceInfo sri = null; 
     sri = Application.GetResourceStream(uri); 

     // Create a new WriteableBitmap object and set it to the JPEG stream. 
     BitmapImage bitmapImage = new BitmapImage(); 
     bitmapImage.CreateOptions = BitmapCreateOptions.None; 
     bitmapImage.SetSource(sri.Stream); 

     //calculate bounding box 
     int originalWidth = bitmapImage.PixelWidth; 
     int originalHeight = bitmapImage.PixelHeight; 

     int newSmallWidth = newRight - newLeft; 
     int newSmallHeight = newBottom - newTop; 

     //generate temporary control to render image 
     Image temporaryImage = new Image { Source = bitmapImage, Width = originalWidth, Height = originalHeight }; 

     //create writeablebitmap 
     WriteableBitmap wb = new WriteableBitmap(newSmallWidth, newSmallHeight); 


     wb.Render(temporaryImage, new TranslateTransform { X = -newLeft, Y = -newTop }); 
     wb.Invalidate(); 

     return wb; 
    } 
관련 문제