uploading images in winforms application - c#

Does anyone know if a there is a control to allow the user to upload a image to a windows form? Or any example code to accomplish this.
I am using win-form applications
Thanks,

To allow users to select files in a Windows Forms application you should look into using the OpenFileDialog class.
To use the dialog on your form you will need to find it in the toolbox in Visual Studio and drag it on to your form.
Once associated with the form you can then invoke the dialog from your code like so:
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string selectedFile = openFileDialog1.FileName;
}
You can then use the file path to perform whatever task you wish with the file.
Note: You can use the FileDialog.Filter Property to limit the type of file extensions (images in your case) the user can select when using the dialog.

It's note clear where you are going to upload your image. If you just want to use an image in a simple desktop application you can use OpenFileDialog to allow a user to select an image file. And then you can use this image path in you application. If you you want to upload this image to database you can read this image into memory using something like FileStream class.

OpenFileDialog open = new OpenFileDialog();
open.Filter = "Image Files(*.jpeg;*.bmp;*.png;*.jpg)|*.jpeg;*.bmp;*.png;*.jpg";
if (open.ShowDialog() == DialogResult.OK)
{
textBox10.Text = open.FileName;
}
cn.Open();
string image = textBox10.Text;
Bitmap bmp = new Bitmap(image);
FileStream fs = new FileStream(image, FileMode.Open, FileAccess.Read);
byte[] bimage = new byte[fs.Length];
fs.Read(bimage, 0, Convert.ToInt32(fs.Length));
fs.Close();
SqlCommand cmd = new SqlCommand("insert into tbl_products(Product_image) values(#imgdata)", cn);
cmd.Parameters.AddWithValue("#imgdata", SqlDbType.Image).Value = bimage;
cmd.ExecuteNonQuery();
cn.Close();

private void cmdBrowser_Click(object sender, EventArgs e)
{
OpenFileDialog fileOpen = new OpenFileDialog();
fileOpen.Title = "Open Image file";
fileOpen.Filter = "JPG Files (*.jpg)| *.jpg";
if (fileOpen.ShowDialog() == DialogResult.OK)
{
picImage.Image = Image.FromFile(fileOpen.FileName);
}
fileOpen.Dispose();
}

Related

OpenFile Dialog Box keeps resources open

After opening a photo in my application using the Openfile Dialog I cannot do anything with the file unless I close my application. I have placed the OpenFile Dialog in a using statement and tried various ways to release resources with no success. How can I release the process to avoid the error message "The process cannot access the file because it is being used by another process?
using (OpenFileDialog GetPhoto = new OpenFileDialog())
{
GetPhoto.Filter = "images | *.jpg";
if (GetPhoto.ShowDialog() == DialogResult.OK)
{
pbPhoto.Image = Image.FromFile(GetPhoto.FileName);
txtPath.Text = GetPhoto.FileName;
txtTitle.Text = System.IO.Path.GetFileNameWithoutExtension(GetPhoto.Fi‌​leName);
//GetPhoto.Dispose(); Tried this
//GetPhoto.Reset(); Tried this
//GC.Collect(): Tried this
}
}
Your problem is not (OpenFileDialog) your problem is for PictureBox
you can use this for load image or if this not works
do this for load image
OpenFileDialog GetPhoto = new OpenFileDialog();
GetPhoto.Filter = "images | *.jpg";
if (GetPhoto.ShowDialog() == DialogResult.OK)
{
FileStream fs = new FileStream(path: GetPhoto.FileName,mode: FileMode.Open);
Bitmap bitmap = new Bitmap(fs);
fs.Close(); // End using
fs.Dispose();
pbPhoto.Image = bitmap;
txtPath.Text = GetPhoto.FileName;
txtTitle.Text = System.IO.Path.GetFileNameWithoutExtension(GetPhoto.Fi‌​leName);
}
As stated in the doc for Image.FromFile:
The file remains locked until the Image is disposed.
So you could try to make a copy of the image and then release an original Image:
using (OpenFileDialog GetPhoto = new OpenFileDialog())
{
GetPhoto.Filter = "images | *.jpg";
if (GetPhoto.ShowDialog() == DialogResult.OK)
{
using (var image = Image.FromFile(GetPhoto.FileName))
{
pbPhoto.Image = (Image) image.Clone(); // Make a copy
txtPath.Text = GetPhoto.FileName;
txtTitle.Text = System.IO.Path.GetFileNameWithoutExtension(GetPhoto.Fi‌​leName);
}
}
}
If it not help, you could try to make a copy via the MemoryStream and the Image.FromStream method: System.Drawing.Image to stream C#

Load undefined images from folders

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);
}

How to display an image from file system in a PictureBox in WinForm

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);
}
}

How to save a picture in WPF application

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.

c# saving an image of a control

what is the best way to save an image of a control?
currently i am doing this:
chart1.SaveImage(ms, ChartImageFormat.Bmp);
Bitmap bm = new Bitmap(ms);
how would i then prompt the user with a windowsavedialogue and save the BMP to a specific location?
if this is not the best way to do this please suggest a different way
Daok has a nice answer for this.
Adapting Daok's code to change the extension Filter gives you this
chart1.SaveImage(ms, ChartImageFormat.Bmp);
Bitmap bm = new Bitmap(ms);
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.InitialDirectory = Environment.SpecialFolder.MyDocuments;
saveFileDialog1.Filter = "Your extension here (*.bmp)|*.*" ;
saveFileDialog1.FilterIndex = 1;
if(saveFileDialog1.ShowDialog() == DialogResult.OK)
{
bm.Save (saveFileDialog1.FileName);//Do what you want here
}
You could prompt them with a SaveFileDialog that would allow them to choose the path and file name and file type where they want to save the file.
Then you just need to write the bmp to a file
Do this:
SaveFileDialog dlg = new SaveFileDialog();
// ... add your dialog options
DialogResult result = dlg.ShowDialog(owner);
if(result == DialogResult.OK)
{
bm.Save(dlg.FileName);
}

Categories