Open, select and add a .jpg file inside a form - c#

How to open, select and add a only jpeg format file inside a form in c# from any directory of computer? Here is the code.
private void open2ToolStripMenuItem_Click(object sender, EventArgs e)
{
pictureBox1.Visible = true;
string Chosen_File = "";
Chosen_File = openFileDialog1.FileName;
openFileDialog1.Title = "Insert an image";
openFileDialog1.InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.Personal);
openFileDialog1.FileName ="";
openFileDialog1.Filter = "JPEG Images|*.jpg|GIF Images|*.gif|All Files|";
openFileDialog1.ShowDialog();
if (openFileDialog1.ShowDialog() != DialogResult.Cancel)
{
Chosen_File = openFileDialog1.FileName;
pictureBox1.Image = Image.FromFile(Chosen_File);
}
}

Just remove first openFileDialog1.ShowDialog()
private void button1_Click(object sender, EventArgs e)
{
pictureBox1.Visible = true;
string Chosen_File = "";
Chosen_File = openFileDialog1.FileName;
openFileDialog1.Title = "Insert an image";
openFileDialog1.InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.Personal);
openFileDialog1.FileName = "";
openFileDialog1.Filter = "JPEG Images|*.jpg|GIF Images|*.gif|All Files|";
//openFileDialog1.ShowDialog();
if (openFileDialog1.ShowDialog() != DialogResult.Cancel)
{
Chosen_File = openFileDialog1.FileName;
pictureBox1.Image = Image.FromFile(Chosen_File);
}
}
Exept this, your code works well!

You have one openFileDialog1.ShowDialog(); to many. Delete the first one and use just the one inside the if clause! The if clause then uses the result after displaying the Dialog. I also prefer to explicitly checking for DialogResult.OK but that should not make a difference..
Your code shows the Dialog window twice, doesn't it? (It certainly does!) If you pick the image to display on the first dialog you will get the dialog a second time and then have to OK it there, too. If you cancel the second dialog nothing will show.
The Result i.e. the user's choice is only returned directly from the Show(); if you don't grab it there it is lost! This is different from the content of the choice, fields of the Dialog (e.g. the filename), which are all there afterwards..

Related

Multiple filters OpenFileDialog

At the moment I have three buttons on a form, each opens a different form (form2 with a textbox to display the text from the textfile, form3 with a picturebox to display the image)
What I am trying to do is put the two together for my last button so the user can filter which type to open (TXT Files or Image files). I am not sure how I can put the two together and get them to work.
The code I used to just open text files:
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.InitialDirectory = #"C:\";
ofd.Filter = "TXT Files(*.txt;)|*.txt;";
if(ofd.ShowDialog() == DialogResult.OK)
{
using(StreamReader rdText = new StreamReader(ofd.FileName))
{
string info = File.ReadAllText(ofd.FileName);
TextDocumentForm newTextDocument = new TextDocumentForm();
newTextDocument.TextFileName = info;
newTextDocument.Show();
}
}
}
What I use to open my image files
private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog ofdi = new OpenFileDialog();
ofdi.InitialDirectory = #"C:\";
ofdi.Filter = "Image Files(*.jpg;*.jpeg;*.bmp)|*.jpg;*.jpeg;.bmp;";
if (ofdi.ShowDialog() == DialogResult.OK)
{
Image image = Image.FromFile(ofdi.FileName);
ImgDoc newImageDoc = new ImgDocumentForm();
newImageDoc.ImageShow = image;
newImageDoc.Show();
}
}
Any help is appreciated as I am trying to develop my understanding of how OpenFileDialog still works.
Combining filters:
var openFile = new OpenFileDialog
{
InitialDirectory = #"C:\",
Filter = "TXT Files(*.txt;)|*.txt;|Image Files(*.jpg;*.jpeg;*.bmp)|*.jpg;*.jpeg;.bmp;"
};
Then use Path.GetExtension() to see which route you should take:
if (openFile.ShowDialog() == true)
{
var ext = System.IO.Path.GetExtension(openFile.FileName);
if (ext == ".txt")
{
// Open text file
}
else if (ext == ".jpg" || ext == ".jpeg" || ext == ".bmp")
{
// Open image file
}
}

How to choose a folder without opening it with OpenFileDialog C#

I have to create a button with which the user has to choose a folder.
I've try with OpenFileDialog, But there I can not select the folder and the folder just open.
This is my code:
private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog fdlg = new OpenFileDialog();
fdlg.Title = "C# Corner Open File Dialog";
fdlg.InitialDirectory = #"D:\dosfiles\ValPoch";
fdlg.Filter = "All files (*.*)|*.*|All files (*.*)|*.*";
fdlg.FilterIndex = 2;
fdlg.RestoreDirectory = true;
if (fdlg.ShowDialog() == DialogResult.OK)
{
label2.Text = fdlg.FileName;
label2.Show();
}
I try with this code, He works perfectly, But I not like the window, who opened, Is too small.
using (FolderBrowserDialog dlg = new FolderBrowserDialog())
{
dlg.Description = "Select a folder";
dlg.SelectedPath = #"D:\dosfiles\ValPoch\";
if (dlg.ShowDialog() == DialogResult.OK)
{
label2.Text = dlg.SelectedPath;
label2.ForeColor = Color.Red;
label2.Show();
}
}
How can I fix my code with OpenFileDIalog to select a folder not to open that folder ?
Thank you.
Take a look at Ookii.Dialogs, a great library in order to use common Windows dialogs including file dialogs.

How do a make this type of select folder dialog in C#?

So I recently tried to the FolderBrowserDialog but much to my disappointment it was not like the following screenshot:
But instead it was formatted and as I think, hard to navigate with like this:
How would I go about getting the other version where it's a dialog box asking for what folder to save to like the select file type natively, instead of what I think is this hard to navigate menu.
The CommonOpenFileDialog class from the NuGet Package "Microsoft.WindowsAPICodePack-Shell" will answer your request.
Set IsFolderPicker property to true and that's it.
using Microsoft.WindowsAPICodePack.Dialogs;
private bool SelectFolder(out string fileName)
{
CommonOpenFileDialog dialog = new CommonOpenFileDialog();
dialog.IsFolderPicker = true;
if (dialog.ShowDialog() == CommonFileDialogResult.Ok)
{
fileName = dialog.FileName;
return true;
}
else
{
fileName = "";
return false;
}
}
thats because you are using FolderBrowserDialog instead of OpenFileDialog
you can check the below
private void btnBrowse_Click(object sender, EventArgs e)
{
OpenFileDialog fileDialog = new OpenFileDialog();
fileDialog.Title = "Browse File";
fileDialog.Filter = "All files (*.*)|*.*|All files (*.*)|*.*";
fileDialog.FilterIndex = 2;
fileDialog.InitialDirectory = "c:\\";
fileDialog.RestoreDirectory = true;
if (fileDialog.ShowDialog() == DialogResult.OK)
{
txtFileName.Text = fileDialog.FileName;
}
}

How do I create a copy of a picture to a specific path in winform

I have 2 system a c# winform and a php and their database is stored in a single database mysql now my problem is storing pictures...., in my html I save my pictures in htdocs/"foldername"/productimages now what I want in my C# winform was get the location path of the picture from open dialogue and copy that picture to the specific folder which is the htdocs/"foldername"/productimages how do i do that
my code
string picloc;
private void UpdBtn_Click(object sender, EventArgs e)
{
dlg.Filter = "JPG Files(*.jpg)|*.jpg|PNG Files(*.png)|*.png|ALL Files(*.*)|*.*";
dlg.Title = "Select Thumbnail";
if (dlg.ShowDialog() == DialogResult.OK)
{
// Result();
picloc = dlg.FileName.ToString();
pic1.ImageLocation = picloc;
}
}
so How do I copy the file picture from the string picloc to the specific loc?
You can use File.Copy to do this. Something like the following should manage the copy for you:
string picloc;
string new_loc;
private void UpdBtn_Click(object sender, EventArgs e)
{
dlg.Filter = "JPG Files(*.jpg)|*.jpg|PNG Files(*.png)|*.png|ALL Files(*.*)|*.*";
dlg.Title = "Select Thumbnail";
if (dlg.ShowDialog() == DialogResult.OK)
{
// Result();
picloc = dlg.FileName.ToString();
pic1.ImageLocation = picloc;
File.Copy(picloc, new_loc); // new_loc being the new location for the file.
}
}
This assumes you already know the location for the copy of the file. If you want to give the user the choice, do so via a SaveFileDialog to get the new location (as a string) and then perform the copy.

openFileDialog DialogResult always shows NONE when opening .exe file

I am currently coding a "quickstart program" which lets you open an .exe file and start them whenever you click a button.
For this I used an openFileDialog to let the user open the desired .exe files.
Additionally I extract the icons from the .exe to show it, above the start button.
This enables the user to select a file (.exe) the DialogResult of the openFileDialog shows none. It never shows OK, even though I load a normal and working .exe.
Here is my code.
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.ShowDialog();
if (DialogResult == DialogResult.OK)
{
string path = openFileDialog1.FileName;
ExtractIcon(path);
}
}
private void ExtractIcon(string filePath)
{
Icon ico = Icon.ExtractAssociatedIcon(filePath);
pictureBox1.Image = ico.ToBitmap();
}
Is there any problem with my code or is it because of DialogResult?
Try changing the code something like this:
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string path = openFileDialog1.FileName;
ExtractIcon(path);
}
You need to store the return value of ShowDialog() in a variable and then compare this to DialogResult.OK
var result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK){
string path = openFileDialog1.FileName;
ExtractIcon(path);
}
What is DialogResult variable? Shouldn't it be:
var result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
string path = openFileDialog1.FileName;
ExtractIcon(path);
}

Categories