I want to add images from folder and list it in dropdown . Like my application has folder name flags containing all the flags images and their country name. how do I add them to dropdown .
Try using the System.IO.Directory.GetFiles and System.IO.Path.GetFileName
http://msdn.microsoft.com/en-us/library/system.io.directory.getfiles(v=vs.110).aspx
http://msdn.microsoft.com/en-us/library/system.io.path.getfilename(v=vs.110).aspx
Something like (haven't tried it)
// Process the list of files found in the directory.
string [] files = Directory.GetFiles(yourDirectory);
foreach(string file in files) {
string language = Path.GetFileName(file);
ddlFlags.Items.Add(new ListItem(language, file));
}
Next time, improve your question by describing what you have tried so far, then it would be easier to help you.
You should include the System.IO namespace and add an ImageList to your form. Set its ImageSize to a nice size for you images.
Then use the code below to do the rest! It loads all files in a folder into both an ImageList and into the Items of a ComboBox. Note that it loads not the filenames but FileInfo objects, so that it can easily display the names without the path. Also note that to display images in a CombBox it has to be owner-drawn, which, as you can see it pretty straight-forward..
Here is the code to use & study:
using System.IO;
//..
// load whereever you like
// e.g. in the From.Load event or after InitializeComponent();
var images = Directory.GetFiles(yourImageFolder, "*.jpg");
foreach (string file in images)
{
imageList1.Images.Add(file, new Bitmap(file));
comboBox1.Items.Add(new FileInfo(file));
}
comboBox1.DrawMode = DrawMode.OwnerDrawFixed;
comboBox1.DrawItem += comboBox1_DrawItem;
comboBox1.ItemHeight = imageList1.ImageSize.Height;
void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
{
FileInfo FI = (FileInfo)comboBox1.Items[e.Index];
e.Graphics.DrawImage(imageList1.Images[FI.FullName], e.Bounds.Location);
e.Graphics.DrawString(FI.Name, Font, Brushes.Black,
e.Bounds.Left + imageList1.ImageSize.Height + 3, e.Bounds.Top + 4);
}
Related
I am currently using a file dialog to export a file, but I was wondering how I could export my file using drag and drop. I couldn't figure out how to get the file path of where the item is being dropped. Here is the code that I used for open file dialogue in case its required.
if (this.listView1.SelectedItems.Count > 0)
{
ListViewItem item = this.listView1.SelectedItems[0];
string text = this.faderLabel8.Text;
if (!text.EndsWith(#"\"))
{
text = text + #"\";
}
using (SaveFileDialog dialog = new SaveFileDialog())
{
if (dialog.ShowDialog() == DialogResult.OK)
{
Jtag.ReceiveFile(item.SubItems[0].Text, text + item.SubItems[0].Text);
}
}
}
You don't need the path of where the file is being dropped. Instead you need to create a temporary file.
Save file to a temporary folder
Initiate drag on an event/command, such as mouse down, in the following way:
//(This example is uses WPF/System.Windows.DragDrop)
//Create temporary file
string fileName = "DragDropSample.txt";
var tempPath = System.IO.Path.GetTempPath();
var tempFilePath = System.IO.Path.Combine(tempPath, fileName);
System.IO.File.WriteAllText(tempFilePath, "Testing drag and drop");
//Create DataObject to drag
DataObject dragData = new DataObject();
dragData.SetData(DataFormats.FileDrop, new string[] { tempFilePath });
//Initiate drag/drop
DragDrop.DoDragDrop(dragSourceElement, dragData, DragDropEffects.Move);
For WinForms example and more details see:
Implement file dragging to the desktop from a .net winforms application?
If you want it to be useful through "drag and drop" you would require some sort of graphical interface that displays the files in a container where they are and then another container to where you want to move them. When you highlight an item with your mouse you add them to your itemList and when your drop them you copy them. Just make sure the List is emptied once in case you remove the highlight.
I initially have a Fileupload tool to upload a textfile, manipulate its content and display into a Listbox or Textbox. The limitation however is Fileupload only supports single uploading, at least to the version of .Net Framework I am using.
What I intend to do is just use a button control and remove the Fileupload. Upon Button click I need to read the textfiles inside a designated folder path and display first the contents inside a multiple lined textbox. (not just the file name) This is my intially written codes, and it is not working.
protected void btnGetFiles_Click(object sender, EventArgs e)
{
string content = string.Empty;
DirectoryInfo dinfo = new DirectoryInfo(#"C:\samplePath");
FileInfo[] Files = dinfo.GetFiles("*.txt");
foreach (FileInfo file in Files)
{
//ListBox1.Items.Add(file.Name);
content += content;
}
txtContent.Text = content;
}
Since your's is web based application you can't access physical paths like c:\\.. you should use Server.MapPath anyway(As per the comment, you don't need to get the file with Server.MapPath). Then for getting the content you can try something like the following:
protected void btnGetFiles_Click(object sender, EventArgs e)
{
try
{
StringBuilder content = new StringBuilder();
if (Directory.Exists(#"C:\samplePath"))
{
// Execute this if the directory exists
foreach (string file in Directory.GetFiles(#"C:\samplePath","*.txt"))
{
// Iterates through the files of type txt in the directories
content.Append(File.ReadAllText(file)); // gives you the conent
}
txtContent.Text = content.ToString();
}
}
catch
{
txtContent.Text = "Something went wrong";
}
}
you wrote content += content;, that is the problem. change it to content += file.Name;, it will work.
Take a look on the code ,i hope you will understand what I'm trying to do:
private void btnOpen_Click(object sender, EventArgs e)
{
string[] Folders = Directory.GetDirectories(txtFolder.Text);
string foldername;
int count=0;
foreach (string f in Folders)
{
foldername = Path.GetDirectoryName(f);
Label newlabe = new Label();
newlabe.Location = new Point(12, 58);
newlabe.Text = foldername;
count++;
}
}
as you can see i insert a Directory Path into text box , then I opened an array which contains the sub Directories,the next step is to open labels which contains the sub directories names from the directory i insert to the text box, that's not working , what should i do ?
Use some kind of container and stack/add the labels into it. You don't need to assign a location to the label, as the container (depending on the container layout algorithm) will layout them for you.
I dont know whether you use WinForms or WPF or something else, so I will not write any sample code.
But here is some pseudocode:
create a container and add it to the form
for each folder
create a label for the folder
add the label to the container
by the way, have you tried a TreeView control?
I'am pretty new to C# and I want a listbox with all text files in a folder and if the user dubbleclicks on the listed file the file will display in a textbox.
I don't want to use the openFileDialog function because the textfiles are on a webserver which I access using username:password#server.com/folder.
Kinda like a texteditor limited to edit only files in 1 folder :)
If this is possible using openFileDialog please tell me how.
I hope you can understand what I want to do.
Greetings,
From what I understand you are after, you want to iterate through the files in a specific directory and then allow them to be edited once opened with a double click in a listbox.
This can be done using the var Files = Directory.GetFiles("path", ".txt");
Files would be a string[] of the file names.
Then filling the Listbox with the files something like this:
ListBox lbx = new ListBox();
lbx.Size = new System.Drawing.Size(X,Y); //Set to desired Size.
lbx.Location = new System.Drawing.Point(X,Y); //Set to desired Location.
this.Controls.Add(listBox1); //Add to the window control list.
lbx.DoubleClick += OpenFileandBeginEditingDelegate;
lbx.BeginUpdate();
for(int i = 0; i < numfiles; i++)
lbx.Items.Add(Files[i]);
lbx.EndUpdate();
Now your event delegate should look something like this:
OpenFileandBeginEditingDelegate(object sender, EventArgs e)
{
string file = lbx.SelectedItem.ToString();
FileStream fs = new FileStream(Path + file, FileMode.Open);
//Now add this to the textbox
byte[] b = new byte[1024];
UTF8Encoding temp = new UTF8Encoding(true);
while (fs.Read(b,0,b.Length) > 0)
{
tbx.Text += temp.GetString(b);//tbx being the textbox you want to use as the editor.
}
}
Now to add an event handler through the VS window editor click on the control in question and go to the properties pane for that control. You will need to then switch to the events pane and scroll untill you find the DoubleClick event if you use that the designer should auto-insert a valid delegate signature and allow you to write the logic for the event.
I want to display all the images from given directory. For that i have given image controls to display images, but i want to display images control with that url automatically as per images in that directory. Suppose in my directory 5 images are present then 5 image controls should be display in my button click event.
I have written code in button_click event that displays only two images from the given directory is as follows :
protected void btncompare_Click(object sender, EventArgs e)
{
Bitmap searchImage;
searchImage = new Bitmap(#"D:\kc\ImageCompare\Images\img579.jpg");
string dir = "D:\\kc\\ImageCompare\\Images";
DirectoryInfo dir1 = new DirectoryInfo(dir);
FileInfo[] files = null;
files = dir1.GetFiles("*.jpg");
double sim;
foreach (FileInfo f in files)
{
sim = Math.Round(GetDifferentPercentageSneller(searchImage, new Bitmap(f.FullName)), 3);
if (sim >= 0.95)
{
string imgPath = "Images/" + files[0];
string imgPath1 = "Images/" + files[1];
Image1.ImageUrl = "~/" + imgPath;
Image2.ImageUrl = "~/" + imgPath1;
Response.Write("Perfect match with Percentage" + " " + sim + " " + f);
Response.Write("</br>");
}
else
{
Response.Write("Not matched" + sim);
Response.Write("</br>");
}
}
}
Seems like you're trying to fit a square peg into a round hole here. The Image control is designed to show a single image.
If you want to display multiple images, use multiple image controls. I suspect that the reason you're not doing this is because you don't know how many images that you're going to need to display.
If I had the same problem, I would turn the contents of the image directory into something that could be bound onto a Repeater. Each ItemTemplate would contain its own Image control, allowing you to display as many images as you need without resorting to hackery.