C# images inside of a ListView - c#

I want to make a ListView that has small images, that was taken from a scanner, inside of the ListView. (I have the scanning script done, and it saves the scanned image under C:/Temp/*.jpg. )
What I'm having trouble with is, I want the scanned images to be displayed inside of the ListView and when you click an image in the ListView it displayed the full Image in the PictureBox.
An Image of what i'm talking about.(tried to post the image inside of this post but rep isn't high enough)
I was thinking about having the images' location stored inside of an List array like
List<string> fileLocationArray = new List<string>();
foreach () {
...
string fileLoc = (#"C:\temp\" + DateTime.Now.ToString("yyyy-MM-dd HHmmss") + ".jpeg");
fileLocationArray.Add(fileLoc);
...
}
Then displaying the images inside the ListView using the List array.
Keep in mind that I plan on uploading these images to an FTP server. That's why I wanted to use a List array.
one more thing, they will be picture of document, not photos, if that means anything to you.

**Fill ListView :**
For(int i=0; i<fileLocationArray.Count();i++)
{
System.Windows.Controls.Image imgControl=new System.Windows.Controls.Image();
BitmapImage imgsrc = new BitmapImage();
imgsrc.BeginInit();
imgsrc.UriSource=fileLocationArray[i];
imgsrc.EndInit();
imgControl.source=imgsrc;
listView.Items.Add(imgControl);
}
**After filling ListView control create event listView SelectionChanged**
**imgContolShow // this control show selected image**
void listw_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
imgContolShow.Source = ((System.Windows.Controls.Image)listwiev.SelectedItem).Source;
}

Related

How to check if a picture box contains a certain image using an IF statement

I am trying to check if a PictureBox contains a certain image, the way I am trying to do it seems like it would work in my head, however, does not, i am not sure if there is any other way to check if the picturebox on the form contains a certain image.
private void user_btn_Click(object sender, EventArgs e)
{
//If statement to check if the forms picture box contains a certain image
if (pictureBox1.Image == Resources.user_male_white_red_brown)
{
this.Hide();
UserProfile User = new UserProfile();
User.ShowDialog();
User.pictureBox1.Image = Resources.user_male_white_red_brown;
this.Close();
}
else if (pictureBox1.Image == Resources.user_female_olive_orange)
{
this.Hide();
UserProfile User = new UserProfile();
User.ShowDialog();
User.pictureBox1.Image = Resources.user_female_olive_orange;
this.Close();
}
}
Although PictureBox Image might be identical as the one in the resources but they are not the same as reference. (Imagine like you have 2 copy of the same photo although their image is the same but they are 2 separate photos).
There are a few ways you can do it, one of them (a simple one) is to set the picturebox Tag to a relevent value whenever setting its image and compare that value instead of comparing images:
User.pictureBox1.Image = Resources.user_male_white_red_brown;
User.pictureBox1.Tag = "user_male_white_red_brown";
And then:
if((string)User.pictureBox1.Tag == "user_male_white_red_brown")
{
// your logic
}
This way you need to set PictureBox tag whenever you set its image.
Another way would be loading all images from resources to an array and setting the PictureBox image from the array, this way as the reference of both images (picturebox.Image and the image item in the array) are the same comparison would work for images but I think the first solution is easier.

What is the fastest way to store one or more user-input images during runtime with their original dimensions for processing later?

I am writing a windows forms application in c# that resizes user-input images. I'm currently trying to add functionality that will allow the user to drag and drop one or more images onto the form, display these images in a list (more detail on current implementation below), then allow the user to click on an image on the list at which point the selected image will show up in a picturebox. With this, I will implement 'batch' resizing.
Current implementation :
in the DragDrop event, populate a string array with the dropped files
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
call 'areAllImages' method which iterates through each file in the string array and returns true if all files are valid image file types, else returns false
if areAllImages == false, show an error messagebox
if (areAllImages(files) == false)
{
MessageBox.Show("Only files with .jpg, .jpeg, .gif, .bmp and .png extensions are allowed. Check the file(s) you are attempting to import into the program.");
}
if areAllImages == true, run a few methods that do stuff with the interface (irrelevant to question), then iterate through the array, adding Bitmaps of the images in the array to an imageList and adding the imageList elements into a Listview (I named it imageListView) :
int i = 0;
foreach (string element in files) //files == string array containing my image files
{
string fileName = Path.GetFileName(files[i]);
Bitmap img = new Bitmap(files[i]);
imageList1.Images.Add("Image" + i, img);
imageListView.Items.Add(fileName, i);
i++;
}
I then have an event handler for the ListView that shows whatever item was selected in the ListView in a PictureBox (Named imgBox). Ignore the updatePreviewer and updateDimensions methods - they are irrelevant.
private void imageListView_SelectedIndexChanged(object sender, EventArgs e)
{
foreach (ListViewItem item in imageListView.SelectedItems)
{
int imgIndex = item.ImageIndex;
if (imgIndex >= 0 && imgIndex < this.imageList1.Images.Count)
{
Bitmap img = new Bitmap(this.imageList1.Images[imgIndex]);
procedures.updatePreviewer(img, imgBox);
updateDimensions(img, heightLabel, widthLabel);
}
}
}
This works - but, the images that get shown in the PictureBox (imgBox) are 16x16 px. I know that this is because the ImageList imageSize is set to 16x16, but I can't find a way to store the drag and dropped image(s) with its/their original dimensions. The maximum is 256, and I'm going to have to allow inputting images much much larger than that.
Question : How could I manipulate this implementation to show images with their original dimensions? If it is not possible/feasible, what are some alternative ways that I could store these images so that I can access them through some sort of list container and show them in a PictureBox with their original dimensions?
Thank you
Solved the problem by adding an array to hold all the filepaths of the imported images and instead of loading the image from the listview index, I load images from the path at the array index equivalent to the item clicked

Problems with creating combined image view / camera view window using EmguCV & Windows Forms

Basically, what I'm trying to do is to create a window with list of images and large image view box. If user points at one of the images in the list, imagebox should display it, otherwise it should display live feed from a webcam.
I'm trying to achieve that by using ImageGrabbed event of EmguCV.Capture, and a field that stores index of the currently viewed image. Camera feed works alright, however, when I move the mouse over listbox, image view merely stops updating. It stays stuck at the last frame it saw, and doesn't display the image I'm pointing at. I've checked that the list has images in it, that index is correct, and that image coming from the camera is different from ones saved in the list. Also, same behavior happens when I use vanila PictureBox.
See the code (stripped of extra parts) below.
Initialization:
int ViewedFrame = -1;
var Frames = new List<Image<Rgb, byte>>();
// This list will have some images added to it along the way.
// They will be displayed in a custom drawn ListBox bound to the list.
Camera = new Emgu.CV.Capture(cameraid);
Camera.SetCaptureProperty(Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_FRAME_WIDTH, width);
Camera.SetCaptureProperty(Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_FRAME_HEIGHT, height);
Camera.ImageGrabbed += UpdateCamera;
Camera.Start();
Frame capture:
private void UpdateCamera(object sender, System.EventArgs ev)
{
if (ViewedFrame == -1)
Preview.Image = Camera.RetrieveBgrFrame();
else
// !!! this line has no apparent effect, image in the ImageBox is still the same
Preview.Image = Frames[ViewedFrame];
}
Mouse handling:
private void FrameList_MouseMove(object sender, MouseEventArgs e)
{
int index = FrameList.IndexFromPoint(e.Location);
if (index == ListBox.NoMatches)
ViewedFrame = -1;
else if (index != ViewedFrame)
{
ViewedFrame = index;
}
}
private void FrameList_MouseLeave(object sender, EventArgs e)
{
ViewedFrame = -1;
}
UPD: I tried adding Preview.Update() and Preview.Refresh() into the same branch as Frames[ViewedFrame], and after the conditional operator. In first case it doesn't have any effect. In second case nothing is shown at all. In case the ImageBox can't keep up updating itself, I enabled doublebuffering, but that didn't help either. Moreover, after I hover the mouse over an image in the list, the camera stream breaks too. The code above is the only one that's somewhat working.

Having trouble with URI Image in Visual Studio 2012?

I am currently creating a C# XAML Windows Store App I have successfully gotten the app to do what i want it to do which is when I click on a listviewitem the image to the right changes, however to do this for each listview item i am having to create a bitmap image for every listviewitem.
I find it very time consuming would anybody be able to recommend a type of method i could create to save me from writing this in every listviewitem Selection_changed.
BitmapImage PlatoImage = new BitmapImage(new Uri(this.BaseUri,"example.jpg"));
PhilosopherImage.Source = PlatoImage;
Below is the original method
private void PhilosopherList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (PhilosopherList.SelectedIndex ==1)
{
BitmapImage Aristotle = new BitmapImage(new Uri(this.BaseUri, "aristotle.png"));
PhilosopherImage.Source = Aristotle;
All i have done for the rest is insert 'else if' statements to compensate for the other 'selectedindex' I haven't created a method yet to save me time to create a new bitmap image for each listviewitem yet
It sounds like your listview items are strings.
Without using data binding, you can change the ListView items to objects that contain the string you want to display and the image url, then add a tostring() method that returns the string you want to display. Then your SelectionChanged handler can simply grab the image url from the selected item.
The listview item type would be something like this:
public class ItemType
{
public string DisplayString{get;set;}
public string ImageUrl{get;set;}
public override string ToString()
{
return DisplayString;
}
}

C# Winforms - Change a Listview item's label

I have looked through the listview controls and wasnt able to find how the label for each item is defined. When I load an image into the listview, I see the full path of the image under the image. What Im looking to do is replace the text with something more descriptive of the image itself, and not the path where it is located.
If someone can point me in the right direction, I would be thankful.
Edit to be more descriptive: What I have is a program that automatically loads images when it starts, the images are loaded into a listview and shown in the Large Icon view. So when they are loaded, the icon displays, and the text(label) underneath shows the path of the image. If I can get it to display just the filename, that would be awesome. All this is done programmatically.
private void Form1_Load(object sender, EventArgs e)
{
listView1.LargeImageList = imageList1;
var files = System.IO.Directory.GetFiles(System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + "\\icons");
foreach (var item in files)
{
addImage(item);
}
}
private void addImage(string imageToLoad)
{
if (imageToLoad != "")
{
imageList1.Images.Add(Image.FromFile(imageToLoad));
listView1.BeginUpdate();
listView1.Items.Add(imageToLoad, imageList1.Images.Count - 1);
listView1.EndUpdate();
}
}
I believe it's just the Text Property of ListViewItem.
Edit:
So just change this line:
listView1.Items.Add(imageToLoad, imageList1.Images.Count - 1);
to
listView1.Items.Add(System.IO.Path.GetFileName(imageToLoad), imageList1.Images.Count - 1);

Categories

Resources