2014-02-10 5 views
0

플러드 채우기 도구를 WPF 페인트 프로그램에 구현하려고합니다. 모든 드로잉 작업을 먼저 비트 맵에 작성하여 벡터 메서드를 무시하려고합니다. 그러면 비트 맵이 캔버스로 설정됩니다.이미지 추가

이미지를 캔버스에 전달할 때 문제가 있습니다. 상대 위치를 사용하여 이미지를 설정하지 않습니다. 이미지는 내가 할 수있는 것보다 훨씬 잘 설명 할 것이다. 채우기 전에

:

Before Fill

채우기 후 :

enter image description here

이미지 비트 맵은 창으로 상대를 작성하고 도구 모음을 겹쳐있다. 나는 이것이 어떻게되는 것을 막을 수 있는지 궁금합니다. 첨부 된 필자 클래스의 관련 코드입니다.

public override void OnMouseDown(CCDrawingCanvas canvas, System.Windows.Input.MouseButtonEventArgs e) { 
     double dpi = 96d; 

     // Get the size of the canvas 
     System.Windows.Size size = new System.Windows.Size((int)canvas.ActualWidth, (int)canvas.ActualHeight); 

     // Measure and arrange the surface 
     canvas.Measure(size); 
     canvas.Arrange(new Rect(size)); 

     RenderTargetBitmap source = new RenderTargetBitmap(
      (int)canvas.ActualWidth, 
      (int)canvas.ActualHeight, 
      dpi, 
      dpi, 
      PixelFormats.Pbgra32); 
     source.Render(canvas); 

     WriteableBitmap modifiedImage = new WriteableBitmap(source); 
     int h = modifiedImage.PixelHeight; 
     int w = modifiedImage.PixelWidth; 
     int[] pixelData = new int[h * w]; 
     int widthInByte = modifiedImage.PixelWidth * (modifiedImage.Format.BitsPerPixel/8); 

     modifiedImage.CopyPixels(pixelData, widthInByte, 0); 

     int oldColor = BitConverter.ToInt32(new byte[] { System.Drawing.Color.White.B, System.Drawing.Color.White.G, System.Drawing.Color.White.R, System.Drawing.Color.White.A }, 0); 
     int newColor = BitConverter.ToInt32(new byte[] { System.Drawing.Color.Black.B, System.Drawing.Color.Black.G, System.Drawing.Color.Black.R, System.Drawing.Color.Black.A }, 0); 

     // Perform the recursive fill 
     FloodFill(pixelData, (int)p.X, (int)p.Y, w, h, oldColor, newColor); 

     modifiedImage.WritePixels(new Int32Rect(0, 0, w, h), pixelData, widthInByte, 0); 

     newFill = new GraphicsFill(modifiedImage); 

     // Adds newFill to canvas.GraphicsList 
     AddObject(canvas, newFill); 

    } 

XAML 코드 : WPF에서

<Window x:Class="ccGui.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:effects="clr-namespace:System.Windows.Media.Effects;assembly=presentationcore" 
    xmlns:lib="clr-namespace:ccDrawingLib;assembly=ccDrawingLib" 
    xmlns:local="clr-namespace:ccGui" 
    Title="MainWindow" Height="600" Width="800"> 

<DockPanel> 
    <Menu DockPanel.Dock="Top"> 
     <MenuItem Header="File"> 
      <MenuItem Header="New" Command="ApplicationCommands.New" /> 
      <MenuItem Header="Open" Command="ApplicationCommands.Open" /> 
      <MenuItem Header="Save" Command="ApplicationCommands.Save" /> 
      <MenuItem Header="Save As" Command="ApplicationCommands.SaveAs" /> 
      <MenuItem Header="Close" Command="ApplicationCommands.Close" /> 
     </MenuItem> 
     <MenuItem Header="Tool" Name="menuTools"> 
      <MenuItem Header="Brush" Name="ccToolBrush" Tag="Brush" /> 
      <MenuItem Header="Fill" Name="ccToolFill" Tag="Fill" /> 
     </MenuItem> 
    </Menu> 
    <lib:CCDrawingCanvas x:Name="canvas" Background="White" /> 
</DockPanel> 
</Window> 
+1

새 개체가 자식 대신 부모 또는 루트 Visual에 추가되는 것처럼 보이는 현상이 거의 보입니다. 일반적으로 레이아웃 시스템은 일반적으로 잘못된 부모/비주얼/컨테이너에 추가하거나 시도하지 않는 한 비 상대 레이아웃을 방지합니다. 관련 XAML도 공유 할 수 있습니까? –

+1

모양 클래스를 사용하고 [RenderTargetBitmap.Render'] (http://msdn.microsoft.com/en-us/library/system)를 사용하여 이미지를 저장할 때 수동으로 모든 것을 수동으로 수행하는 이유는 무엇입니까? .windows.media.imaging.rendertargetbitmap.render.aspx) 방법? – Sheridan

+0

@JTrana 곧 게시 할 예정입니다. – Alex

답변

1

는 거의 모든 UI 컨트롤은 Visual 클래스를 확장합니다. 해당 Visual 참조를 사용하여 RenderTargetBitmap.Render Method으로 전달하면 해당 UI 컨트롤의 이미지를 쉽게 저장할 수 있습니다. 귀하의 Shape을 채우기 위해 ShapeBitMapImage 사이를 전환하지 않아도됩니다. 모양을 그릴 때 Path을 사용하고있는 것 같습니다. 이것이 사실이라면 수동 계산을하지 않고도 Path.Fill 속성을 사용하여 입력 할 수 있습니다.

또한 각각의 새로운 그려진 도형에 Panel.Zindex Attached Property을 설정하고 그 다음으로 더 높은 Zindex 값을 사용하면 최신 표준 도형이 항상 드로잉 표준 프로그램에서와 같이 맨 아래에 배치됩니다. 사용자는이 속성을 사용하여 그린 그림의 모양 레이어를 전환 할 수도 있습니다.