Click to See Complete Forum and Search --> : using a savefilediaglog box


djing1985
July 19th, 2005, 02:38 PM
Hi guys,

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

MadHatter
July 19th, 2005, 04:29 PM
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.

djing1985
July 20th, 2005, 03:08 AM
i want to save jpgs , but what is the actual code to do this i am very new to it all.

Thanks

exterminator
July 20th, 2005, 04:08 AM
This is the link (http://www.freevbcode.com/ShowCode.asp?ID=5583) 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 :
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 (http://www.google.com). Hope this helps and dont forget to rate good/helpful posts. :thumb:

P.S. - this post should have had been on the ASP.NET forum and not the C# programming one.

djing1985
July 20th, 2005, 04:18 AM
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?

exterminator
July 20th, 2005, 04:48 AM
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.1. The save file dialog (http://www.functionx.com/vcsharp/controls/savefiledialog.htm)
2. SaveFileDialog component (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/vbtchEssentialCodeForWindowsFormsDialogBoxes.asp)
3. Using the OpenFileDialog and SaveFileDialog Controls (http://www.informit.com/articles/article.asp?p=101720&seqNum=22) 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.

djing1985
July 20th, 2005, 10:51 AM
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

MadHatter
July 20th, 2005, 10:56 AM
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);
}
}
}

djing1985
July 20th, 2005, 01:31 PM
as ive tried to explain i am really new to this os some comments with this code might help me.

Thanks

MadHatter
July 20th, 2005, 02:09 PM
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.

djing1985
July 21st, 2005, 04:10 AM
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.

exterminator
July 21st, 2005, 05:30 AM
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.
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- 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.

djing1985
July 21st, 2005, 10:22 AM
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

exterminator
July 21st, 2005, 01:42 PM
thanks soo much it works woohoo, hopefully i wont need anymore help on this.You are welcome. :cool: :D
:thumb: