Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
F#
private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            open.Filter = "Image Files(*.jpeg;*.bmp;*.png;*.jpg)|*.jpeg;*.bmp;*.png;*.jpg";
            if (open.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = open.FileName;
            }
           
            string image = textBox4.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();
            
        }
Posted
Updated 12-Jun-15 0:41am
v2
Comments
Andy Lanng 12-Jun-15 6:49am    
debug
MayankSemwal 12-Jun-15 6:52am    
did.
Andy Lanng 12-Jun-15 6:53am    
result?

I was terse because you have given us next to no info.
What was the format of the filename?
Tomas Takac 12-Jun-15 6:52am    
What line?

Hi,

If you have a look at the documentation for the OpenFileDialog Class[^] you'll see that you should be setting a value for the FileDialog.InitialDirectory Property [^].

In your example:
C#
private void button1_Click(object sender, EventArgs e)
        {
            string AppPath = Directory.GetCurrentDirectory();
            OpenFileDialog open = new OpenFileDialog();
            open.InitialDirectory = "c:\\" ;
            open.Filter = "Image Files(*.jpeg;*.bmp;*.png;*.jpg)|*.jpeg;*.bmp;*.png;*.jpg";
            if (open.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = open.FileName;
                System.IO.File.Copy(open.FileName, Path.Combine(AppPath, Path.GetFileName(open.FileName)), true);
            }
        }


[edit]
Added basic example of how to copy selected file into application folder using File.Copy Method[^]
[/edit]
 
Share this answer
 
v2
Comments
MayankSemwal 12-Jun-15 7:02am    
@hypermellow from the page you suggested code,can you tell me how to save that picture into a folder in my application
hypermellow 12-Jun-15 7:21am    
Hi, I've updated the solution to demonstrate a basic file copy of the selected file.
... hope it helps.
MayankSemwal 12-Jun-15 7:24am    
thanks man, appreciate your help.. :)
If the user selects "OK", you're storing the selected path in textBox1. You're then trying to open the file based on the value of textBox4, which almost certainly doesn't contain a valid file path.

If the user selects "Cancel", you're still trying to open the file based on the value of textBox4.

Change your code to use the correct path, and only process the path if the user selects "OK":
C#
private void button1_Click(object sender, EventArgs e)
{
    using (OpenFileDialog open = new OpenFileDialog())
    {
        open.Filter = "Image Files(*.jpeg;*.bmp;*.png;*.jpg)|*.jpeg;*.bmp;*.png;*.jpg";
        if (open.ShowDialog() == DialogResult.OK)
        {
            textBox1.Text = open.FileName;
            
            // TODO: Do something with these...
            Bitmap bitmap = new Bitmap(open.FileName);
            byte[] bimage = File.ReadAllBytes(open.FileName);
        }
    }
}


Also, do yourself a favour and give your controls meaningful names. You might remember that textBox1 is the image path and textBox42 is the frood-name now, but when you come back to your code in a few weeks' time, you'll have forgotten what these controls represent.
 
Share this answer
 
Comments
hypermellow 12-Jun-15 7:27am    
... very valid and well made points, Richard.
THIS WORKED FINE AS PER MY NEEDS. POSTING IT SO OTHERS MIGHT GET HELP
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "jpg files (*.jpg)|*.jpg|All files (*.*)|*.*";


OpenFileDialog openImage = new OpenFileDialog();
DialogResult result = openImage.ShowDialog();
if (result == DialogResult.OK)
{
//Load image in picture box and set size mode property of image as normal.
pictureBox1.Load(openImage.FileName);
pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
//Store source location of image in textbox.
textBox1.Text = openImage.FileName;
//Retrive height and width of image and store in OriginalImageSize variable.
int imgWidth = pictureBox1.Image.Width;
int imgHeight = pictureBox1.Image.Height;
OriginalImageSize = new Size(imgWidth, imgHeight);
string imgSize = "Width " + imgWidth + " px Height " + imgHeight + " px";

//image width greater than 102 or height greater than 76then only Resize
if (pictureBox1.Image.Width > 102 || pictureBox1.Image.Height > 76)
{
Image scaledImage = ScaleByPercent(pictureBox1.Image, 10);
pictureBox1.Image = scaledImage; //Display that image in picture box.
//Retrive width and height of image.
imgSize = "Width " + scaledImage.Width + " px Height " + scaledImage.Height + " px";
pictureBox1.Image.Save("@path", ImageFormat.Jpeg);
}
else
{
imgSize = "Width " + pictureBox1.Image.Width + " px Height " + pictureBox1.Image.Height + " px";
}
pictureBox1.Image.Save("@path",ImageFormat.Jpeg);

}


}
 
Share this answer
 
Comments
Thanks7872 15-Jun-15 5:07am    
Don't think everytime that posting solution to your own solution help others. If you want to, you can post some Tip/Trick : Problem and quick solution to it. See here : http://www.codeproject.com/KB/FAQs/ArticleFAQ.aspx
MayankSemwal 16-Jun-15 1:55am    
sure..

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