Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Hi,

I have to upload images into my windows application using openDialogBox. Is there any method to store the images inside the application.? How is this possible? Please give me any idea about this.
Posted

What do you mean by 'upload' ? You can store images anywhere you like, so long as you have permission. I'd think a folder inside the app data area of windows is a logical place, much easier than storing it in a DB.
 
Share this answer
 
Comments
Neha Thanka 31-Jul-12 21:30pm    
I want to store photos ofb employees. These photos are selecting through openFileDialog box. Then how can that image stored in a folder?
Christian Graus 31-Jul-12 21:33pm    
Use Environment.SpecialFolder to find the location, System.IO.Path to build your new file location string, and System.IO.File to copy the file.
1. use openfiledialog to ask the file from the user.
2. filter the openfiledialog to accept images only.
3. Use File.Copy function to copy file from openfiledialog opened filepath to your chosen destination path.
 
Share this answer
 
I think the better option to storing image is to convert image to byte[] then store it in Database
Open a File First
C#
byte[] big;
private void button7_Click(object sender, EventArgs e)
        {
             OpenFD.Title = "Select Files";
            OpenFD.Filter = "Jpg|*.jpg|Jpge|*.jpge|Gif|*.gif";
            OpenFD.FileName = null;
            string fileName;
            if (OpenFD.ShowDialog() != DialogResult.Cancel)
            {
                querybuilder qu = new querybuilder();
                fileName = OpenFD.FileName;
                Object refmissing = System.Reflection.Missing.Value;
                try
                {
                     // show it to picturebox
                    pictureBox2.Load(fileName);
                     // Here get_image is a function and Big is the byte[] type
                    big = get_image(fileName);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error" + ex.Message.ToString());
                }
            }
        }

get_image function is below
C#
public byte[] get_image(string filePath)
        {
            FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
            BinaryReader reader = new BinaryReader(stream);
            byte[] photo = reader.ReadBytes((int)stream.Length);
            reader.Close();
            stream.Close();

            return photo;
        }

And now make sql query
C#
string sqlquery="insert into Tablename(id,image) values(1,@big)";
SqlParameter param = new SqlParameter("@big", SqlDbType.Binary);
    param.Value = big;

Note:- Create Sqlcommand self I didn't include it in that solution
 
Share this answer
 
v2
C#
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();
        }




make on button click on windows application.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900