8 Replies - 795 Views - Last Post: 15 February 2013 - 06:23 AM Rate Topic: -----

#1 blaze007  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 13-February 13

how do i save data on inkcanvas into a text file?

Posted 14 February 2013 - 07:32 AM

i have to save the data written on inkcanvas by the user into a text file and then perform operations on the data saved.
Is This A Good Question/Topic? 0
  • +

Replies To: how do i save data on inkcanvas into a text file?

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8915
  • View blog
  • Posts: 33,427
  • Joined: 12-June 08

Re: how do i save data on inkcanvas into a text file?

Posted 14 February 2013 - 07:37 AM

You would need to devise a way to interpret the 'stroke' object into characters. That's all up on you.
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,649
  • Joined: 02-June 10

Re: how do i save data on inkcanvas into a text file?

Posted 14 February 2013 - 07:43 AM



Though it might be better to just take the image of the canvas and store it as a byte[]

Otherwise you're talking about having to OCR someone's handwriting and that just never works well.
Was This Post Helpful? 0
  • +
  • -

#4 blaze007  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 13-February 13

Re: how do i save data on inkcanvas into a text file?

Posted 14 February 2013 - 08:59 AM

namespace SaveStrokes
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            StrokeCollection sc = MyCanvas.Strokes;
            byte[] inkData = null;
            using (MemoryStream inkMemStream = new MemoryStream())
            {
                sc.Save(inkMemStream);
                inkData = inkMemStream.ToArray();
            }

            byte[] gifData = null;
            using (Microsoft.Ink.Ink ink = new Microsoft.Ink.Ink())
            {
                ink.Load(inkData);
                gifData = ink.Save(PersistenceFormat.Gif);
            }

            File.WriteAllBytes("c://strokes.gif", gifData);

        }

        public byte[] GetStrokesData(StrokeCollection strokes)
        {
            byte[] data = null;
            if (strokes.Count > 0)
            {
                MyCustomStrokes customStrokes = new MyCustomStrokes();
                customStrokes.StrokeCollection = new Point[strokes.Count][];
                for (int i = 0; i < strokes.Count; i++)
                {
                    customStrokes.StrokeCollection[i] = new Point[strokes[i].StylusPoints.Count];
                    for (int j = 0; j < strokes[i].StylusPoints.Count; j++)
                    {
                        customStrokes.StrokeCollection[i][j] = new Point();
                        customStrokes.StrokeCollection[i][j].X = strokes[i].StylusPoints[j].X;
                        customStrokes.StrokeCollection[i][j].Y = strokes[i].StylusPoints[j].Y;
                    }
                }
                //Serialize
                MemoryStream ms = new MemoryStream();
                BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(ms, customStrokes);
                data = ms.GetBuffer();
            }
            return data;
        }

        [Serializable]
        public sealed class MyCustomStrokes
        {
            public MyCustomStrokes() { }

            /// <summary>
            /// The first index is the stroke no.
            /// The second index is for the keep the 2D point.
            /// </summary>
            public Point[][] StrokeCollection;
        }
    }
}


I tried using the above code and it works well to save the data as a gif file.
Can anyone explain me whats the function of "public byte[] GetStrokesData(StrokeCollection strokes" method?

This post has been edited by modi123_1: 14 February 2013 - 09:03 AM
Reason for edit:: highlight the text and just click the 'code' button in the text editor

Was This Post Helpful? 0
  • +
  • -

#5 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8915
  • View blog
  • Posts: 33,427
  • Joined: 12-June 08

Re: how do i save data on inkcanvas into a text file?

Posted 14 February 2013 - 09:03 AM

... did you just randomly rip the code from here?
Was This Post Helpful? 0
  • +
  • -

#6 blaze007  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 13-February 13

Re: how do i save data on inkcanvas into a text file?

Posted 14 February 2013 - 09:12 AM

yes i did because i wanted to understand the use of public byte[] GetStrokesData(StrokeCollection strokes)
Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,649
  • Joined: 02-June 10

Re: how do i save data on inkcanvas into a text file?

Posted 14 February 2013 - 10:16 AM

Dissecting code that is more advanced than our current skills is a way that we all learn.
public byte[] GetStrokesData(StrokeCollection strokes)
        {
            byte[] data = null;
            if ( strokes.Count > 0)
            {
                MyCustomStrokes customStrokes = new MyCustomStrokes();
                customStrokes.StrokeCollection = new Point[strokes.Count][];
                for (int i = 0; i < strokes.Count; i++)
                {
                    customStrokes.StrokeCollection[i] = new Point[strokes[i].StylusPoints.Count];
                    for (int j = 0; j < strokes[i].StylusPoints.Count; j++)
                    {
                        customStrokes.StrokeCollection[i][j] = new Point();
                        customStrokes.StrokeCollection[i][j].X = strokes[i].StylusPoints[j].X;
                        customStrokes.StrokeCollection[i][j].Y = strokes[i].StylusPoints[j].Y;
                    }
                }
                //Serialize
                MemoryStream ms = new MemoryStream();
                BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(ms, customStrokes);
                data = ms.GetBuffer();
            }
            return data;
        }


What part of that method did you want to 'understand'?
Do you know what the access modifiers (public, private, protected) mean?
Did you not understand what a byte[] was - or that a byte[] is what that method returns?
What about the serializion of the data?
Or the custom class created to hold each stroke as a pair of Points
 [Serializable]
        public sealed class MyCustomStrokes
        {
            public MyCustomStrokes() { }

            /// <summary>
            /// The first index is the stroke no.
            /// The second index is for the keep the 2D point.
            /// </summary>
            public Point[][] StrokeCollection;
        }


Was This Post Helpful? 0
  • +
  • -

#8 blaze007  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 13-February 13

Re: how do i save data on inkcanvas into a text file?

Posted 14 February 2013 - 11:16 PM

can u explain the last three points of your post?
Was This Post Helpful? 0
  • +
  • -

#9 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,649
  • Joined: 02-June 10

Re: how do i save data on inkcanvas into a text file?

Posted 15 February 2013 - 06:23 AM

I'll take that to mean you didn't understand a great deal of the basic concepts of C#.

You need to stop dissecting other code or trying to build a program from scratch until you've taken the time to at least learn the ultra basics of the language.

We can't be a private tutor to everyone that can't be bothered with first reading "Learn C# in 30 days" from cover to cover.


My standard beginner resources post - Updated JAN 2013


Plan your study route:
There are three routes people seem to take when learning programming.
  • Just start trying to create programs
  • Start taking apart other programs and try to figure out the language by reverse engineering
  • Follow a guided learning course (school or self-teaching books)


For the life of me I can't figure out why people try 1 & 2. I strongly suggest taking the guided learning approach. Those book authors go in a certain order for a reason: They know what they're doing and they know the best order to learn the materials.

  • First learn the language by working 2-5 "Learn C# in 30 days" type books cover to cover.
  • Do a dozen on-line tutorial projects where you build what you're told to build, the way you are told to build it WITH AN EXPLANATION OF WHY so you can learn.
  • Learn to plan before you type.
  • THEN you start designing software with a purpose.



I don't learn from reading books: I learn by doing.
Spoiler


Newbie/Rookie said:

I have little/no programming experience but I need to write a program by Friday that does XYZ.
Spoiler



Resources, references and suggestions for new programmers.
Spoiler

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1