在图像上绘制画布线

时间:2015-03-12 10:49:08

标签: c# wpf canvas

我必须在我的WPF应用程序中执行一项简单的任务。它基于Canvas Control上的绘制线。代码工作正常,直到我正在为这个画布控件添加一个Image,并在其上进行相同的思考。

让我们看看代码:

void loadImage()
{
    Image image = new Image();

    System.Windows.Forms.OpenFileDialog ofd = new System.Windows.Forms.OpenFileDialog
    {
        Filter = @"Images|*.png;*.jpg;*.jpeg;*.bmp;*.gif"
    };
    if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {
        System.IO.FileInfo fileInfo = new System.IO.FileInfo(ofd.FileName);
        BitmapImage src = new BitmapImage();
        src.BeginInit();
        src.CacheOption = BitmapCacheOption.OnLoad;
        src.UriSource = new Uri(fileInfo.FullName, UriKind.Relative);
        src.EndInit();
        image.Source = src;
        image.Stretch = Stretch.Uniform;

        CanvasE.Children.Add(image); <- Canvas object with my image

        int zindex = CanvasE.Children.Count;
        Panel.SetZIndex(image, zindex);
        Canvas.SetLeft(image, 50);
        Canvas.SetTop(image, 50);
    }
}

我的画布控件上的事件代码,它在绘图时作出响应。

private void CanvasE_MouseDown(object sender, MouseButtonEventArgs e)
{
    if (e.ButtonState == MouseButtonState.Pressed)
        currentPoint = e.GetPosition(this);
}

private void CanvasE_MouseMove(object sender, MouseEventArgs e)
{
    if (e.LeftButton == MouseButtonState.Pressed)
    {
        Line line = new Line();

        line.Stroke = SystemColors.WindowFrameBrush;
        line.X1 = currentPoint.X;
        line.Y1 = currentPoint.Y;
        line.X2 = e.GetPosition(this).X;
        line.Y2 = e.GetPosition(this).Y;

        currentPoint = e.GetPosition(this);

        CanvasE.Children.Add(line);
    }
}

现在问题。 如上所述,如果我们以该形式将图像添加到Canvas Control,则会显示图形。我的问题在于分层子对象。

如何绘制以后保存图像和绘制线?

0 个答案:

没有答案
相关问题