Previewing images in a listBox using pictureBox - c#

I am using Winforms to create a 2D Map Editor.
I want to be able to preview an image of my assets that are stored in listBox using a pictureBox.
My current code for doing so is thus.
private void listBox_Assets_SelectedIndexChanged(object sender, EventArgs e)
{
pictureBox1.Image = Image.FromFile(((FileInfo)listBox_Assets.SelectedItem).FullName);
}
But when I select an asset I get this error.
Unable to cast object of type 'System.String' to type 'System.IO.FileInfo'.
I have searched high and low for a solution but can't find an answer to this error, any help would be greatly appreciated.

You use the file name from the listbox like this, and protect the code with a check for the file.
private void listBox_Assets_SelectedIndexChanged(object sender, EventArgs e)
{
string file = IO.Path.Combine("the directory", listBox_Assets.SelectedItem);
if (IO.File.Exists(file))
pictureBox1.Image = Image.FromFile(file);
}

Related

Image.FromFile and picturebox won't display image from computer

I'm using Winforms and have a picturebox. When the user clicks on it, I want to load a image from my computer. But its not working. Any ideas why?
private void pictureBox1_Click(object sender, EventArgs e)
{
pictureBox1.Image = Image.FromFile("C:/wamp/www/cyber.jpeg");
pictureBox1.Image = new Bitmap(#"C:\wamp\www\cyber.jpeg");
}
If I click on it, I get the error "FileNotFoundException was unhandled". If I remove the Image.FromFile and use the Bitmap instead, I get the error "Parameter is not valid"
try this
pictureBox1.Image = Image.FromFile(#"C:\wamp\www\cyber.jpeg");
Assuming your filepath is correct , I think You have .jpg as extension for your image but you are trying to access with extension .jpeg
Try This:
pictureBox1.Image = Image.FromFile("C:/wamp/www/cyber.jpg");

Using custom image buttons to display an image in RichTextBox

I am creating a text editor much like notepad/word and i have buttons on my form with custom images attached. The images have been loaded into the resource folder. Now when i click the button i want it to display that same image in the RichTextBox, the images being displayed will be various emoticons. I only found ways to create picture viewers and not this.
private void button11_Click(object sender, EventArgs e)
{
Image.FromFile("../../Resources/sad-icon.png");
}
This will be used to place a sad emoticon in the RichTextBox. This is what i have so far i am pretty new to C#.
I have been trying various methods and i found one that works but only with certain emoticons and not the emoticons that i need to be able to use.
void InsertEmoticon(string ImageName)
{
int StringLength = 0;
StringLength = txtPAD.Text.Length;
Image img = Image.FromFile("../../Resources/Emoticons/" + ImageName + ".png");
Clipboard.SetImage(img);
txtPAD.SelectionStart = (StringLength + 1);
txtPAD.Paste();
txtPAD.Clear();
}
I will then use:
private void button10_Click(object sender, EventArgs e)
{
InsertEmoticon("glad");
}
Am i missing something here? The images have been loaded into the resource folder and are inside the folder Emoticons.

picture viewer in c# .net

Regarding .Net technology,What will be the basic element of a picture viewer(like Windows Picture and Fax Viewer)? Is it a user control inside a form, or is it something other components. Could you please give me an idea in the context of C#.Net
You don't really get one that's bundled into .NET Framework (and that's probably a good thing, it's fairly large already).
If using WinForms, the nearest thing you do get is the PictureBox component, and the BackgroundImage property of some other components like Form and Panel. But you have to implement the rest of the functionality of an image viewer yourself.
WPF certainly has its own equivalents but I can't name them off the top of my head.
for making picture viewer is better to have PictureBox to show Pictures, ImageList to hold list of images due to have more that one picture and also two buttons for next pictur and previews picture.
a simple code which I suggest is as follow:
private void btnLoadImage_Click(object sender, EventArgs e)
{
imageList1.Images.Clear();
if (openFDialogImage.ShowDialog() == DialogResult.OK)
{
for (int i = 0; i < openFDialogImage.FileNames.Length; i++)
{
imageList1.Images.Add(Image.FromFile(openFDialogImage.FileNames[i]));
}
pictureBox1.Image = imageList1.Images[currentIndex];
}
}
private void BtnForward_Click(object sender, EventArgs e)
{
if(currentIndex!=imageList1.Images.Count-1 && imageList1.Images.Count > 0)
{
pictureBox1.Image = imageList1.Images[currentIndex++];
}
}
private void btnBack_Click(object sender, EventArgs e)
{
if (currentIndex!=0)
{
pictureBox1.Image = imageList1.Images[--currentIndex];
}
}
Thats all, :)

using image in if statement

how can i use image in if statement?
for example i want to check in click event of a picturebox that if a specific image(for example image1) is in it,then do some work.thanks in advance
update:
like this
the picturebox can be null or have an image in it,i want to check image1 is in it or not
private void pictureBox34_Click(object sender, EventArgs e)
{
if (///picturebox34=image1)
{
f();
}
}
now when i compile this code i took error:cannot convert system.drawing.image to bool
If you have all the images loaded into memory then you could simply perform a reference comparison between the Image object assigned to the PictureBox and the other Image objects to determine which one is in the PictureBox.
Alternatively when you assign the Image to the PictureBox you can set the Tag property of the PictureBox to identify the Image and use the value of the Tag property for the test in your event handler. Of course in this case you only need the one Image in memory, but you will have to have some kind of identifying information, like the name of the image to assign to the Tag property.
Update: Based on your updated question, it seems you are happy to perform a refernce comparison. Which you can do as follows
private void pictureBox34_Click(object sender, EventArgs e)
{
if (picturebox34.Image == image1)
{
f();
}
}

Setting a picture from my Resources programmatically to a PictureBox

How can I set a picture to a PictureBox in code?
The code below gives me the error:
Cannot implicitly convert Bitmap to
String.
private void ptbLocalidadAdd_MouseEnter(object sender, EventArgs e)
{
ptbLocalidadAdd.ImageLocation = Properties.Resources.addg;
}
If the resource is a bitmap, this should work:
ptbLocalidadAdd.Image = Properties.Resources.addg;

Categories