2013-07-26 5 views
0

이미지에 커브를 만들고 싶기 때문에 이미지에 경로를 추가하고 싶습니다. 커브를 이미지에 어떻게 추가 할 수 있는지 모르겠습니다. 나는 이미지 위에 도형을 그리고 캔버스를 사용하고 싶지 않습니다. wpf 및 C에서 BitmapImage에 도형을 추가하는 방법 #

  Ellipse circle = new Ellipse(); 
      circle.BeginInit(); 
      circle.Height = 100; 
      circle.Width = 100; 
      circle.Stroke = System.Windows.Media.Brushes.Black; 
      circle.StrokeThickness = 1.0; 
      circle.Margin = new Thickness(0, 0, 0, 0); 
      circle.EndInit(); 
      circle.Measure(new Size(200, 200)); 
circle.Arrange(
     new Rect(new Size(200, 200))); 
circle.UpdateLayout(); 
Line line = new Line(); 
line.BeginInit(); 
line.X1 = 0; 
line.Y1 = 0; 
line.X2 = 300; 
line.Y2 = 300; 
line.Stroke = System.Windows.Media.Brushes.Magenta; 
line.StrokeThickness = 1; 
line.EndInit(); 
line.Measure(new Size(300, 300)); 
line.Arrange(new 
     Rect(new Size(300, 300))); 
SolidColorBrush blueBrush = new SolidColorBrush(); 

blueBrush.Color = Colors.Blue; 

SolidColorBrush blackBrush = new SolidColorBrush(); 

blackBrush.Color = Colors.Black; 



// Create a Path with black brush and blue fill 

Path bluePath = new Path(); 
bluePath.BeginInit(); 

bluePath.Stroke = blackBrush; 

bluePath.StrokeThickness = 3; 

bluePath.Fill = blueBrush; 



// Create a line geometry 

LineGeometry blackLineGeometry = new LineGeometry(); 

blackLineGeometry.StartPoint = new Point(20, 200); 

blackLineGeometry.EndPoint = new Point(300, 200); 



// Create an ellipse geometry 

EllipseGeometry blackEllipseGeometry = new EllipseGeometry(); 

blackEllipseGeometry.Center = new Point(80, 150); 

blackEllipseGeometry.RadiusX = 50; 

blackEllipseGeometry.RadiusY = 50; 



// Create a rectangle geometry 

RectangleGeometry blackRectGeometry = new RectangleGeometry(); 

Rect rct = new Rect(); 

rct.X = 80; 

rct.Y = 167; 

rct.Width = 150; 

rct.Height = 30; 

blackRectGeometry.Rect = rct; 



// Add all the geometries to a GeometryGroup. 

GeometryGroup blueGeometryGroup = new GeometryGroup(); 

blueGeometryGroup.Children.Add(blackLineGeometry); 

blueGeometryGroup.Children.Add(blackEllipseGeometry); 

blueGeometryGroup.Children.Add(blackRectGeometry); 



// Set Path.Data 

bluePath.Data = blueGeometryGroup; 
bluePath.EndInit(); 
bluePath.Measure(new Size(300, 300)); 
bluePath.Arrange(new  Rect(new Size(300, 300))); 
      RenderTargetBitmap RTbmap = new 
       RenderTargetBitmap(200, 200, 96, 96, 
       PixelFormats.Default); 
      RTbmap.Render(bluePath); 
      var renderTargetBitmap = RTbmap; 
      var bitmapImage = new BitmapImage(); 
      var bitmapEncoder = new BmpBitmapEncoder(); 
      bitmapEncoder.Frames.Add(BitmapFrame.Create(renderTargetBitmap)); 

      using (var stream = new System.IO.MemoryStream()) 
      { 
       bitmapEncoder.Save(stream); 
       stream.Seek(0, System.IO.SeekOrigin.Begin); 
       bitmapImage.BeginInit(); 
       bitmapImage.CacheOption = BitmapCacheOption.OnLoad; 
       bitmapImage.UriSource = new Uri("C:\\Users\\ErnaGroup.Com\\Pictures\\Pictures\\cartoon-ice-cream-3 - Copy.jpg"); // I want to add line to this image. 
       bitmapImage.StreamSource = stream; 

       bitmapImage.EndInit(); 
      } 
      image1.Source = bitmapImage; 
     } 

나는이 어떻게 할 수있는

: 나는 래스터 이미지에 라인을 변환 할 때문에 내 코드는?

답변

2

이미지가 컨테이너 컨트롤이 아닙니다. 셰이프를 유지하려면 Canvas와 같은 컨테이너 컨트롤이 필요합니다.

또는 ImageBrush를 사용하여 Canvas의 배경으로 BitmapImage를 설정하고 Canvas에 경로/선/타원을 그리고 나중에 캔버스에 그림을 JPG 이미지로 저장할 수 있습니다.

<Canvas> 
    <Canvas.Background> 
     <ImageBrush ImageSource="Your BitmapImage Path"></ImageBrush> 
    </Canvas.Background> 
</Canvas> 

그럼 당신은 JPG 이미지

public static void CreateBitmapFromVisual(Visual target, string filename) 
{ 
    // target will be your Canvas 
    // filename is the path where you want to save the image 

    if (target == null) 
     return; 

    Rect bounds = VisualTreeHelper.GetDescendantBounds(target);    

    RenderTargetBitmap rtb = new RenderTargetBitmap((Int32)bounds.Width, (Int32)bounds.Height, 96, 96, PixelFormats.Default); 
    rtb.Render(target);    

    JpegBitmapEncoder jpg = new JpegBitmapEncoder(); 

    jpg.Frames.Add(BitmapFrame.Create(rtb)); 
    using (Stream stm = File.Create(filename)) 
    { 
     jpg.Save(stm); 
    } 
} 
로 수정 된 데이터를 저장할 수 있습니다
관련 문제