CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Nov 2004
    Posts
    37

    using a savefilediaglog box

    Hi guys,

    How do i get a picture to be saved when a button is clicked?

  2. #2
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    Re: using a savefilediaglog box

    images (bitmaps maybe) have a Save function that will let you save them as an image file. use the save file dialog to get the name to save it.

  3. #3
    Join Date
    Nov 2004
    Posts
    37

    Re: using a savefilediaglog box

    i want to save jpgs , but what is the actual code to do this i am very new to it all.

    Thanks

  4. #4
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Thumbs up Re: using a savefilediaglog box

    This is the link that will help you do what you want. The code here is in VB.Net so you would need to change it into the corresponding C# version.

    Here's another way of doing this:

    Now if you are providing a link button (which I am not sure because you just said a button, and not command button or link button) could do this by just providing the link to the download option. So suppose you have a link button to your whatever file and you want the user to be able to view the save dialog box and then save the whatever file linked there to whatever location. It will automatically open the save dialog box, nothing else would be needed.

    But - sometimes this may not happen - because of MIME (Multipurpose Internet Mail Extensions) which enables your browser to automatically display the content of many different (but known to your browser) formats within the browser window itself. Now, this is not what you might have intended..right? So you need to over-ride the browser's capabilties to render the known MIME types itself..For this you would need to use a content disposition header in your web-page.

    Add this script to your asp page :
    Code:
    response.addHeader "content-disposition", "attachment;filename=filename.ext"
    Just replace filename <filename> and extension <ext> for the file you have and its all done. If this still doesnt work trying reading up on content disposition headers and try some searching on Google. Hope this helps and dont forget to rate good/helpful posts.

    P.S. - this post should have had been on the ASP.NET forum and not the C# programming one.
    Last edited by exterminator; July 20th, 2005 at 04:30 AM. Reason: mentioned the same link twice

  5. #5
    Join Date
    Nov 2004
    Posts
    37

    Re: using a savefilediaglog box

    hi,

    Im looking for it to be done using a normal button in c# , i dont need to connect online or anything.

    All i want to do is save a picture which is in a picture box in my form when the user clicks a button on the form.

    This shoudl be very simple but because im new i dont know how to do it.

    Thanks


    ps, what is asp.net isnt that online stuff?

  6. #6
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Thumbs up Re: using a savefilediaglog box

    Okay..so you are working on windows forms. Go through one (or more) of these to get to know how to use the Save File Dialog box.I am sure you can get help about how to go about with this. The msdn link is good. Also its explains with code for a RichTextBox, I think you could reuse that with some changes like picture box instead of rich text box control.

    Hope this helps and dont forget to rate good/helpful posts.

  7. #7
    Join Date
    Nov 2004
    Posts
    37

    Question Re: using a savefilediaglog box

    hi, im really new to this im still abit confused im not sure how to link in some way the saveing of the picture an dthe button click and the savedialog.

    I know when the button is clicked how to get the save diaglog to pop up and hence let me save the file where i want but i dont know how to make it save the actuall picture which is in the picture box.

    Any ideas??


    ps, if anyone can help with my other threads pls do also if you can give me info on gif files, pls do.

    Thanks

  8. #8
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    Re: using a savefilediaglog box

    Code:
    private void button2_Click(object sender, System.EventArgs e) {
        if(saveFileDialog1.ShowDialog(this) == DialogResult.OK) {
            using ( Stream s = File.Create(saveFileDialog1.FileName) ) {
                s.Write(buffer, 0, buffer.Length);
            }
        }
    }

  9. #9
    Join Date
    Nov 2004
    Posts
    37

    Re: using a savefilediaglog box

    as ive tried to explain i am really new to this os some comments with this code might help me.

    Thanks

  10. #10
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    Re: using a savefilediaglog box

    Code:
    private void button2_Click(object sender, System.EventArgs e) {
        if(saveFileDialog1.ShowDialog(this) == DialogResult.OK) {
            using ( Stream s = File.Create(saveFileDialog1.FileName) ) {
                s.Write(buffer, 0, buffer.Length);
            }
        }
    }

    private void button2_Click(object sender, System.EventArgs e) {
    this code is the function for the button press event handler

    if(saveFileDialog1.ShowDialog(this) == DialogResult.OK) {
    first:
    saveFileDialog1.ShowDialog(this)
    this code is what actually displays the save file dialog.
    second:
    == DialogResult.OK
    after the file name is selected, ShowDialog returns a DialogResult enumeration specifying what the user did (pressed ok or cancel or whatever else the user can do). in this case if the user selected a file & pressed ok,
    third:
    saveFileDialog1.FileName
    this is the actual name of the file they chose. the rest of the code is just the convention to open up a file stream and write the image bytes to a file.

    using (stream ... ) {

    }
    allows for the file stream to be created and deleted automagically.

    s.Write(buffer, 0, buffer.Length)
    is the code to write the image data to the file.


    I dont know exactly how you're writing the image to a file, but i hope this helps.
    Last edited by MadHatter; July 20th, 2005 at 02:13 PM.

  11. #11
    Join Date
    Nov 2004
    Posts
    37

    Re: using a savefilediaglog box

    Hi,

    I dont think i need to use streams do i?

    here is what i am trying to do i think it must be on the right lines.



    //code for the save gif button
    private void button5_Click(object sender, System.EventArgs e)
    {string chosensave;
    saveFileDialog1.InitialDirectory = @"c:\";
    saveFileDialog1.ShowDialog();
    chosensave = saveFileDialog1.FileName;
    pictureBox1.Image.Save(chosensave);
    //saveFileDialog1
    }

    this is the error it gives me

    An unhandled exception of type 'System.NullReferenceException' occurred in Giffed.exe

    Additional information: Object reference not set to an instance of an object.

  12. #12
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Thumbs up Re: using a savefilediaglog box

    I will try a little modification in MadHatter's code. I am sure this is going to work if you have got everything else correct.
    Quote Originally Posted by MadHatter
    Code:
    private void button2_Click(object sender, System.EventArgs e) {
        if(saveFileDialog1.ShowDialog(this) == DialogResult.OK) {
              System.Drawing.Imaging.ImageFormat  myGifFormat;
              //System.Drawing.Imaging.ImageFormat.Gif is a public static get which returns System.Drawing.Imaging.ImageFormat type
              myGifFormat = System.Drawing.Imaging.ImageFormat.Gif;
              pictureBox1.Image.Save(saveFileDialog1.FileName, myGifFormat);
        }
    }
    In your code, you should try getting what the user has chosen, you dont want to save the file if he chose No or cancel ..would you? so the following if-block is essential in the above code-
    Code:
    if(saveFileDialog1.ShowDialog(this) == DialogResult.OK)
    Object reference not set to an instance of an object - this is a very generic error message and can cause due to many, many reasons. Try debugging your code and find out where , at what line, at what method call or property access (or anything) does this error get in...where does the exception come from and when? Try getting us some input about it. As far as the image saving functionality is concerned the above code should do the trick.
    Last edited by exterminator; July 21st, 2005 at 05:33 AM.

  13. #13
    Join Date
    Nov 2004
    Posts
    37

    Re: using a savefilediaglog box

    thanks soo much it works woohoo, hopefully i wont need anymore help on this.

    However i do need help on gif animation so anything you can give would be greately appreciated.

    thanks

  14. #14
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured