I have this problem. I want to add image to listView. Exactly I want use openFileDialog for choose image on disc, load file to aplication and show them in listView.
Now I do it like this:
openFileDialog1.Filter = "png (*.png)|*.png";
openFileDialog1.Multiselect = true;
if ( openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string[] files = openFileDialog1.FileNames;
foreach ( var pngFile in files ) {
try {
Bitmap image = new Bitmap( pngFile );
imageList1.Images.Add( image );
} catch {
}
}
listView1.LargeImageList = imageList1;
listView1.Refresh();
}
But it doesn't work. What do I make wrong?
edit
I get blank listView. Nothing error.
Well, that's fine. But you only added an image to the image list. You haven't modified an item in the list view that actually uses that added image. Add this line of code and tweak as necessary:
listView1.Items.Add(new ListViewItem("Added an image", imageList1.Images.Count - 1));
Also ensure that listView1.LargeImages = imageList1. You set that in the designer.
Related
In my windows application I am using the ImageListView.dll displaying selected images.
I used the following code for Adding the selected images to ImageListView
string folder = Photo_Care.Properties.Settings.Default.LastFolder;
if (Directory.Exists(folder))
openFileDialog1.InitialDirectory = folder;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
folder = Path.GetDirectoryName(openFileDialog1.FileName);
Properties.Settings.Default.LastFolder = folder;
Properties.Settings.Default.Save();
imageListView1.Items.AddRange(openFileDialog1.FileNames);
}
And I can able to remove the selected Image from Image List using the below code.
imageListView1.SuspendLayout();
// Remove selected items
foreach (var item in imageListView1.SelectedItems)
imageListView1.Items.Remove(item);
// Resume layout logic.
imageListView1.ResumeLayout(true);
But Now I want to add the close button right top of the added image instantly for removing the image. Like Below Image
I'm trying to change the image of a button in different situations.
The only thing it will do is adding the new (smaller) image ontop of the old (wider) image. How can i make sure the old image will be cleared. I can't find button.image.clear or something.
if (Global.van_zoek)
{
btnterug.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.ZoekBedrijf2));
}
else if (!diftext)
{
btnterug.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.AlleBedrijven2));
}
else if (_Prparent != null)
{
btnterug.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.InfoContact2));
}
else
{
btnterug.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.InfoProject2));
}
http://img225.imageshack.us/i/imageprobl.jpg/
//Change old image to null
btnterug.BackgroundImage = null;
//Load New Image
btnterug.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.InfoProject2));
Try to use invalidate method:
button2.Invalidate();
You Could just set the source to null before you change it to the new image.
Are you getting Button.Image and Button.BackgroundImage confused?
I have a Listview and an "ADD" button,when I click on ADD i should be able to browse the files in computer, select files and when click OK or the Open, the file list should be added in the listview...how to do that...is listview correct or any other alternative...?
ListView should be fine for file listing. Just be aware that longer file paths are difficult to see (have to horizontally scroll which is bad!) if you are gonna just add the full path to list. You can toy with the idea of other representation like:
File.Txt (C:\Users\Me\Documents)
C:\Users\..\File.Txt
etc
As far as doing it using code is concerned, you will need to use OpenFileDialog control to let user choose the files.
var ofd = new OpenFileDialog ();
//add extension filter etc
ofd.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ;
if(ofd.ShowDialog() == DialogResult.OK)
{
foreach (var f in openFileDialog1.FileNames)
{
//Transform the list to a better presentation if needed
//Below code just adds the full path to list
listView1.Items.Add (f);
//Or use below code to just add file names
//listView1.Items.Add (Path.GetFileName (f));
}
}
If you want to do this in the designer, you can take the following steps to add the images to the ListView control:
Switch to the designer, click on the ImageList component on the Component Tray, there will be a smart tag appear on the top-right corner of the ImageList.
Click the smart tag, and click "Choose Images" on the pane.
On the pop-up Image Collection Editor dialog, choose the images from the folder your want.
Click OK to finish adding images to the ImageList.
Click the ListView on the form, there will be a smart tag appear on the top-right corner.
Click the smart tag, you will find there're three ComboBoxes there, choose a ImageList from the list as you want.
Click the "Add items" option on the smart tag, a ListViewItem Collection Editor will appear, you can add items to the ListView, it's important here to set the ImageIndex or ImageKey property, or the image won't appear.
Click OK to finish item editing, now you'll find the images are displayed on the ListView.
If you want to add the images to the ListView by code, you can do something like this
give the following code in addButton_click
var fdlg = new OpenFileDialog();
fdlg.Multiselect = true;
fdlg.Title = "Select a file to add... ";
fdlg.InitialDirectory = "C:\\";
fdlg.Filter = "All files|*.*";
fdlg.RestoreDirectory = true;
if (fdlg.ShowDialog() == DialogResult.OK)
{
foreach (var files in fdlg.FileNames)
{
try
{
this.imageList1.Images.Add(Image.FromFile(files));
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
this.listView1.View = View.LargeIcon;
this.imageList1.ImageSize = new Size(32, 32);
this.listView1.LargeImageList = this.imageList1;
//or
//this.listView1.View = View.SmallIcon;
//this.listView1.SmallImageList = this.imageList1;
for (int j = 0; j < this.imageList1.Images.Count; j++)
{
ListViewItem item = new ListViewItem();
item.ImageIndex = j;
this.listView1.Items.Add(item);
}
}
In my listview I show thumbnails of small images in a certain folder. I setup the listview as follows:
var imageList = new ImageList();
foreach (var fileInfo in dir.GetFiles())
{
try
{
var image = Image.FromFile(fileInfo.FullName);
imageList.Images.Add(image);
}
catch
{
Console.WriteLine("error");
}
}
listView.View = View.LargeIcon;
imageList.ImageSize = new Size(64, 64);
listView.LargeImageList = imageList;
for (int j = 0; j < imageList.Images.Count; j++)
{
var item = new ListViewItem {ImageIndex = j, Text = "blabla"};
listView.Items.Add(item);
}
The user can rightclick on an image in the listview to remove it. I remove it from the listview and then I want to delete this image from the folder. Now I get the error that the file is in use. Of course this is logical since the imagelist is using the file.
I tried to first remove the image from the imagelist, but I keep on having the file lock.
Can somebody tell me what I am doing wrong?
Thanks!
You need to load the file into a MemoryStream, like this:
var image = Image.FromStream(new MemoryStream(File.ReadAllBytes(fileInfo.FullName)));
This way, the file will only be read once, and will not remain locked.
EDIT
You're loading the images into an ImageList.
Since the ImageList makes copies of its images, you should simply dispose the originals right away, like this:
using (var image = Image.FromFile(fileInfo.FullName))
imageList.Images.Add(image);
The image will need to be disposed of before it will unlock the file. Try calling Dispose on the image object after you remove it from the image list.
So long as you have a reference to the image object and the GC hasn't collected it, it will keep the lock. Calling Dispose should force it to give up its handle and cause the file to be unlocked.
You also have to make sure that the app didn't CopyHandle or otherwise get a second reference to the image resource before doing this.
Use GetThumbnailImage and then dispose image:
var image = Image.FromFile(fileN);
Image imgThumb = image.GetThumbnailImage(100, 100, null, new IntPtr());
imageList1.Images.Add(imgThumb);
image.Dispose();
listView1.LargeImageList = imageList1;
Now you can delete the file:
File.Delete(FileN);
How to navigate the image using Keyboard arrow keys in C#.
My 1st form contains listView. Listview contains 10 images in thumbnail format.Images are from perticular folder. When i double click the image in the list view, its opening in a new window as large image. I want to navigate the image in the new window as per listview order.
If i click the image randomly, want to navigate the image from that selected image.
Its like a Microsoft picture manager.
Plz give me an Idea.
Set your form's KeyPreview property to True. Then add this line of code to the top of your CS file:
using System.IO;
Next, inside the scope of your form, add these two lines:
private FileInfo[] _files;
private int _currentFile;
In your form's Load event, put this code:
DirectoryInfo dirinfo = new DirectoryInfo(
Path.Combine(Application.StartupPath, "images"));
_files = dirinfo.GetFiles();
_currentFile = 0;
Bitmap bmp = (Bitmap)Bitmap.FromFile(_files[_currentFile].FullName);
pictureBox1.Image = bmp;
Finally, in your form's KeyDown event, put this code:
if (e.KeyCode == Keys.Down)
{
_currentFile--;
if (_currentFile < 0)
{
_currentFile = _files.Length - 1;
}
}
else if (e.KeyCode == Keys.Up)
{
_currentFile++;
if (_currentFile >= _files.Length)
{
_currentFile = 0;
}
}
Bitmap bmp = (Bitmap)Bitmap.FromFile(_files[_currentFile].FullName);
if (pictureBox1.Image != null)
{
pictureBox1.Image.Dispose();
}
pictureBox1.Image = bmp;
This code assumes that you have a PictureBox on your form named "pictureBox1", and it assumes that you have a folder named "\images" in your application folder that contains the image files you wish to display.
As o.k.w. mentioned in a comment, you may want to enhance this code by resizing the PictureBox to fit the dimensions of the image file. You can do that by setting the SizeMode property of your PictureBox to AutoSize (or set it to CenterImage, if you wish to keep the overall image centered on your form).