Question

I tried to save all blobs into png picture but when I named the png with current time stamp, it seem like the saving process has timeout and only success for the last blob. If I put 2 seconds delay, only it can save all.

reportthread_image is List<byte[]> of blob (in this case is 4 blobs) retrieved from mysql database.

foreach (byte[] rt_image in reportthread_image)
{
    if (rt_image.Count() != 0)
    {
        try
        {
            ms = new MemoryStream(rt_image);
            fullsizeImage = Image.FromStream(ms);
            newImage = fullsizeImage.GetThumbnailImage(pictureBox1.Width, pictureBox1.Height, null, IntPtr.Zero);
            pictureBox1.Image = newImage;
            bm = new Bitmap(pictureBox1.Image);
            count++;
            //only can save last image without delay
            //Thread.Sleep(2000); //need long enough delay to save all
            //bm.Save(DateTime.Now.ToString("yyMMddHHmmss") + ".png", ImageFormat.Png);//can save all blobs to png.
            bm.Save(count + ".png", ImageFormat.Png);//no error will occurs here.
            ms.Dispose();
            bm.Dispose();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }
}

May I know that how can I save the image with current timestamp and only proceed to next element after finish the saving process in the reportthread_image? I tried lock(), I tried Invoked()

and I actually cannot understand

why the process is not wait the save process finish only proceed to next line??

Était-ce utile?

La solution

Because you overwrite your previous saved file. Include milliseconds in file name and be happy.

bm.Save(DateTime.Now.ToString("yyMMddHHmmssfff") + ".png", ImageFormat.Png);

I think it is not good idea to sleep thread. To be more accurate, you must use guaranteed changeable factor in your generated name, because DateTame hasn't enough accuracy. Something like this:

bm.Save(DateTime.Now.ToString("yyMMddHHmmss") + "_" + count++ + ".png", ImageFormat.Png);

Then

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top