Exemple #1
0
        private void buttonResizeImage_Click(object sender, EventArgs e)
        {
            progressBar.Visible = true;
            int count = 0;

            using (Data countData = ShardDb.getImageCount())
            {
                if (!countData.Success)
                {
                    return;
                }
                count = Convert.ToInt16(countData.DataTable.Rows[0].ItemArray[0]);
            }
            MessageBox.Show("Total " + count + " images need to be resized.");
            int cursor = 0;

            while (cursor < count)
            {
                int end = 0;
                if ((count - cursor) > 50)
                {
                    end = cursor + 50;
                }
                else
                {
                    end = count;
                }
                using (Data imageData = ShardDb.getImages(cursor, end))
                {
                    if (!imageData.Success)
                    {
                        return;
                    }
                    Picture[] pictures = new Picture[imageData.DataTable.Rows.Count];
                    for (int i = 0; i < imageData.DataTable.Rows.Count; i++)
                    {
                        DataRow row = imageData.DataTable.Rows[i];
                        if (row.ItemArray[2] != DBNull.Value)
                        {
                            //resize file to normal and thumbnail
                            using (Bitmap bitmap = (Bitmap)Image.FromStream(new MemoryStream((byte[])row.ItemArray[2])))
                            {
                                Picture picture = new Picture();
                                picture.pic_image = getImage(bitmap, 800, 600);
                                picture.thumbnail = getImage(bitmap, 100, 100);
                                picture.pic_id    = Convert.ToInt16(row.ItemArray[1]);
                                picture.wd_id     = row.ItemArray[0].ToString();
                                pictures[i]       = picture;
                            }
                        }
                    }
                    if (ShardDb.updatePictures(pictures))
                    {
                        cursor            = end;
                        progressBar.Value = cursor * 100 / count;
                    }
                    pictures = null;
                    GC.Collect();
                }
            }
            MessageBox.Show("Image resize completed.");
            progressBar.Visible = false;
        }