I want to save opened picture to a predefined location with a defined naming convention such as date+ original name. How can a eliminate to be asked the file name and folder by savedialog;
Image Dosya;
private void btnopen_Click(object sender, EventArgs e)
{
OpenFileDialog of = new OpenFileDialog();
of.Filter = "Jpg|*.Jpg";
if (of.ShowDialog()==DialogResult.OK)
{
Dosya = Image.FromFile(of.FileName);
pictureBox1.Image = Dosya;
}
}
private void btnSave_Click(object sender, EventArgs e)
{
SaveFileDialog sd = new SaveFileDialog();
sd.InitialDirectory = "C:\\Users\\sonyy\\Videos\\";
sd.Title = "Save Files";
sd.CheckFileExists = true;
sd.CheckPathExists = true;
sd.DefaultExt = "jpg";
sd.Filter = "JPG(*.jpg)|*.jpg|All files (*.*)|*.*";
sd.FilterIndex = 1;
sd.RestoreDirectory = false;
if (sd.ShowDialog() == DialogResult.OK)
{
string dosyaadi = sd.InitialDirectory;
string date = Convert.ToString(DateTime.Today.ToShortDateString());
sd.FileName = date;
Dosya.Save(sd.InitialDirectory+date+"."+sd.DefaultExt);
}
}
You don't need to open the file save dialog.
You know what's the path, you know what's the name (date time for example).
Simply save the file without showing the dialog.
Use the Image.Save assuming you have the image.
Image Dosya;
private void btnopen_Click(object sender, EventArgs e)
{
OpenFileDialog of = new OpenFileDialog();
of.Filter = "Jpg|*.Jpg";
if (of.ShowDialog()==DialogResult.OK)
{
Dosya = Image.FromFile(of.FileName);
pictureBox1.Image = Dosya;
}
}
private void btnSave_Click(object sender, EventArgs e)
{
SaveFileDialog sd = new SaveFileDialog();
sd.InitialDirectory = "C:\\Users\\sonyy\\Videos\\";
sd.Title = "Save Files";
sd.CheckFileExists = true;
sd.CheckPathExists = true;
sd.DefaultExt = "jpg";
sd.Filter = "JPG(*.jpg)|*.jpg|All files (*.*)|*.*";
sd.FilterIndex = 1;
sd.RestoreDirectory = false;
string dosyaadi = sd.InitialDirectory;
string date = Convert.ToString(DateTime.Today.ToShortDateString());
sd.FileName = date;
Dosya.Save(sd.InitialDirectory+date+"."+sd.DefaultExt);
SaveFileDialog sd = new SaveFileDialog();
sd.InitialDirectory = "C:\\Users\\sonyy\\Videos\\";
sd.Title = "Save Files";
//sd.CheckFileExists = true;
sd.CheckPathExists = true;
sd.DefaultExt = "jpg";
sd.Filter = "JPG(*.jpg)|*.jpg|All files (*.*)|*.*";
sd.FilterIndex = 1;
sd.RestoreDirectory = false;
if (sd.ShowDialog() == DialogResult.OK)
{
string dosyaadi = sd.InitialDirectory;
string date = Convert.ToString(DateTime.Today.ToShortDateString());
sd.FileName = date;
Dosya.Save(date + "." + sd.DefaultExt);
}
Related
I am trying to build a small .pdf -> .txt / searchable .pdf converter, but I am having trouble to assign the first var result to the other buttons
Made myself a "solution" but the code seems too messed and exagerated.
using IronOcr;
using System;
using System.IO;
namespace ocr
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "pdf files (*.pdf)|*.pdf|All Files (*.*)|*.*";
if (ofd.ShowDialog() == DialogResult.OK)
{
textBox1.Text = ofd.FileName;
var Ocr = new IronTesseract(); // nothing to configure
IronOcr.License.LicenseKey = "SOMELICENSEKEY";
using (var Input = new OcrInput())
{
Input.AddPdf(ofd.FileName, "password");
var Result = Ocr.Read(Input);
Console.WriteLine(Result.Text);
richTextBox1.Text = Result.Text;
}
}
}
private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "pdf files (*.pdf)|*.pdf|All Files (*.*)|*.*";
if (ofd.ShowDialog() == DialogResult.OK)
{
textBox1.Text = ofd.FileName;
var Ocr = new IronTesseract(); // nothing to configure
IronOcr.License.LicenseKey = "SOMELICENSEKEY";
using (var Input = new OcrInput())
{
Input.AddPdf(ofd.FileName, "password");
var Result = Ocr.Read(Input);
Console.WriteLine(Result.Text);
Result.SaveAsTextFile("pdf.txt");
}
}
}
private void button3_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "pdf files (*.pdf)|*.pdf|All Files (*.*)|*.*";
if (ofd.ShowDialog() == DialogResult.OK)
{
textBox1.Text = ofd.FileName;
var Ocr = new IronTesseract(); // nothing to configure
IronOcr.License.LicenseKey = "SOMELICENSEKEY";
using (var Input = new OcrInput())
{
Input.AddPdf(ofd.FileName, "password");
var Result = Ocr.Read(Input);
Console.WriteLine(Result.Text);
Result.SaveAsSearchablePdf("pdfpesquisavel.pdf");
}
}
}
}
}
Tried to assign and use the variable "Result" in the others buttons fuctions (button 2 and button 3)
But it didn't worked.
I tried to create a little program to convert PDF to a TIF file using ghostscript but unfortunately it results in an error ("null"). Can't figure out why it's failing:
void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "PDF Files|*.pdf";
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
strfilename = openFileDialog1.FileName;
}
}
void button2_Click(object sender, EventArgs e)
{
FolderBrowserDialog targetfolder = new FolderBrowserDialog();
if (targetfolder.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
folder = targetfolder.SelectedPath;
}
}
void button3_Click(object sender, EventArgs e)
{
const string DLL_64BITS = "gsdll64.dll";
string NomeGhostscriptDLL;
NomeGhostscriptDLL = DLL_64BITS;
GhostscriptVersionInfo gvi = new GhostscriptVersionInfo(NomeGhostscriptDLL);
///var xDpi = 300;
var yDpi = 300;
using (var rasterizer = new GhostscriptRasterizer())
{
byte[] buffer = File.ReadAllBytes(strfilename);
MemoryStream ms = new MemoryStream(buffer);
rasterizer.Open(ms, gvi, true);
int PdfPages = rasterizer.PageCount;
for (int pageNumber = 1; pageNumber < rasterizer.PageCount; pageNumber++)
{
string outputTIFPath = Path.Combine(folder, "00" + pageNumber.ToString() + ".tiff");
Image pdf2TIF = rasterizer.GetPage(yDpi, pageNumber);
MessageBox.Show(outputTIFPath);
pdf2TIF.Save(outputTIFPath, ImageFormat.Tiff);
}
rasterizer.Close();
}
}
The error looks like this
Can anyone help me to sort this out?
try adding this
MyPlaceHolder.Controls.Add(pd2TIF);
below:
Image pdf2TIF = rasterizer.GetPage(yDpi, pageNumber);
I just read that on a different thread. im not 100% sure if it works
private void browsebtn_Click(object sender, EventArgs e)
{
OpenFileDialog fdlg = new OpenFileDialog();
fdlg.Title = "Select Song";
fdlg.InitialDirectory = #"c:\";
fdlg.Filter = "All files (*.*)|*.*|All files (*.*)|*.*";
fdlg.FilterIndex = 2;
fdlg.RestoreDirectory = true;
if (fdlg.ShowDialog() == DialogResult.OK)
{
filedialoglbl.Text = fdlg.FileName;
}
}
private void runbtn_Click(object sender, EventArgs e)
{
var Path = fdlg.FileName;
var pi = new System.Diagnostics.ProcessStartInfo(Path)
{
Arguments = Path.GetFileName(Path),
UseShellExecute = true,
WorkingDirectory = Path.GetDirectoryName(Path),
FileName = "C:\\relax.exe",
Verb = "OPEN"
};
System.Diagnostics.Process.Start(pi);
}
}
As you can see I am trying to select a file and use that file to run another program but I don't want to use windows default.
My error is in
var Path = fdlg.FileName;
The problem is fdlg isnt in the same context and i need to use the directory chosen from before, any ideas on how to do this?
From the answers I have tried this:
private OpenFileDialog fdlg = new OpenFileDialog();
private void browsebtn_Click(object sender, EventArgs e)
{
fdlg.Title = "Select Song";
fdlg.InitialDirectory = #"c:\";
fdlg.Filter = "All files (*.*)|*.*|All files (*.*)|*.*";
fdlg.FilterIndex = 2;
fdlg.RestoreDirectory = true;
if (fdlg.ShowDialog() == DialogResult.OK)
{
filedialoglbl.Text = fdlg.FileName;
}
}
private void runbtn_Click(object sender, EventArgs e)
{
var Path = fdlg.Text;
var pi = new System.Diagnostics.ProcessStartInfo(Path)
{
Arguments = Path.GetFileName(Path),
UseShellExecute = true,
WorkingDirectory = Path.GetDirectoryName(Path),
FileName = "C:\\relax.exe",
Verb = "OPEN"
};
System.Diagnostics.Process.Start(pi);
}
and I get an error saying 'OpenFileDialog' doesnt have a definition or extension method for 'Text' and with the line in the class:
private OpenFileDialog fdlg = new OpenFileDialog();
I get a new error saying that 'GetFileName' and 'GetDirectoryName' don't have definitions or extension methods in the 'string'
EDIT
I renamed path to directory but same error
private void runbtn_Click(object sender, EventArgs e)
{
var directory =filedialoglbl.Text;
var pi = new System.Diagnostics.ProcessStartInfo(directory)
{
Arguments = directory.GetFileName(directory),
UseShellExecute = true,
WorkingDirectory = directory.GetDirectoryName(directory),
FileName = "C:\\relax.exe",
Verb = "OPEN"
};
System.Diagnostics.Process.Start(pi);
}
You are using the dialog in your runbtn_Click method only for the FileName. You saved it into a label called filedialoglbl.Text. Use it.
private void browsebtn_Click(object sender, EventArgs e)
{
OpenFileDialog fdlg = new OpenFileDialog();
fdlg.Title = "Select Song";
fdlg.InitialDirectory = #"c:\";
fdlg.Filter = "All files (*.*)|*.*|All files (*.*)|*.*";
fdlg.FilterIndex = 2;
fdlg.RestoreDirectory = true;
if (fdlg.ShowDialog() == DialogResult.OK)
{
filedialoglbl.Text = fdlg.FileName;
}
}
private void runbtn_Click(object sender, EventArgs e)
{
var path = filedialoglbl.Text;
var pi = new System.Diagnostics.ProcessStartInfo(path)
{
Arguments = Path.GetFileName(path),
UseShellExecute = true,
WorkingDirectory = Path.GetDirectoryName(path),
FileName = "C:\\relax.exe",
Verb = "OPEN"
};
System.Diagnostics.Process.Start(pi);
}
Edit
You can't use Path with uppercase because your are shawoding the Path class and you can't use their methods.
Declare OpenFileDialog fdlg outside of your methods as a class member (field), e.g.
private OpenFileDialog fdlg = new OpenFileDialog();
I want to add filenames (without the full path) to the ListBox.
The code below is working smoothly, but when when I change FileNames to SafeFileNames (for hiding item location) it's not working anymore.
XAML
<MediaElement x:Name="mePlayer" Margin="64,0,90,61"/>
<ListBox x:Name="listbox4" Background="Salmon" BorderBrush="Black" BorderThickness="3"/>
CS
private void load_Click(object sender, RoutedEventArgs e)
{
Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog();
ofd.DefaultExt = ".mp3";
ofd.Filter = "All|*.*";
ofd.Multiselect = true;
Nullable<bool> result = ofd.ShowDialog();
if (result == true)
{
for (int i = 0; i < ofd.FileNames.Length; i++)
{
listbox4.Items.Add(ofd.FileNames[i].ToString());
listbox4.SelectedItem = ofd.FileName;
mePlayer.Source = new Uri(
listbox4.SelectedItem.ToString(),
UriKind.RelativeOrAbsolute);
mePlayer.LoadedBehavior = MediaState.Play;
}
}
}
This code should work for you. Please read the comments in the code before you proceed.
private Dictionary<string, string> fileDictionary = new Dictionary<string, string>();
private void load_Click(object sender, RoutedEventArgs e)
{
Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog();
ofd.DefaultExt = ".mp3";
ofd.Filter = "All|*.*";
ofd.Multiselect = true;
Nullable<bool> result = ofd.ShowDialog();
if (result == true)
{
for (int i = 0; i < ofd.FileNames.Length; i++)
{
var filePath = ofd.FileNames[i];
var fileName = System.IO.Path.GetFileName(filePath);
fileDictionary.Add(fileName, filePath);
// not sure about this logic. You may need to reconsider what you are trying to do here.
//Instead of doing this, create a click event for the list box and get the selected file path to be played from the dictionary.
listbox4.Items.Add(fileName);
}
}
}
private void listbox4_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (listbox4.SelectedItem != null)
{
var selectedFile = listbox4.SelectedItem.ToString();
string selectedFilePath;
fileDictionary.TryGetValue(selectedFile, out selectedFilePath);
if (!string.IsNullOrEmpty(selectedFilePath))
{
mePlayer.Source = new Uri(selectedFilePath, UriKind.RelativeOrAbsolute);
mePlayer.LoadedBehavior = MediaState.Play;
}
}
}
In my app, the user can browse and select a text file. I'm saving the path like this:
private void nacitanie_Click(object sender, EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.Title = "Otvoriť Textový súbor.";
dialog.Filter = "TXT files|*.txt";
dialog.InitialDirectory = #"C:\";
if (dialog.ShowDialog() == DialogResult.OK)
{
string path = dialog.FileName;
}
}
Then I need to work with that path in other buttons. How can I return the path of the text file from the button handler method?
You should create path outside nacitanie_Click:
class SomeClass
{
private string path;
.....
private void nacitanie_Click(object sender, EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.Title = "Otvoriť Textový súbor.";
dialog.Filter = "TXT files|*.txt";
dialog.InitialDirectory = #"C:\";
if (dialog.ShowDialog() == DialogResult.OK)
{
path = dialog.FileName;
}
}
....
}
and the use it in another methods/handlers.
You need to declare a variable outside of the button handler method:
private string path = string.Empty;
private void nacitanie_Click(object sender, EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.Title = "Otvoriť Textový súbor.";
dialog.Filter = "TXT files|*.txt";
dialog.InitialDirectory = #"C:\";
if (dialog.ShowDialog() == DialogResult.OK)
{
path = dialog.FileName;
}
}
That way, you'll be able to access the variable from other button handlers.