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.
Related
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 2 system a c# winform and a php and their database is stored in a single database mysql now my problem is storing pictures...., in my html I save my pictures in htdocs/"foldername"/productimages now what I want in my C# winform was get the location path of the picture from open dialogue and copy that picture to the specific folder which is the htdocs/"foldername"/productimages how do i do that
my code
string picloc;
private void UpdBtn_Click(object sender, EventArgs e)
{
dlg.Filter = "JPG Files(*.jpg)|*.jpg|PNG Files(*.png)|*.png|ALL Files(*.*)|*.*";
dlg.Title = "Select Thumbnail";
if (dlg.ShowDialog() == DialogResult.OK)
{
// Result();
picloc = dlg.FileName.ToString();
pic1.ImageLocation = picloc;
}
}
so How do I copy the file picture from the string picloc to the specific loc?
You can use File.Copy to do this. Something like the following should manage the copy for you:
string picloc;
string new_loc;
private void UpdBtn_Click(object sender, EventArgs e)
{
dlg.Filter = "JPG Files(*.jpg)|*.jpg|PNG Files(*.png)|*.png|ALL Files(*.*)|*.*";
dlg.Title = "Select Thumbnail";
if (dlg.ShowDialog() == DialogResult.OK)
{
// Result();
picloc = dlg.FileName.ToString();
pic1.ImageLocation = picloc;
File.Copy(picloc, new_loc); // new_loc being the new location for the file.
}
}
This assumes you already know the location for the copy of the file. If you want to give the user the choice, do so via a SaveFileDialog to get the new location (as a string) and then perform the copy.
I have openfiledialog that reading user image address with file info and load it in textbox
I want to have another button in order to open image address (that already saved in textbox)
how to code this button at wpf ? I know i should use process.start but no idea !
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
tbl_Moshtari tt = new tbl_Moshtari();
dlg.FileName = "pic-file-name"; // Default file name
dlg.DefaultExt = ".jpg"; // Default file extension
dlg.Filter = "JPEG(.jpeg)|*.jpeg | PNG(.png)|*.png | JPG Files (*.jpg)|*.jpg|GIF Files (*.gif)|*.gif"; // Filter files by extension
Nullable<bool> result = dlg.ShowDialog();
if (result == true)
{
//// picbox.Source = new BitmapImage(new Uri(dlg.FileName, UriKind.Absolute));
//bitmapImage = new BitmapImage();
//bitmapImage.BeginInit();
//bitmapImage.StreamSource = System.IO.File.OpenRead(dlg.FileName);
//bitmapImage.EndInit();
////now, the Position of the StreamSource is not in the begin of the stream.
//picbox.Source = bitmapImage;
FileInfo fi = new FileInfo(dlg.FileName);
string filename = dlg.FileName;
txt_picaddress.Text = filename;
System.Windows.MessageBox.Show("Successfully done");
}
This second button i have
private void btn_go_Click(object sender, RoutedEventArgs e)
{
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
//FileInfo fi = new FileInfo(dlg.FileName);
string filename = dlg.FileName;
Process.Start(filename);
}
This isnt working for me .
Process.Start() should open up the image as long as filename is an absolute path to the file. With that being said, where in your btn_go_Click method are you actually opening up the dialog to get the file name? dlg.FileName returns an empty string if you don't show the dialog in which case Process.Start() fails.
If the file name needs to come from the previous dialog, you shouldn't create a new dialog; instead, change
Process.Start(filename)
to
Process.Start(txt_picaddress.Text)
Of course, you need to do some input verification to make sure the path is correct (unless the textbox is read-only).
Also, consider setting a breakpoint on string filename = dlg.FileName; to make sure it has the correct path to the file if it's still not working.
To open and highlight the file in Windows Explorer:
string filename = txt_picaddress.Text;
ProcessStartInfo pInfo =
new ProcessStartInfo("explorer.exe", string.Format("/Select, {0}", filename));
Process.Start(pInfo);
In your second code sample, you created a new instance of openFileDialog, you need instead to use the previous instance of the openFileDialog that is holding the correct image filename:
if you create the first openFileDialog in the window constructor you can do this:
private void btn_go_Click(object sender, RoutedEventArgs e)
{
string filename = this.dlg.FileName;
Process.Start(filename);
}
hope this helps, this is what i can say given the code you provided.
You don't need an OpenFileDialog in btn_go_Click if you want to use the path in your textbox:
private void btn_go_Click(object sender, RoutedEventArgs e)
{
string filename = txt_picaddress.Text;
Process.Start(filename);
}
I have a small doubt regarding loading an image in PictureBox in WinForms.
I want to show an image file from file system in a PictureBox on my form, say form1.
I am doing Windows applications using C#.
I want to check the file type also say is it pdf/text/png/gif/jpeg.
Is it possible to programmatically open a file from file system using C#?
If anyone knows please give any idea or sample code for doing this.
Modified Code: I have done like this for opening a file in my system, but I don't know how to attach the file and attach the file.
private void button1_Click(object sender, EventArgs e)
{
string filepath = #"D:\";
openFileDialog1.Filter = "Image Files (*.jpg)|*.jpg|(*.png)|*.png|(*.gif)|*.gif|(*.jpeg)|*.jpeg|";
openFileDialog1.CheckFileExists = true;
openFileDialog1.CheckPathExists = true;
if (openFileDialog1.ShowDialog(this) == DialogResult.OK)
{
try
{
}
}
}
I don't know what I have to write in try block. Can anyone help on this?
Use Image.ImageFromFile http://msdn.microsoft.com/en-us/library/system.drawing.image.fromfile.aspx method
Image img = Image.ImageFromFile(openFileDialog1.FileName);
Should work.
EDIT
If you're going to set it to PictureBox, and what to see complete inside it, use picturebox
SizeMode property.
using System.IO;
openFileDialog1.FilterIndex = 1;
openFileDialog1.Multiselect = false; //not allow multiline selection at the file selection level
openFileDialog1.Title = "Open Data file"; //define the name of openfileDialog
openFileDialog1.InitialDirectory = #"Desktop"; //define the initial directory
if (openFileDialog1.ShowDialog(this) == DialogResult.OK)
{
try
{
string filename = openFileDialog1.FileName;
FileStream fs=new FileStream(filename, FileMode.Open, FileAccess.Read); //set file stream
Byte[] bindata=new byte[Convert.ToInt32(fs.Length)];
fs.Read(bindata, 0, Convert.ToInt32(fs.Length));
MemoryStream stream = new MemoryStream(bindata);//load picture
stream.Position = 0;
pictureBox1.Image = Image.FromStream(stream);
}
}