I am using below code to load image in my image button. But the Image is not loading in Button. However, I'm not receiving any kind of error.
XAML Code:
Image Name="imgPhoto" HorizontalAlignment="Left" Height="160" Margin="10,41,0,0" VerticalAlignment="Top" Width="164"/>
Button Content="Load" HorizontalAlignment="Left" Margin="191,67,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/>
Below is the code to load the Image in Image Button.
Button Click Event Code:
private void Button_Click_1(object sender, RoutedEventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "Image files (*.jpg)|*.jpg|All Files (*.*)|*.*";
if (dlg.ShowDialog() == true)
{
string selectedFileName = dlg.File.Name;
imgPhoto.Source = new BitmapImage(new Uri(selectedFileName, UriKind.Relative));
}
}
with OpenFIleDialog we don't have File.Name we have only FileName property which Gets or sets a string containing the file name selected in the file dialog box.
check the below code
OpenFileDialog objOpenFileDialog = new OpenFileDialog();
objOpenFileDialog.Filter = "Image Files(.jpg)|*.jpg;*.gif;*.png";
if (objOpenFileDialog.ShowDialog() == true)
{
imgPhoto.Source =new BitmapImage(new Uri(objOpenFileDialog.FileName));
}
The problem is here: UriKind.Relative.
If you're building URI from file name, picked up from system's file dialog, then it is absolute URI:
myImage.Source = new BitmapImage(new Uri(fileDialog.FileName));
In other words, you should treat full path (e.g., "c:\folder\filename.ext") as absolute URI.
Change imgPhoto.Source code as below
private void Button_Click_1(object sender, RoutedEventArgs e)
{
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.Filter = "Image files (*.jpg)|*.jpg|All Files (*.*)|*.*";
if (dlg.ShowDialog() == true)
{
string selectedFileName = dlg.FileName;
//imgPhoto.Source = new BitmapImage(new Uri(selectedFileName, UriKind.Relative));
imgPhoto.Source = (ImageSource)new ImageSourceConverter().ConvertFromString(selectedFileName);
}
}
Related
At the moment I have three buttons on a form, each opens a different form (form2 with a textbox to display the text from the textfile, form3 with a picturebox to display the image)
What I am trying to do is put the two together for my last button so the user can filter which type to open (TXT Files or Image files). I am not sure how I can put the two together and get them to work.
The code I used to just open text files:
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.InitialDirectory = #"C:\";
ofd.Filter = "TXT Files(*.txt;)|*.txt;";
if(ofd.ShowDialog() == DialogResult.OK)
{
using(StreamReader rdText = new StreamReader(ofd.FileName))
{
string info = File.ReadAllText(ofd.FileName);
TextDocumentForm newTextDocument = new TextDocumentForm();
newTextDocument.TextFileName = info;
newTextDocument.Show();
}
}
}
What I use to open my image files
private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog ofdi = new OpenFileDialog();
ofdi.InitialDirectory = #"C:\";
ofdi.Filter = "Image Files(*.jpg;*.jpeg;*.bmp)|*.jpg;*.jpeg;.bmp;";
if (ofdi.ShowDialog() == DialogResult.OK)
{
Image image = Image.FromFile(ofdi.FileName);
ImgDoc newImageDoc = new ImgDocumentForm();
newImageDoc.ImageShow = image;
newImageDoc.Show();
}
}
Any help is appreciated as I am trying to develop my understanding of how OpenFileDialog still works.
Combining filters:
var openFile = new OpenFileDialog
{
InitialDirectory = #"C:\",
Filter = "TXT Files(*.txt;)|*.txt;|Image Files(*.jpg;*.jpeg;*.bmp)|*.jpg;*.jpeg;.bmp;"
};
Then use Path.GetExtension() to see which route you should take:
if (openFile.ShowDialog() == true)
{
var ext = System.IO.Path.GetExtension(openFile.FileName);
if (ext == ".txt")
{
// Open text file
}
else if (ext == ".jpg" || ext == ".jpeg" || ext == ".bmp")
{
// Open image file
}
}
I saw many similar threads, but nothing that could help me solving my problem. I'm new to c# and I want to load 3 images into 3 picture boxes from 3 different folders on a form and print them later. The images are created through screenshots by a third party application and saved in those folders.
Nevertheless I can't define their names which is making problems with the file path, I think...
Instead of creating a SystemWatchFolder, I saw someone using:
open.Filter = "Image Files (*.png)|*.png"
Is that working in general or do I need a watch folder?
I tried to combine codes from similar projects and ended up with the one below (btw sorry for posting the whole code).
I also tried to change the path to: (#"C:....)
same error message.
I really need and appreciate your help, comments, thoughts etc.
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Bitmap bmp = new Bitmap("C:\\Users\\Public\\1-2d27a482-b755\\Files\\Snapshots\\Snapshot2\\");
OpenFileDialog open = new OpenFileDialog();
open.Filter = "Image Files (*.png)|*.png";
pictureBox1.Image = bmp;
}
private void PictureBox1_Click(object sender, EventArgs e)
{
Bitmap bmp = new Bitmap("C:\\Users\\Public\\1-2d27a482-b755\\Files\\Snapshots\\Snapshot2\\");
OpenFileDialog open = new OpenFileDialog();
open.Filter = "Image Files (*.png)|*.png";
pictureBox2.Image = bmp;
}
private void PictureBox2_Click(object sender, EventArgs e)
{
Bitmap bmp = new Bitmap("C:\\Users\\Public\\1-2d27a482-b755\\Files\\Snapshots\\Snapshot3\\");
OpenFileDialog open = new OpenFileDialog();
open.Filter = "Image Files (*.png)|*.png";
pictureBox3.Image = bmp;
}
private void PictureBox3_Click(object sender, EventArgs e)
The images in the boxes are not shown and I get this error:
System.ArgumentException: 'Parameter is not valid.'
for this line:
Bitmap bmp = new Bitmap("C:\\Users\\Public\\1-2d27a482-b755\\Files\\Snapshots\\Snapshot1\\");
What are you doing wrong
At first your Bitmap should take path to your picture. So it must be initialized at the end after user selects picture in OpenFileDialog. Also, you never opened your OpenFileDialog.
So all your methods should look look this:
private void PictureBox1_Click(object sender, EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
open.Filter = "Image Files (*.png)|*.png|All files (*.*)|*.*";
if (open.ShowDialog() == DialogResult.OK)
{
Bitmap bmp = new Bitmap(open.FileName);
pictureBox1.Image = bmp;
}
}
Better way to do this
You really don't need to create three similar methods that doing the same thing.
You can create only one and use it in all picture boxes:
private void PictureBox_Click(object sender, EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
open.Filter = "Image Files (*.png)|*.png|All files (*.*)|*.*";
if (open.ShowDialog() == DialogResult.OK)
{
Bitmap bmp = new Bitmap(open.FileName);
PictureBox targetPictureBox = e.Source as PictureBox;
targetPictureBox.Image = bmp;
}
}
In the following code:
Bitmap bmp = new Bitmap("C:\\Users\\Public\\1-2d27a482-b755\\Files\\Snapshots\\Snapshot2\\");
OpenFileDialog open = new OpenFileDialog();
open.Filter = "Image Files (*.png)|*.png";
pictureBox2.Image = bmp;
there are two problems:
Problem 1
In the line:
Bitmap bmp = new Bitmap("C:\\Users\\Public\\1-2d27a482-b755\\Files\\Snapshots\\Snapshot2\\");
You are loading a Bitmap image from a file. The path:
"C:\\Users\\Public\\1-2d27a482-b755\\Files\\Snapshots\\Snapshot2\\"
is incorrect because it cant end with a \\.
It should end with Snapshot2 or Snapshot2.png depending on the name of the file.
Problem 2
In the lines:
OpenFileDialog open = new OpenFileDialog();
open.Filter = "Image Files (*.png)|*.png";
You are initializing an OpenFileDialog but not using it. You can get the file name by using a file dialogue or hard-code the file name as you did in the first line, but you are mixing theme up.
Option 1: Hard-coding the file name
This should work, assuming the file name is Snapshot2.png:
Bitmap bmp = new Bitmap("C:\\Users\\Public\\1-2d27a482-b755\\Files\\Snapshots\\Snapshot2.png");
pictureBox2.Image = bmp;
Option 2: Using a file dialogue
OpenFileDialog open = new OpenFileDialog();
open.Filter = "Image Files (*.png)|*.png";
if (open.ShowDialog() == DialogResult.OK)
{
pictureBox2.Image = new Bitmap(open.FileName);
}
I need help with this code. I want to create basic image convert program but this program is not working? What am I doing wrong. Thanks for answers.
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog file = new OpenFileDialog();
file.ShowDialog();
string DosyaYolu = file.FileName;
string DosyaAdi = file.SafeFileName;
if (file.ShowDialog() == DialogResult.OK)
{
System.Drawing.Image image = System.Drawing.Image.FromFile(DosyaYolu);
image.Save(DosyaYolu, System.Drawing.Imaging.ImageFormat.Png);
}
You choose the wrong target path to save the new image to. Also you invoked the ShowDialog() twice, which is not necessary. The following code will save the new file with the same name but a different extension.
var dialog = new OpenFileDialog();
if (dialog.ShowDialog() == DialogResult.OK)
{
string sourceFile = dialog.FileName;
string targetFile = Path.ChangeExtension(sourceFile, "png");
Image image = Image.FromFile(sourceFile);
image.Save(targetFile, ImageFormat.Png);
}
I have a TextBox named textbox1 and a Button named button1.
When I click on button1 I want to browse my files to search only for image files (type jpg, png, bmp...).
And when I select an image file and click Ok in the file dialog I want the file directory to be written in the textbox1.text like this:
textbox1.Text = "C:\myfolder\myimage.jpg"
Something like that should be what you need
private void button1_Click(object sender, RoutedEventArgs e)
{
// Create OpenFileDialog
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
// Set filter for file extension and default file extension
dlg.DefaultExt = ".png";
dlg.Filter = "JPEG Files (*.jpeg)|*.jpeg|PNG Files (*.png)|*.png|JPG Files (*.jpg)|*.jpg|GIF Files (*.gif)|*.gif";
// Display OpenFileDialog by calling ShowDialog method
Nullable<bool> result = dlg.ShowDialog();
// Get the selected file name and display in a TextBox
if (result == true)
{
// Open document
string filename = dlg.FileName;
textBox1.Text = filename;
}
}
var ofd = new Microsoft.Win32.OpenFileDialog() {Filter = "JPEG Files (*.jpeg)|*.jpeg|PNG Files (*.png)|*.png|JPG Files (*.jpg)|*.jpg|GIF Files (*.gif)|*.gif"};
var result = ofd.ShowDialog();
if (result == false) return;
textBox1.Text = ofd.FileName;
Goal:
Save a picture from the harddrive into my WPF application. The picture should be available if copying the WPF application. The address to the picture located in the WPF application should be saved in the database.
Problem:
How should I do it in a course of action?
private void btnBrowse_Click(object sender, RoutedEventArgs e)
{
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.Filter = "jpg files (*.jpg)|*.jpg|gif files (*.gif)|*.gif|jpeg files (*.jpeg)|*.jpeg";
Nullable<bool> result = dlg.ShowDialog();
if (result == true)
{
// Open document
string filename = dlg.FileName;
txtPicture.Text = filename;
BitmapImage myBitmapImage = new BitmapImage(new Uri(dlg.FileName, UriKind.Absolute));
string sss = myBitmapImage.Format.ToString();
string asd = dlg.SafeFileName.ToString();
}
}
There are several image encoders available. A simple example for PNG files can be found here, or a more complete sample here. The same concept applies for the other supported image file types.