# [.net] Bitmap Sequence creation

## Recommended Posts

I know this is a pretty demanding question, so thanks in advance for the suggestions. I need to create a long sequence of images (1000 bitmaps), do some manipulations to each image, and save them to disk for later viewing. I also save another representation of the bitmap sequence in a text file. The creation of the text file occurs in parallel with the sequence generation. I've implemented this in c# and it takes an incredibly long time (about 20 minutes for 1000 images). Below is the basic layout of my code. I can post the rest if needed, but it is quite long, so I'll start with this:

for (int i = 1; i < numberofimages + 1; i++)
{

Bitmap newbitmap = new Bitmap(bitmapwidth + 1, bitmapheight + 1);
Graphics newgraphic = Graphics.FromImage(newbitmap);
newgraphic.FillRectangle(Brushes.White, 0, 0, bitmapwidth + 1, bitmapheight + 1);

/* a large number of more fillrectangle calls are here, code not shown, usually around 300 fillrectangles per image */

newbitmap.Save(bitmapfileslocation + i + ".bmp", System.Drawing.Imaging.ImageFormat.Bmp);
bitmapSequenceToText.convertBitmapToText(newbitmap, bitmapfileslocation, bitmapheight, bitmapwidth);
System.Drawing.Imaging.ImageFormat.Bmp);
newbitmap.Dispose();
newgraphic.Dispose();

}


Now this is the interesting part. I did a few tests making 50 image sequences. This is how long it took to generate the sequences. As is: 32 seconds newbitmap.save commented out: 24 seconds (improvement of 8 seconds) convertBitmapToText commented out: 18 seconds (improvement of 14 seconds) save and convert commented out: <<<1sec (improvement of 32 seconds) What you'll notice is that for some reason the function calls seem to cooperatively slow down the process. What I mean is that the speed imrpovement that you see by removing both calls seems to improve the process more than the sum of the contribution of each call individually. Notice though, I'm not passing the saved bitmap to the convertBitmapToText function, I'm passing the bitmap object, so the two should work independently. the "bitmapfileslocation" only specifies where to save the text file. Are there any ideas as to why I'm seeing this phenomenon? My guess is that the performance slow down is due to the write time of the disk, with some kind of cooperative effect due to writing different file types or writing to different disk locations. Of course this is just a wild ass guess because I have no idea. You might be thinking that this performance difference is trivial, however, when I do 1000 bitmaps, the effect is very noticeable. When both of them are commented out, the process still takes less than a second, while commenting either individually makes only a modest improvement (20 minutes to ~15 minutes). Below is the code for the convertBitmapToText function, but I don't see how it could be signficant, so I'm posting it last.

public class bitmapSequenceToText
{

public static void convertBitmapToText(Bitmap convertedbitmap, string path, int bitmapheight, int bitmapwidth)
{
if (!File.Exists(path + "sequence.txt"))
{
using (StreamWriter sequence = File.CreateText(path + "sequence.txt"))
{

}
}

using (StreamWriter sequence = File.AppendText(path + "sequence.txt"))
{
for (int j = 0; j < (bitmapheight); j++)
{
for (int k = 0; k < (bitmapwidth); k++)
{
if (convertedbitmap.GetPixel(k, j).B == 0)
{
sequence.Write("A");
}

else
{
sequence.Write("B");
}
}
}
}
}
}



##### Share on other sites
My first guess is that getting a single pixel and writing a single character at the time is slowing down things considerably.

You might want to write to a memorybuffer first and at the end dump the memorystream in one call to disk.

The getpixel could be quite slow too. So if it's still slow after the previous fix you might want to investigate using unsafe access to the pixeldata so you can access the pixel data using pointers.

I'd try the buffer approach first.

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627689
• Total Posts
2978659

• 18
• 14
• 12
• 10
• 12