2017-11-28 6 views
0

ViewModel에서 캔버스를 렌더링하려고합니다. 내 VM에 캔버스를 통과 한 후 나는이 기능을 사용하여 렌더링이 잘 작동하고 준비() 함수하지만 모두가 시각적으로 내 UI를 나누기의 부모의 왼쪽 상단으로 이동 내 캔버스가 발생arrange()를 사용하지 않고 WPF UIElement 렌더링

private void GenerateTextures(object obj) 
{ 
    if (!(obj is UIElement)) 
     return; 

    UIElement control = (UIElement)obj; 

    for (int i = 0; i <= _textureCount - 1; ++i) 
    { 
     string path = OutPath + i.ToString() + TextureFormat; 
     FontText = i.ToString(); 

     using (FileStream fs = new FileStream(path,FileMode.Create)) 
     { 
      control.Measure(new Size(_textureSize, _textureSize)); 
      //control.Measure(size); 
      control.Arrange(new Rect(new Size(_textureSize, _textureSize))); 
      control.UpdateLayout(); 

      RenderTargetBitmap bitmap = new RenderTargetBitmap(_textureSize, _textureSize, 96, 96, PixelFormats.Pbgra32); 
      bitmap.Render(control); 
      BitmapEncoder encoder; 
      switch (TextureFormat) 
      { 
       case ".png": 
        encoder = new PngBitmapEncoder(); 
        break; 
       case ".jpg": 
        encoder = new JpegBitmapEncoder(); 
        break; 
       case ".gif": 
        encoder = new GifBitmapEncoder(); 
        break; 
       default: 
        encoder = new PngBitmapEncoder(); 
        break; 
      } 

      encoder.Frames.Add(BitmapFrame.Create(bitmap)); 
      encoder.Save(fs); 
     } 
    } 
} 

. arranged()에 대한 호출을 제거하면 렌더링 된 비트 맵이 cavas의 작은 모서리에 불과합니다.

내 cavas를 UI에서 이동하지 않고 어떻게 렌더링 할 수 있습니까?

답변

0

배치 할 때 캔버스의 위치를 ​​지정해야한다고 생각합니다. Rect의 생성자를 사용할 수 있습니다.

public Rect (포인트 위치, 크기 크기);

그래서 코드는 다음과 같을 수

control.Arrange(new Rect(new Point(X,Y), new Size(_textureSize, _textureSize))); 

또한, 당신이 조치를 호출해야하고 부모 레이아웃이 변경되거나 업데이트되면 코드를 배열합니다.

+0

팁을 주셔서 감사합니다. 측정을 이동하고 레이아웃을 업데이트 한 후 정렬하십시오. 내 캔버스에 관해서는, 'Point controlLoc = control.PointToScreen (new Point (0, 0));'을 사용하여 위치를 잡았고 다음과 같이 직사각형 위치를 설정했습니다 :'control.Arrange (new Rect (newLeft (controlLoc.X , controlLoc.Y), 새로운 크기 (_textureSize, _textureSize)); '하지만 캔버스가 사라지고 비트 맵이 완전히 투명 해집니다. 내가 뭘 잘못하고 있는거야? – Andronomos