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;
}
}
}
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
I've been developing a MP3 player in WPF CSharp and when I add a mp3 file, it adds normally, but when I add another mp3 file separately the name of the newly added mp3 file is the same as the first added one here's my code:
private List<String> Files = new List<string> { };
private List<String> Paths = new List<string> { };
private void AddSong_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Multiselect = true;
if (ofd.ShowDialog() == true)
{
mediaPlayer.Stop();
for (int i = 0; i < ofd.FileNames.Length; i++)
{
Files.Add(System.IO.Path.GetFileNameWithoutExtension(ofd.FileNames[i]));
Paths.Add(ofd.FileNames[i]);
Playlist.Items.Add(Files[i]);
}
}
}
You're adding in Files[i], but i is not the right index to be using there. Instead, add in the most recent file, System.IO.Path.GetFileNameWithoutExtension(ofd.FileNames[i]).
(This is a problem when the Files is not empty when you add extra files, as you observed.)
private List<String> Files = new List<string> { };
private List<String> Paths = new List<string> { };
private void AddSong_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Multiselect = true;
if (ofd.ShowDialog() == true)
{
int InitialFilesLength = Files.Length;
mediaPlayer.Stop();
for (int i = 0; i < ofd.FileNames.Length; i++)
{
Files.Add(System.IO.Path.GetFileNameWithoutExtension(ofd.FileNames[i]));
Paths.Add(ofd.FileNames[i]);
Playlist.Items.Add(Files[i - InitialFilesLength]);
}
}
}
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);
}
I have this code here which i use in order to upload some stuff in a windows form:
public Form1()
{
InitializeComponent();
}
private void btnLoad_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
System.Windows.Forms.DialogResult dr = ofd.ShowDialog();
if (dr == DialogResult.OK)
{
userSelectedFilePath = ofd.FileName;
}
}
public string userSelectedFilePath
{
get
{ return tbFilePath.Text;
}
set
{tbFilePath.Text = value;
}
}
private void btn_compare_Click(object sender, EventArgs e)
{
string Xml1 = tbFilePath.Text;
string Xml2 = System.IO.File.ReadAllText(#"C:");
compare.comparison(Xml1, Xml2);
}
Apparently i'm doing something wrong because i'm not passing the tbFilePath.Text which i need when i have: string Xml1 = tbFilePath.Text;
What is it?
What you probably want is to compare the contents of 2 files.
As siride said your code does not make sense(see his comment)
Add this method to your class
private string FindFile()
{
OpenFileDialog ofd = new OpenFileDialog();
string _xmlPath1 = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
System.Windows.Forms.DialogResult dr = ofd.ShowDialog();
if (dr == DialogResult.OK)
return ofd.FileName;
else
return null;
}
And then you can do this:
private void btn_compare_Click(object sender, EventArgs e)
{
string x1 = System.IO.File.ReadAllText(FindFile(), Encoding.UTF8);
string x2 = System.IO.File.ReadAllText(FindFile(), Encoding.UTF8);
//Or if you already have the second file
//string x2 = System.IO.File.ReadAllText(#"C:\YourPath\someFileName.xml", Encoding.UTF8);
compare.comparison(x1, x2);
}