Reputation: 523
I have part of an application which is used to save a users signature on a hand-held device. I have managed to do this by having an arraylist of "Lines" which contains an arraylist of "points". I need to store this information in a SQL database. The only info I have seen to do this, is to use the image data type and pass it a byte array. There being the question. How would I do this, or is there a better way?
Upvotes: 3
Views: 2290
Reputation: 172270
If you convert this data into an image, some information (which point belongs to which line) is lost. If you want to keep this information, you should serialize your data.
Decide on the format: You could use something terribly verbose like XML...
<Line>
<Point X="3" Y="4" />
<Point X="3" Y="5" />
...
</Line>
<Line>
<Point X="10" Y="10" />
...
</Line>
...
...or some custom text-based format:
3,4-3,5-...;10,10-...;...
Of course, you'll be most efficient storing the data in some kind of binary encoding.
If you use a text- (or XML-)based format, your serialization will yield a string which you can store in a varchar(MAX)
or text
field in SQL Server. If you decide on a binary encoding, the serialization will result in a byte array. For this, varbinary(MAX)
or image
is the data type of choice. Note that the name image
might be misleading: This does not necessarily mean that your data is encoded as an "image" (jpeg, png, etc.).
Implementation: For XML and a standard binary encoding, .NET provides built-in functions. Here's an introduction and links to walkthroughs on MSDN:
If you decide on some custom text-based or custom binary encoding, you will need to code the serialization and deserialization parts yourself. If you want help with that, you need to provide some more information: How does your data structure look exactly? What data types are your X and Y coordinates?
Upvotes: 2
Reputation: 23890
Just create a black and white bitmap from your data and then convert your bitmap to PNG image using .NET library:
MemoryStream pngbuffer = new MemoryStream();
Bitmap.Save(pngbuffer,ImageFormat.Png)
And store it as a binary data. Most databases have a format for storing binary data: PostgreSQL bytea, MySQL and Oracle blob, MSSQL image etc.
Upvotes: 0
Reputation: 1062865
It sounds like you want some form of binary serialization; here is a working version using protobuf-net (note I changed ArrayList
to List<T>
):
using System.Collections.Generic;
using System.IO;
using ProtoBuf;
[ProtoContract]
class Line
{
private readonly List<Point> points = new List<Point>();
[ProtoMember(1, DataFormat = DataFormat.Group)]
public List<Point> Points { get { return points; } }
}
[ProtoContract]
class Point
{
[ProtoMember(1)]
public int X { get; set; }
[ProtoMember(2)]
public int Y { get; set; }
}
static class Program
{
static void Main()
{
var lines = new List<Line> {
new Line { Points = {
new Point { X = 1, Y = 2}
}},
new Line { Points = {
new Point { X = 3, Y = 4},
new Point { X = 5, Y = 6}
}},
};
byte[] raw;
// serialize
using (var ms = new MemoryStream())
{
Serializer.Serialize(ms, lines);
raw = ms.ToArray();
}
List<Line> clone;
// deserialize
using (var ms = new MemoryStream(raw))
{
clone = Serializer.Deserialize<List<Line>>(ms);
}
}
}
Upvotes: 2
Reputation: 4481
So you let the user create a bitmap-graphic by signing with a stylus? Sounds perfectly reasonable to me to store this as an image.
Upvotes: 1