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. C#
  4. C# -Savefile Dialog to save an image file

C# -Savefile Dialog to save an image file

Scheduled Pinned Locked Moved C#
csharpgraphicshelp
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.
  • V Offline
    V Offline
    vijaywithu
    wrote on last edited by
    #1

    hi friends I am new to .NET, I have the following code to save an image file in specific location. It does compile and it is showing that null reference eexception is unhandled it is asking to create an object instance in this line (System.Drawing.Imaging.ImageFormat) how can it be done any help please SaveFileDialog save = new SaveFileDialog(); save.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif"; save.Title = "Save the File"; save.ShowDialog(); string fName = save.FileName; if (save.FileName != "") { System.IO.Stream fileStream = (System.IO.FileStream)save.OpenFile(); switch (save.FilterIndex) { case 1: this.button7.Image.Save(fileStream, System.Drawing.Imaging.ImageFormat.Jpeg); break; case 2: this.button7.Image.Save(fileStream, System.Drawing.Imaging.ImageFormat.Gif); break; case 3: this.button7.Image.Save(fileStream, System.Drawing.Imaging.ImageFormat.Bmp); break; } fileStream.Close(); } }

    OriginalGriffO R 2 Replies Last reply
    0
    • V vijaywithu

      hi friends I am new to .NET, I have the following code to save an image file in specific location. It does compile and it is showing that null reference eexception is unhandled it is asking to create an object instance in this line (System.Drawing.Imaging.ImageFormat) how can it be done any help please SaveFileDialog save = new SaveFileDialog(); save.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif"; save.Title = "Save the File"; save.ShowDialog(); string fName = save.FileName; if (save.FileName != "") { System.IO.Stream fileStream = (System.IO.FileStream)save.OpenFile(); switch (save.FilterIndex) { case 1: this.button7.Image.Save(fileStream, System.Drawing.Imaging.ImageFormat.Jpeg); break; case 2: this.button7.Image.Save(fileStream, System.Drawing.Imaging.ImageFormat.Gif); break; case 3: this.button7.Image.Save(fileStream, System.Drawing.Imaging.ImageFormat.Bmp); break; } fileStream.Close(); } }

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      I think the error is here:

      vijaywithu wrote:

      System.IO.Stream fileStream = (System.IO.FileStream)save.OpenFile();

      For whatever reason, the OpenFile has returned a null Stream (Did you press cancel?)

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      1 Reply Last reply
      0
      • V vijaywithu

        hi friends I am new to .NET, I have the following code to save an image file in specific location. It does compile and it is showing that null reference eexception is unhandled it is asking to create an object instance in this line (System.Drawing.Imaging.ImageFormat) how can it be done any help please SaveFileDialog save = new SaveFileDialog(); save.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif"; save.Title = "Save the File"; save.ShowDialog(); string fName = save.FileName; if (save.FileName != "") { System.IO.Stream fileStream = (System.IO.FileStream)save.OpenFile(); switch (save.FilterIndex) { case 1: this.button7.Image.Save(fileStream, System.Drawing.Imaging.ImageFormat.Jpeg); break; case 2: this.button7.Image.Save(fileStream, System.Drawing.Imaging.ImageFormat.Gif); break; case 3: this.button7.Image.Save(fileStream, System.Drawing.Imaging.ImageFormat.Bmp); break; } fileStream.Close(); } }

        R Offline
        R Offline
        Roman Lerman
        wrote on last edited by
        #3

        Two things: 1: SaveFileDialog save = new SaveFileDialog(); save.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif"; save.Title = "Save the File"; if(save.ShowDialog() == DialogResult.OK) Use this to prevent exception on Cancel click { string fName = save.FileName; if (save.FileName != "") { System.IO.Stream fileStream = (System.IO.FileStream)save.OpenFile(); switch (save.FilterIndex) { case 1: this.button7.Image.Save(fileStream, System.Drawing.Imaging.ImageFormat.Jpeg); break; case 2: this.button7.Image.Save(fileStream, System.Drawing.Imaging.ImageFormat.Gif); break; case 3: this.button7.Image.Save(fileStream, System.Drawing.Imaging.ImageFormat.Bmp); break; } fileStream.Close(); } } } 2: Set Buttons Image Property !!!

        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