save and save as in Windows Form App by C# - c#

I'm using this code for making save as of image in c#
SaveFileDialog svf = new SaveFileDialog();
svf.Filter = "JPEG files (*.jpeg)|*.jpeg";
if (DialogResult.OK == svf.ShowDialog())
{
this.imgbox.Image.Save(svf.FileName,ImageFormat.Jpeg);
}
I need to make save now for image without change name or location ( apply save not save as as the above code ) how i can use this code for save now ?

Assuming WinForms, add a field or property to the appropriate control to hold the full path to the file currently opened, and call it currentFileName or something.
When opening a file, assign the path to the variable. Now when the user hits the Save menu, you can use the already stored path to overwrite the opened file. You can add a Save As menu that pops up the SaveFileDialog, and afterwards assigns the result to the variable again - if not cancelled.
When the user invokes the New or Close menu, make sure to clear the stored path.

Related

Saving Button location in XML file in Windows Form

My problem is Saving Button location in XML file. My Form have button that are draggable on edit form, so when i am finished editing the form i want to save Button location in XML file, so then i can read the location from the XML file and display the button on the modified position.
Any Suggestions?
Unless it's otherwise a requirement for your application or you need to store additional complex data, you don't really need to create an XML file. You could use a plain text file like this:
var fileContents = string.Format("{0}\r\n{1}\r\n", button.Top, button.Left);
File.WriteAllText("ButtonCoordinates.dat");
Then to read and apply back the coordinates:
var fileContents = File.ReadAllLines("ButtonCoordinates.dat");
button.Top = double.Parse(fileContents[0]);
button.Left = double.Parse(fileContents[1]);
Of course you need to add error checking, file path management et al, but you get the idea.

How can I change an image/bitmap that is already in use?

I have a directory with .png images which I display in a third party combobox of my c# program. So the user is able to choose one of this images using the combo box. Basic code used:
Bitmap thump = new Bitmap(<path>);
ComboItem item = new ComboItem();
item.Image = thump;
MyComboBox.Items.Add(item);
Now I would like to update one of this images at runtime. Unfortunately I can't delete the old image because it is still opened in my program, so somehow I either need to close it or open it in a way that does not keep the image in use by my program. The changes to the bitmap are not done in my program, I just pass the path of the dirctory to another program which saves the bitmap there (but fails at the moment because it can't delete the old bitmap).
I guess this is a simple problem but I could not find a solution here or on the internet.
First read the file to memory, then create the Bitmap using that data.
var m = new MemoryStream(File.ReadAllBytes(filename));
Bitmap thump = (Bitmap)Bitmap.FromStream(m);

WPF C# Opening files in multiple directories

I have 3 open file buttons which open a file open dialog, whenever one file is opened the starting directory for the next button is always the same as the last button that was used.
I need to be able to have each button open only the last directory that it was associated with, not what the last button opened was associated with.
How can make each dialog open in the directory that that specific dialog was opened in last?
Example, I have 3 buttons i want to open in the following order:
Btn1 Open File in dir C:\temp\1 then
Btn2 Open File in dir C:\temp\1 then change to C:\temp\2
Btn3 Open File in dir C:\temp\2 then change to C:\temp\3
Btn1 Open File in dir C:\temp\1 NOT in C:\temp\3
declare some private fields in your class:
string startLocationForDialog1 = "C:\";
string startLocationForDialog2 = "C:\";
string startLocationForDialog3 = "C:\";
Then in your methods, when you create the open file dialog, set the starting location to the value of the corresponding variable.
After the file is selected, save the location of the file (without the file name) in the corresponding variable. Next time you press the same button, you use that variable which contains the last location from which a file was selected.

How to create a save button that overwrites previously selected file

How would you make a Save button that, unlike a SaveAs button, will save over the previously selected file without opening a dialog? I have no trouble utilizing SaveAs to open a Save Dialog and create a file but automatically saving to a previously set text file has stumped me.
did you try something like that :
System.IO.File.AppendAllText(previously file path, the new value);

how can I upload a file via file input box using webbrowser in C#

May be this question is asked in past but I have searched and not found its solution yet.
I have tried all the options that I have found till now but all in vain.
SendKeys doesn't work as it does not fill the file input box with file path, that is to be uploaded.
Cannot set file input box "SetAttribute" value as there is no value attribute available:
thats all.
If I use element.focus() it pops up "choose file to upload" dialog and now I don't know how to fill it programmatically and open it in file input box.
I want it to be automated completed so that user does not have to interact with the application.
Application shall pick the file from hard disk from given file path and fill other fields of form then start uploading, all using webbrowser control in windows form application.
No solutions found!
Can anyone help please? (This is my first ever question on stackoverflow, therefore if I am doing anything wrong then please guide, I mean if I am not allowed to post such question!)
Here is the code:
HtmlElementCollection heCollection = doc.GetElementsByTagName("input");
foreach (HtmlElement heSpan in heCollection)
{
string strType = heSpan.GetAttribute("type");
string strName = heSpan.GetAttribute("name");
if (strType.Equals("file") && strName.Equals("file"))
{
heSpan.Focus();
//heSpan.SetAttribute("value", "test.jpg");
SendKeys.Send("C:\\1.txt");
//heSpan.InnerText = "c:\\1.txt";
}
//Title for the attachment
if (strName.Equals("field_title"))
{
heSpan.InnerText = "1.txt";
}
}
When this code executes, cursor starts blinking in fine input box (as I have set heSpan.focus()) but the file path doesn't show in the file input box.
If I implement
heSpan.InvokeMember("click");
It opens the choose a file to upload dialoge/popup window and there I get stuck, because I don't know how to fill that popup dynamically and then insert the file path in file input box.
Try setting the focus to the WebBrowser control right before you set the focus to the input field.
That worked for me.

Categories