Пример #1
0
        private void exportWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;

            int cc = int.Parse(tbStartNumber.Text);

            int progress = 0;
            var query =
                from p in photos
                where !p.Excluded
                group p by p.GroupNumber into g
                select new { GroupNumber = g.Key, Photos = g };

            StringBuilder sb = new StringBuilder();
            CSV csv = new CSV();
            csv.MakeHeader(sb);

            foreach (var group in query)
            {
                if (worker.CancellationPending)
                {
                    e.Cancel = true;
                    break;
                }
                else
                {
                    if (group.GroupNumber > 0)
                    {
                        // a group
                        Product prod = null;
                        foreach (var ph in group.Photos)
                        {
                            if (prod == null)
                            {
                                prod = new Product(ph);
                            }
                            else
                            {
                                prod.addPhoto(ph);
                            }
                            progress++;
                        }
                        sendToOutput(sb, csv, prod, tbCode.Text, cc++);
                        worker.ReportProgress(progress);
                    }
                    else
                    {
                        // group 0 - means photos outside groups
                        foreach (var ph in group.Photos)
                        {
                            Product prod = new Product(ph);
                            sendToOutput(sb, csv, prod, tbCode.Text, cc++);
                            progress++;
                            worker.ReportProgress(progress);
                        }
                    }
                }

            }

            // make cp1251 string
            String output = sb.ToString();

            Encoding te = Encoding.GetEncoding("windows-1251");
            Encoding se = Encoding.Unicode;

            byte[] sourceBytes = se.GetBytes(output);
            byte[] destBytes = Encoding.Convert(se, te, sourceBytes);

            char[] destChars = new char[te.GetCharCount(destBytes, 0, destBytes.Length)];
            te.GetChars(destBytes, 0, destBytes.Length, destChars, 0);
            output = new String(destChars);

            // remove '?'
            output = Regex.Replace(output, "\\?", "");

            String outfile = Path.Combine(target, "output.csv");
            TextWriter tw = new StreamWriter(outfile, false, te);
            tw.Write(output);
            tw.Close();

            e.Result = cc;
        }
Пример #2
0
        private void sendToOutput(StringBuilder sb, CSV csv, Product prod, String code, int cc)
        {
            try
            {
                // prepare product data for CSV
                Dictionary<String, String> pdata = new Dictionary<String, String>();
                String description = Regex.Replace(prod.Description, "\r|\n|\t", " ");
                description = Regex.Replace(description, "\\s{2,}", " ");
                description = Regex.Replace(description, "\"", "\"\"");

                String[] dparts = description.Split(" ".ToCharArray());
                int limit = dparts.Length < 5 ? dparts.Length : 5;
                String name = String.Join(" ", dparts, 0, limit);

                pdata.Add("description", description);
                pdata.Add("name", name);
                pdata.Add("code", code+cc);
                
                // process pictures
                String[] pics = prod.getPhotoNames();

                String fullcode = "ispc_" + code + cc;

                for (int i = 0; i < pics.Length; i++)
                {
                    Image img = Image.FromFile(Path.Combine(Storage, pics[i]));

                    // small thumbnail
                    String n1 = fullcode + "_" + i + "_th.jpg";
                    ImageConvert.MakeSmallThumbnail(img, Path.Combine(targetPP, n1));

                    // large thumbnail
                    String n2 = fullcode + "_" + i + ".jpg";
                    ImageConvert.MakeLargeThumbnail(img, Path.Combine(targetPP, n2));

                    // save original image
                    String n3 = fullcode + "_" + i + "_enl.jpg";
                    img.Save(Path.Combine(targetPP, n3));

                    pdata.Add("picture_" + (i + 1), n2 + "," + n1 + "," + n3);
                }

                csv.AddRow(sb, pdata);
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");
            }

        }