0

我想添加图像路径,因为我想在图像上制作曲线。我不知道如何为图像添加曲线。我想在图像上绘制形状并且不想使用画布。因为我想将线转换为光栅图像这是我的代码:

            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;
        }

我怎样才能做到这一点?

4

1 回答 1

2

图像不是容器控件。你肯定需要像 Canvas 这样的容器控件来保存你的形状。

或者,您可以使用 ImageBrush 将 BitmapImage 设置为 Canvas 的背景,并在 Canvas 上绘制路径/线条/椭圆,然后您可以将在 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);
    }
}
于 2013-07-26T06:09:19.037 回答