I need help with this code. I want to create basic image convert program but this program is not working? What am I doing wrong. Thanks for answers.
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog file = new OpenFileDialog();
file.ShowDialog();
string DosyaYolu = file.FileName;
string DosyaAdi = file.SafeFileName;
if (file.ShowDialog() == DialogResult.OK)
{
System.Drawing.Image image = System.Drawing.Image.FromFile(DosyaYolu);
image.Save(DosyaYolu, System.Drawing.Imaging.ImageFormat.Png);
}
You choose the wrong target path to save the new image to. Also you invoked the ShowDialog() twice, which is not necessary. The following code will save the new file with the same name but a different extension.
var dialog = new OpenFileDialog();
if (dialog.ShowDialog() == DialogResult.OK)
{
string sourceFile = dialog.FileName;
string targetFile = Path.ChangeExtension(sourceFile, "png");
Image image = Image.FromFile(sourceFile);
image.Save(targetFile, ImageFormat.Png);
}
Related
I'm working on my first WPF application, and I'm trying to copy the browsed image into designated folder, however I have problem with CopyTo function.
private void btnBrowse_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "Select image(*.JPG;*.PNG)|*.JPG;*.PNG";
openFileDialog1.Title = "Select your image";
openFileDialog1.InitialDirectory = #"C:\";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
// The image to be shown into PictureBox
pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
// The Image
var theImg = Image.FromFile(openFileDialog1.FileName);
string fileName = openFileDialog1.FileName;
if(theImg != null)
{
string exePath = System.Environment.GetCommandLineArgs()[0];
string destinationFolder = Path.Combine(exePath, "Profiles");
if (!Directory.Exists(destinationFolder))
{
Directory.CreateDirectory(destinationFolder);
}
string filePath = Path.Combine(destinationFolder, fileName);
using(var fileStream = new FileStream(filePath, FileMode.Create))
{
theImg.CopyTo(theImg, fileStream);
}
}
}
}
Error: No overload for method 'CopyTo' takes 2 arguments
How can I solve this issue? Thank you in advance
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
}
}
I click on the button to upload a photo. When I cancel the photo selection, the program throws an exception.
Here's my code:
private void button1_Click(object sender, EventArgs e)
{
Image<Bgr, byte> img = new Image<Bgr, byte>(Islem.getFileName());
ımageBox1.Image = img;
}
class Islem
{
public static string getFileName()
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Images (*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|" + "All files (*.*)|*.*";
if (ofd.ShowDialog() == DialogResult.OK)
{
return ofd.FileName;
}
return "";
}
}
How to fix this situation?
When the user cancels the dialog, your getFileName function returns an empty string. Therefore, you get an exception when you try to load the image from a path that doesn't exist (i.e., the empty string). If you read the exception message, you would figure it out yourself.
You can do something like this:
private void button1_Click(object sender, EventArgs e)
{
string imgFilePath = Islem.getFileName();
if (!string.IsNullOrEmpty(imgFilePath))
{
Image<Bgr, byte> img = new Image<Bgr, byte>(imgFilePath);
ımageBox1.Image = img;
}
}
That would only use the file path (i.e., load the image) if the returned string isn't empty.
Alternatively, you can check if the file exists:
if (System.IO.File.Exists(imgFilePath)) { //use it to load the image }
I have openfiledialog that reading user image address with file info and load it in textbox
I want to have another button in order to open image address (that already saved in textbox)
how to code this button at wpf ? I know i should use process.start but no idea !
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
tbl_Moshtari tt = new tbl_Moshtari();
dlg.FileName = "pic-file-name"; // Default file name
dlg.DefaultExt = ".jpg"; // Default file extension
dlg.Filter = "JPEG(.jpeg)|*.jpeg | PNG(.png)|*.png | JPG Files (*.jpg)|*.jpg|GIF Files (*.gif)|*.gif"; // Filter files by extension
Nullable<bool> result = dlg.ShowDialog();
if (result == true)
{
//// picbox.Source = new BitmapImage(new Uri(dlg.FileName, UriKind.Absolute));
//bitmapImage = new BitmapImage();
//bitmapImage.BeginInit();
//bitmapImage.StreamSource = System.IO.File.OpenRead(dlg.FileName);
//bitmapImage.EndInit();
////now, the Position of the StreamSource is not in the begin of the stream.
//picbox.Source = bitmapImage;
FileInfo fi = new FileInfo(dlg.FileName);
string filename = dlg.FileName;
txt_picaddress.Text = filename;
System.Windows.MessageBox.Show("Successfully done");
}
This second button i have
private void btn_go_Click(object sender, RoutedEventArgs e)
{
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
//FileInfo fi = new FileInfo(dlg.FileName);
string filename = dlg.FileName;
Process.Start(filename);
}
This isnt working for me .
Process.Start() should open up the image as long as filename is an absolute path to the file. With that being said, where in your btn_go_Click method are you actually opening up the dialog to get the file name? dlg.FileName returns an empty string if you don't show the dialog in which case Process.Start() fails.
If the file name needs to come from the previous dialog, you shouldn't create a new dialog; instead, change
Process.Start(filename)
to
Process.Start(txt_picaddress.Text)
Of course, you need to do some input verification to make sure the path is correct (unless the textbox is read-only).
Also, consider setting a breakpoint on string filename = dlg.FileName; to make sure it has the correct path to the file if it's still not working.
To open and highlight the file in Windows Explorer:
string filename = txt_picaddress.Text;
ProcessStartInfo pInfo =
new ProcessStartInfo("explorer.exe", string.Format("/Select, {0}", filename));
Process.Start(pInfo);
In your second code sample, you created a new instance of openFileDialog, you need instead to use the previous instance of the openFileDialog that is holding the correct image filename:
if you create the first openFileDialog in the window constructor you can do this:
private void btn_go_Click(object sender, RoutedEventArgs e)
{
string filename = this.dlg.FileName;
Process.Start(filename);
}
hope this helps, this is what i can say given the code you provided.
You don't need an OpenFileDialog in btn_go_Click if you want to use the path in your textbox:
private void btn_go_Click(object sender, RoutedEventArgs e)
{
string filename = txt_picaddress.Text;
Process.Start(filename);
}
I am getting a error when i decrypt my file.
"The path is not of a legal form"
if i direct the privateKeyLocation manually e.g. string privateKeyLocation = #"c:\privatekey.txt", its okay and runs fine.
But i want to find the key file myself using open file dialog.
Anybody know what went wrong?
Thanks in advance!
private void decryptFileButton_Click(object sender, EventArgs e)
{
string inputFileLocation = attachmentTextBox.Text;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "c:\\";
openFileDialog1.RestoreDirectory = true;
string privateKeyLocation = new FileInfo(openFileDialog1.FileName).ToString();
string privateKeyPassword = passphrase2TextBox.Text;
string outputFile = #"c:\Original.txt";
// decrypt and obtain the original file name
// of the decrypted file
string originalFileName =
pgp.DecryptFile(inputFileLocation,
privateKeyLocation,
privateKeyPassword,
outputFile);
}
Just a simple matter of showdialog() and its result:
private void decryptFileButton_Click(object sender, EventArgs e)
{
string inputFileLocation = attachmentTextBox.Text;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "c:\\";
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string privateKeyLocation = new FileInfo(openFileDialog1.FileName).ToString();
string privateKeyPassword = passphrase2TextBox.Text;
string outputFile = #"c:\Original.txt";
decrypt and obtain the original file name
of the decrypted file
string originalFileName =
pgp.DecryptFile(inputFileLocation,
privateKeyLocation,
privateKeyPassword,
outputFile);
}
}