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.
Related
I am trying to make a file display. When the user selects a file, it displays the icon of the file in the window. When I select the Google Chrome icon and click on 'OK' in the openfiledialog, the intended result happens. (see pictures below)
However, when I select another icon (e.g Word), it gives me the error 'Path does not exist'.
(see pictures below)
If I select another file (e.g File Explorer) it gives me 'Catastrophic Error' (see pictures below)
For some reason, this problem only happens with shortcut files. For other files like .txt files or .exe files, this problem does not occur.
Here is my code (Add_Item is the name of the button)
private void AddItem_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == true)
{
foreach (String myfile in openFileDialog.FileNames)
{
// here myfile represent your selected file name
//get filename
string filename = System.IO.Path.GetFileName(myfile);
//TODO: Create settings
Icon icon1 = System.Drawing.Icon.ExtractAssociatedIcon(myfile);
Bitmap icon = icon1.ToBitmap();
System.Windows.Controls.Image image = new System.Windows.Controls.Image();
image.Source = BitmapToImageSource(icon);
Tiles.Children.Add(image);
}
}
}
Can anyone help me?
Thanks
Alright, {DeferenceLinks = false} fixed my problem.
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.
}
}
}
}
In my SaveFileDialog I have multiple types in the filter, however when viewing the dialog if I choose a filter to view files of that type in the directory I am only able to see files for the first and last filters.
bool save;
SaveFileDialog dlg = new SaveFileDialog();
dlg.FileName = "*";
dlg.DefaultExt = "bmp";
dlg.ValidateNames = true;
dlg.Filter = "Bitmap Image (.bmp)|*.bmp|Gif Image (.gif)|*.gif |JPEG Image (.jpeg)|*.jpeg |Png Image (.png)|*.png |Tiff Image (.tiff)|*.tiff |Wmf Image (.wmf)|*.wmf";
save = (bool)dlg.ShowDialog();
if (save)
{
SaveImage(dlg.FileName);
}
I can see files of type .bmp and .wmf
If I change the order of the filters I can always only see the first and last.
Remove the spaces after the file type:
dlg.Filter = "Bitmap Image (.bmp)|*.bmp|Gif Image (.gif)|*.gif|JPEG Image (.jpeg)|*.jpeg|Png Image (.png)|*.png|Tiff Image (.tiff)|*.tiff|Wmf Image (.wmf)|*.wmf";
FilterIndex ... DefaultExt is used just during a save.
An index is 1-based so if you want to choose the 2nd option then:
dlg.FilterIndex = 2;
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);