Пример #1
0
        /// <summary>
        /// Сохраняет вектор в формате .PCV (OPCV вида).
        /// </summary>
        /// <param name="FileName">Имя файла для загрузки.</param>
        private void SaveOPCV(string FileName)
        {
            string DirectoryName = FileName.Split('.').Reverse().ToArray()[1] + '\\';

            Directory.CreateDirectory(DirectoryName);
            var Include = new VectorIncludeFile()
            {
                Items = new List <VectorIncludeFileItem>()
            };
            var fileInfo = new VectorFileInfo()
            {
                DisplayName = FileName,
                Height      = (UInt16)Header.Height,
                VectType    = Header.VectType,
                Width       = (UInt16)Header.Width
            };

            Include.Items.Add(new VectorIncludeFileItem()
            {
                FileName = "fileInfo.xml", Type = VectorFileType.FileInfo
            });
            Include.Items.Add(new VectorIncludeFileItem()
            {
                FileName = "1.rawVect", Type = VectorFileType.Vector
            });
            int vectCounter = 2;

            if (RaswDataEX != null)
            {
                foreach (var item in RaswDataEX)
                {
                    string locFileName = vectCounter++ + ".rawVect";
                    Include.Items.Add(new VectorIncludeFileItem()
                    {
                        FileName = locFileName, Type = VectorFileType.Vector
                    });
                    using (FileStream fs = new FileStream(DirectoryName + locFileName, FileMode.OpenOrCreate))
                    {
                        var bytes = new RawVector(this, item).ToBytes();
                        fs.Write(bytes, 0, bytes.Length);
                    }
                }
            }
            if (Header.ExParams != null)
            {
                foreach (var item in Header.ExParams)
                {
                    Include.Items.Add(new VectorIncludeFileItem()
                    {
                        FileName = item.Key + "|" + item.Value, Type = VectorFileType.OtherData
                    });
                }
            }

            XmlSerializer fileInfoFormatter = new XmlSerializer(typeof(VectorFileInfo));

            using (FileStream fs = new FileStream(DirectoryName + "fileInfo.xml", FileMode.OpenOrCreate))
            {
                fileInfoFormatter.Serialize(fs, fileInfo);
            }
            using (FileStream fs = new FileStream(DirectoryName + "1.rawVect", FileMode.OpenOrCreate))
            {
                var bytes = new RawVector(this).ToBytes();
                fs.Write(bytes, 0, bytes.Length);
            }
            XmlSerializer includeFormatter = new XmlSerializer(typeof(VectorIncludeFile));

            using (FileStream fs = new FileStream(DirectoryName + "includes.xml", FileMode.OpenOrCreate))
            {
                includeFormatter.Serialize(fs, Include);
            }
            if (File.Exists(FileName))
            {
                File.Delete(FileName);
            }
            ZipFile.CreateFromDirectory(DirectoryName, FileName);
            foreach (var item in Directory.GetFiles(DirectoryName))
            {
                File.Delete(item);
            }
            Directory.Delete(DirectoryName);
        }
Пример #2
0
        /// <summary>
        /// Загружает вектор формата .PCV (OPCV вида).
        /// </summary>
        /// <param name="FileName">Имя файла для загрузки.</param>
        private void LoadVectOPCV(string FileName)
        {
            //Идея с локальной директорией неплоха, но неприятно, когда при открытие эксплоера, появляется папка, а потом удаляется -_-.
            //string DirectoryName = FileName.Split('.').Reverse().ToArray()[1] + '\\';

            //С папкой "где-то там", намного приятнее :З.
            //Рандом для того, чтобы минимизировать шанс коллизии, при открытии виндой.
            string DirectoryName = Path.GetTempPath() + string.Format("vect{0}\\", new Random().Next(0, 10000));

            if (Directory.Exists(DirectoryName))
            {
                foreach (var item in Directory.GetFiles(DirectoryName))
                {
                    File.Delete(item);
                }
                Directory.Delete(DirectoryName);
            }

            ZipFile.ExtractToDirectory(FileName, DirectoryName);

            VectorIncludeFile           Include          = null;
            VectorFileInfo              FileInfo         = null;
            RawVector                   Vector           = null;
            List <RawVector>            VectorsEx        = null;
            Dictionary <string, string> ExParams         = null;
            XmlSerializer               includeFormatter = new XmlSerializer(typeof(VectorIncludeFile));

            using (FileStream fs = new FileStream(DirectoryName + "includes.xml", FileMode.Open))
            {
                Include = (VectorIncludeFile)includeFormatter.Deserialize(fs);
            }
            foreach (var item in Include.Items)
            {
                switch (item.Type)
                {
                case VectorFileType.FileInfo:
                {
                    XmlSerializer fileInfoFormatter = new XmlSerializer(typeof(VectorFileInfo));
                    using (FileStream fs = new FileStream(DirectoryName + item.FileName, FileMode.Open))
                    {
                        FileInfo = (VectorFileInfo)fileInfoFormatter.Deserialize(fs);
                    }
                }
                break;

                case VectorFileType.Vector:
                {
                    if (Vector == null)
                    {
                        Vector = new RawVector(File.ReadAllBytes(DirectoryName + item.FileName));
                    }
                    else
                    {
                        if (VectorsEx == null)
                        {
                            VectorsEx = new List <RawVector>();
                        }
                        VectorsEx.Add(new RawVector(File.ReadAllBytes(DirectoryName + item.FileName)));
                    }
                }
                break;

                case VectorFileType.PenInfo:
                    break;

                case VectorFileType.OtherData:
                {
                    if (ExParams == null)
                    {
                        ExParams = new Dictionary <string, string>();
                    }
                    ExParams.Add(item.FileName.Split('|').First(), item.FileName.Split('|').Last());
                }
                break;

                default:
                    break;
                }
            }

            foreach (var item in Directory.GetFiles(DirectoryName))
            {
                File.Delete(item);
            }
            Directory.Delete(DirectoryName);
            if (Vector == null)
            {
                throw new ArgumentNullException(nameof(Vector));
            }
            if (FileInfo == null)
            {
                throw new ArgumentNullException(nameof(Vector));
            }
            RawData            = Vector.ToRawData();
            Header             = FileInfo.ToHeader();
            Header.CountOfCont = RawData.Length;
            if (VectorsEx != null)
            {
                RaswDataEX = VectorsEx.Select(p => p.ToRawData()).ToArray();
            }
            if (ExParams != null)
            {
                Header.ExParams = ExParams;
            }
        }