if(pictureBox4.Image.ToString() ==
ePRO_Decision_Tool.Properties.Resources.mod_onalertq.ToString())...
How to read name of image file loaded in pictureBox (or from resources)?
The image loaded in PictureBox is just an array of bytes,
so to find out what is the filename you must fill the Tag property of PictureBox when any image loaded in it.
An Image object only contains the image's binary data. You can manually set the Tag property of the Image to contain the file name (After you've created the image).
If you load an image to the PictureBox using the Load() method, that will update the PictureBox's ImageLocation property to the file's path.
Then you can use pictureBox4.ImageLocation for comparison.
ImageLocation on MSDN
I'm pretty sure there's no way, the Image class doesn't expose where it came from.
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.FileName = "";
openFileDialog1.Title = "Images";
openFileDialog1.Filter = "JPG Image(*.jpg)|*.jpg|BMP Image(*.bmp)|*.bmp";
openFileDialog1.ShowDialog();
if (openFileDialog1.FileName.ToString() != "")
{
string imagePath = openFileDialog1.FileName.ToString();
string imagepath = imagePath.ToString();
imagepath = imagepath.Substring(imagepath.LastIndexOf("\\"));
imagepath = imagepath.Remove(0, 1);
}
}
You can use this way to get name of picture in picture box:
System.IO.Path.GetFileName(PictureBox.ImageLocation);
this Method just working with loading image by PictureBox.Image.Load(Image Path)
not working with load image directly from resource
not working with load image by PictureBoc.Image = Image.FromFile(Image Path)
because above methods(except Image.Load()) making Image.ImageLocation set to null
PictureBox.Image.Load("Image Path");
string imagepath = PictureBox.Image.ImageLocation.ToString();
imagepath = imagepath.Substring(imagepath.LastIndexOf("\\"));
imagepath = imagepath.Remove(0, 1);
get Image name by click
String getImageName = pictureBox1.Name;
MessageBox.Show(getImageName);
OpenFileDialog open = new OpenFileDialog();
open.Filter = "Image Files(* .jpg; * .jpeg; * .png;)|* .jpg; * .jpeg; * .png;";
if(open.ShowDialog() == DialogResult.OK){
piturebox1.Image = new Bitmap(open.FileName);
String ImageName = Path.GetFileName(open.FileName);
}
here is a simple way to get image name from picture box in c#:
string imgPath = pictureBox1.ImageLocation;
string nameImage =imgPath.Substring(imgPath.LastIndexOf('\\')+1);
Related
I'm trying to do something when I click image displayed inside pictureBox1.
pictureBox is loaded with this code:
string imgpath = #"img\256.png";
pictureBox48.Image = Image.FromFile(imgpath);
Then control is released to me so I can see that the picture loaded correctly.
Then i click the picture:
public void pictureBox48_Click(object sender, EventArgs e)
{
string variable1 = pictureBox48.ImageLocation;
Form3 fo = new Form3(variable1);
fo.ShowDialog();
}
This doesn't work. When I debug the code I see that variable1 stay null, that is pictureBox48.ImageLocation is null. Why is that? Shouldn't it be the path to the image that is assigned there?
You can't get the image path when you set the image using the Image property because you are assigning an Image object which can come from different sources.
Set the image using ImageLocation.
string imgpath = #"img\256.png";
pictureBox48.ImageLocation = imgpath;
When you click in the PictureBox you can get the path using the same property:
public void pictureBox48_Click(object sender, EventArgs e)
{
string variable1 = pictureBox48.ImageLocation;
Form3 fo = new Form3(variable1);
fo.ShowDialog();
}
When dealing with Image or PictureBox I would recommend to not use something like Location or Path of the image. Assume that when the image is loaded user removes it from the hard drive and you're left with the code full of errors.
That's why you should rely on Image itself as it contains every information about the image like pixel format, width, height and raw pixel data.
I would recommend you to just copy the image, not the path to the file.
This piece of code should give you a hint:
pixtureBox48.Image = Image.FromFile(imgPath);
// above code assumes that the image is still on hard drive and is accessible,
// now let's assume user deletes that file. You have the data but not on the physical location.
Image copyImage = (Image)pictureBox48.Image.Clone();
Form3 fo = new Form(copyImage); // change .ctor definition to Form(Image copy)
fo.ShowDialog();
So I want to display the file name of an image in a label in form 2, form 1 is where I'm going to select the picture and then it'll place it into form 2, but i want it to display the filename of the image that has been selected in the label called 'FileNameLabel' into form 2. This meaning the file location
If you have any more questions, feel free to ask.
This should display the filename.
if (SelectMedia.ShowDialog() == DialogResult.OK)
{
if (SelectMedia.FilterIndex == 1)
{
PictureViewer picture = new PictureViewer();
picture.SetPicture(SelectMedia.FileName);
FileNameLabel.Text = SelectMedia.FileName;
picture.Show();
}
}
Your SetPicture method takes the filename to put the picture into the picture box.
That is the same filename that will get displayed in your FileNameLabel.
If you are asking to display the full filepath then you need to do this.
if (SelectMedia.ShowDialog() == DialogResult.OK)
{
if (SelectMedia.FilterIndex == 1)
{
PictureViewer picture = new PictureViewer();
picture.SetPicture(SelectMedia.FileName);
FileInfo path = new FileInfo(SelectMedia.FileName);
FileNameLabel.Text = path;
picture.Show();
}
}
If those aren't what you are after explain a lot better about what you are trying to do.
I have an Image Control (WPF) called image1 that load an Image when I click on a commandbutton (here is the code about that event). Now what I have to do if I want to copy that image file on the current directory (so the project dir) ?
private void commandButton1_Click(object sender, System.Windows.RoutedEventArgs e)
{
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.DefaultExt = "*.jpg";
dlg.Filter = "Image Files|*.jpg";
Nullable<bool> result = dlg.ShowDialog();
if (result == true)
{
image1.Source = new BitmapImage(new Uri(dlg.FileName));
}
// to do
}
}
Use the File.Copy method. You may get the file name without path from Path.GetFileName:
// get file name from full source path (for example)
string targetFileName = Path.GetFileName(dlg.FileName);
// copy to relative path, i.e. current directory
File.Copy(dlg.FileName, targetFileName);
In case your question is about how to save a BitmapSource, you may look at BitmapEncoder.
I am trying to select an image and split it into 9 pieces and place randomly in a canvas. I am at the point where I have selected an image, but I am unsure of how to split the image.
I have looked at similar questions but no luck. I'm not looking for someone else to do the work, but advice on how best to do it.
private void uploadImage_Click(object sender, RoutedEventArgs e)
{
//creates (but not yet displays) a OpenFile dialog box
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
//specifies the look of the open file dialog box
dlg.DefaultExt = ".jpg";
dlg.Filter = "Image Files|*.jpg;";
dlg.InitialDirectory = Environment.CurrentDirectory;
//shows the open file dialog box to user and awaits OK or Cancel
bool? result = dlg.ShowDialog();
//result is true if user selects an image file
if (result == true)
{
filename = dlg.FileName;
var uri = new Uri(filename);
var bitmap = new BitmapImage(uri);
Image bg = new Image();
bg.Source = new BitmapImage(uri);
}
else
return;
}
I have already answered similiar question. Either use my solution, or resort to CroppedBitmap.
How to crop image and save into ImageSource in WPF?
I am looking for a way to embed image to Rich text box. My rtf file is portable which contains both image and text. i.e. It can be moved from one computer to another. So the user should be able to see the content of rtf file (text+image) even if its in another machine.
Now I am using the following code to insert image.
public static void ApplyImage(RichTextBox RichTextBoxControl)
{
try
{
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.DefaultExt = ".png";
dlg.Filter = "PNG Files (.png)|*.png|JPG Files (.jpg)|*.jpg|GIF Files (.gif)|*.gif";
Nullable<bool> result = dlg.ShowDialog();
if (result == true)
{
string fileName = dlg.FileName;
if (File.Exists(fileName))
{
BitmapImage bi = new BitmapImage(new Uri(fileName));
Image image = new Image();
image.Source = bi;
InlineUIContainer container = new InlineUIContainer(image);
Paragraph paragraph = new Paragraph(container);
RichTextBoxControl.Document.Blocks.Add(paragraph);
}
}
}
catch
{
throw;
}
}
But this code is not suitable for my purpose. Because the embedded image may not be in the other machine. So it won't work. Either I need to embed image or store image as binary in rtf file. I searched everywhere and no luck.
Can anyone help me please?
I got a solution for the problem. I am not sure its the right way, but still this solution is working for me. Hope it may help others.
The solution is to iterate through all image tags in flow document and copy the images to a repository folder which is portable. Then load the images from that repository folder next time to show the images back in to richtextbox.
foreach (Block block in rtb.Blocks)
{
if (block is Paragraph)
{
Paragraph paragraph = (Paragraph)block;
foreach (Inline inline in paragraph.Inlines)
{
if (inline is InlineUIContainer)
{
InlineUIContainer uiContainer = (InlineUIContainer)inline;
if (uiContainer.Child is Image)
{
Image image = (Image)uiContainer.Child;
//Copy Image source to another repository folder and save the path.
}
}
}
}