Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Graphics
  4. Closing stream after calling Bitmap.Save

Closing stream after calling Bitmap.Save

Scheduled Pinned Locked Moved Graphics
graphicshelpquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jchalfant
    wrote on last edited by
    #1

    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?

    M C 2 Replies Last reply
    0
    • J jchalfant

      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?

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      This doesn't help much, but your code works fine for me - calling it multiple times. Mark

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      1 Reply Last reply
      0
      • J jchalfant

        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?

        C Offline
        C Offline
        Christian Graus
        wrote on last edited by
        #3

        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 )

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups