Closing stream after calling Bitmap.Save
-
This is probably a simple problem as the most I've ever done with GDI was a simple ray tracer that drew a circle on a flat plain using drawline ALOT. Needless to say... I'm a GDI nub. So anyway, I tried to use Bitmap.Save to take a screenshot of each of the windows on a WinForm application. This works great on the first run, but if I run the function a second time an exception is thrown where I called the save. I decided to use a filestream so I'd have control over the stream instead of passing a pathname. Even though I close the stream when I'm done with it, it throws a file in use exception when I try to create the stream on the second go around. Here's my current code:
Bitmap mybmp = new Bitmap(formwidth, formheight,PixelFormat.Format32bppArgb) // //code to capture image // System.IO.FileStream fs = System.IO.File.Open(imagePath, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite); mybmp.Save(fs, ImageFormat.Jpeg); fs.Close();
Am I missing something here? -
This is probably a simple problem as the most I've ever done with GDI was a simple ray tracer that drew a circle on a flat plain using drawline ALOT. Needless to say... I'm a GDI nub. So anyway, I tried to use Bitmap.Save to take a screenshot of each of the windows on a WinForm application. This works great on the first run, but if I run the function a second time an exception is thrown where I called the save. I decided to use a filestream so I'd have control over the stream instead of passing a pathname. Even though I close the stream when I'm done with it, it throws a file in use exception when I try to create the stream on the second go around. Here's my current code:
Bitmap mybmp = new Bitmap(formwidth, formheight,PixelFormat.Format32bppArgb) // //code to capture image // System.IO.FileStream fs = System.IO.File.Open(imagePath, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite); mybmp.Save(fs, ImageFormat.Jpeg); fs.Close();
Am I missing something here?This doesn't help much, but your code works fine for me - calling it multiple times. Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
This is probably a simple problem as the most I've ever done with GDI was a simple ray tracer that drew a circle on a flat plain using drawline ALOT. Needless to say... I'm a GDI nub. So anyway, I tried to use Bitmap.Save to take a screenshot of each of the windows on a WinForm application. This works great on the first run, but if I run the function a second time an exception is thrown where I called the save. I decided to use a filestream so I'd have control over the stream instead of passing a pathname. Even though I close the stream when I'm done with it, it throws a file in use exception when I try to create the stream on the second go around. Here's my current code:
Bitmap mybmp = new Bitmap(formwidth, formheight,PixelFormat.Format32bppArgb) // //code to capture image // System.IO.FileStream fs = System.IO.File.Open(imagePath, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite); mybmp.Save(fs, ImageFormat.Jpeg); fs.Close();
Am I missing something here?If you were to open the same file using Bitmap bm = new Bitmap(imagePath) somewhere, THEN it would be locked as long as that bitmap was open. Using a filestream to open a bitmap and closing the stream, solves the problem.
Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )