保存鼠标事件图形以备将来修改 [英] Save my mouse event drawing for future modification

查看:84
本文介绍了保存鼠标事件图形以备将来修改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用(Visual Studio,Windows窗体,C#)创建了一些图形。

I have used (visual studio, windows form, c#) to create some drawings.

我的目标是添加保存按钮以按原样保存图形,然后以后当我打开保存的文件时,我可以继续我的旧工作...

My goal is to add save button to save the drawing as it is and when I open the saved file in future I can continue my old work...

现在发生的事情是每次打开Visual Studio时我都必须重绘所有内容。 / p>

What happen now is every time I open visual studio I have to redraw every thing .

推荐答案

第一个任务是收集您在 List< T>
有关如何收集它们的代码,请参阅(全部)我的评论在这里或Reza的回答在这里。

The first task is collecting the data you draw in a List<T>. For code on how to collect them see (all) my comments here or Reza's answer here.

这里是保存&加载简单的 PointF 列表,可用于绘制曲线:

Here is an example to save & load simple PointF lists you can use to draw curves:

using System.IO;
using System.Xml.Serialization;

// all drawn curve points are collected here:
List<List<PointF>> curves = new List<List<PointF>>();



private void SaveButton_Click(object sender, EventArgs e)
{

    XmlSerializer xmls = new XmlSerializer(typeof(List<List<PointF>>));
    using (Stream writer = new FileStream(yourDrawingFileName, FileMode.Create))
    {
        xmls.Serialize(writer, curves);
        writer.Close();
    }
}

private void LoadButton_Click(object sender, EventArgs e)
{
    if (File.Exists(yourDrawingFileName))
    {

        XmlSerializer xmls = new XmlSerializer(typeof(List<List<PointF>>));
        using (Stream reader = new FileStream(yourDrawingFileName, FileMode.Open))
        {
            var curves_ = xmls.Deserialize(reader);
            reader.Close();
            curves = (List<List<PointF>>) curves_;
            Console.Write(curves.Count + " curves loaded.");
        }
    }
    yourPanelOrPictureBoxOrForm.Invalidate;
}

如果要保存更复杂的绘图动作类,请替换 PointF yourClass 。确保该类可序列化! (很好, ints 字符串当然是好; 颜色需要一点帮助。.

If you want to save a more complex class of drawing actions replace PointF by yourClass. Make sure the class is serializable! (Points are fine, ints and strings of course as well; Colors need a little help..)

有关如何设计更复杂的绘制动作类的提示请参见此处

For hints on how to design a more complex draw action class see here

这篇关于保存鼠标事件图形以备将来修改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆