简体   繁体   English

将Points的ArrayList转换为字节数组以存储在SQL数据库中

[英]Converting an ArrayList of Points to a byte array to store in a SQL database

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". 我设法通过一个包含一个“点”的arraylist的“Lines”的arraylist来做到这一点。 I need to store this information in a SQL database. 我需要将此信息存储在SQL数据库中。 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? 我该怎么做,还是有更好的方法?

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> ): 这是一个使用protobuf-net的工作版本(注意我将ArrayList更改为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);
        }
    }
}

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... 决定格式 :你可以使用像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. 如果使用基于文本(或XML)的格式,则序列化将生成一个字符串,您可以将其存储在SQL Server的varchar(MAX)text字段中。 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. 为此, varbinary(MAX)image是首选的数据类型。 Note that the name image might be misleading: This does not necessarily mean that your data is encoded as an "image" (jpeg, png, etc.). 需要注意的是名称image可能会产生误导:这并不一定意味着你的数据被编码为“图像”(JPEG,PNG等)。

Implementation : For XML and a standard binary encoding, .NET provides built-in functions. 实现 :对于XML和标准二进制编码,.NET提供内置函数。 Here's an introduction and links to walkthroughs on MSDN: 以下是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? 您的X和Y坐标是什么数据类型?

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. 对于我来说,将它存储为图像听起来非常合理。

create bitmap from array 从数组创建位图

Just create a black and white bitmap from your data and then convert your bitmap to PNG image using .NET library: 只需从数据中创建黑白位图,然后使用.NET库将位图转换为PNG图像:

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. 大多数数据库都有一种存储二进制数据的格式:PostgreSQL bytea,MySQL和Oracle blob,MSSQL图像等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM