Click here to Skip to main content
16,019,614 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how to check image property. i want to inert only image but when i change extension of any exe file or any txt into jpeg after that when i insert it, it is inserted but want to stop like this.
same in case of video...
please any 1 help me....
thanks....
Posted
Comments
OriginalGriff 9-Jul-14 1:55am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
Use the "Improve question" widget to edit your question and provide better information.
krrazyumesh 9-Jul-14 2:18am    
code for how to upload only jpeg image using C#. if any one try to change the extension of txt file or exe file into jpeg and upload it, then it will throw an exception "error"
Sergey Alexandrovich Kryukov 9-Jul-14 2:01am    
Not clear. What would this "check" mean, why?
—SA
krrazyumesh 9-Jul-14 2:18am    
check means whether selected image is only jpeg


code for how to upload only jpeg image using C#. if any one try to change the extension of txt file or exe file into jpeg and upload it, then it will throw an exception "error"
syed shanu 9-Jul-14 2:25am    
Is this Windows or Web Application.here insert means do you store images to your database if so what data type you used.Or do you upload the images to your folder.Clarify thia and improve your question.

krrazyumesh wrote:
if any one try to change the extension of txt file or exe file into jpeg and upload it, then it will throw an exception "error"
I would rely on file name. If you got this exception, just show it to the user.

The possible user's action of renaming *.txt to *.jpg would be stupid enough to dub it "exceptional". It does not mean you should not be prepared to it. You should ultimately handle all exceptions, but this one should not be a special case. Handle it with all other exceptions at once.

—SA
 
Share this answer
 
Hi,
I have made a simple Work out for you.

Place a Button and Pictuerbox on you winform.button1,pictureBox1

In Button click open the jpg file and load it in pictuer box. I have used try catch if the selected file is valid jpeg then display in pictuer box .if other file like texct or exe file extention change as jpg and upload display the error message from exceptaiton part.
see the buttonclikc code here for details.
C#
private void button1_Click(object sender, EventArgs e)
      {
          string filename = "";
          OpenFileDialog open = new OpenFileDialog() { Filter = "Image Files|*.JPG;*.JPEG;" };
          if (open.ShowDialog() == DialogResult.OK)
          {
              filename = open.FileName;
          }
          if (filename != "")
          {
              string image = filename;
              try
              {
                  Bitmap bmp = new Bitmap(image);


              pictureBox1.Image = bmp;//add this line
              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();
              byte[] Photo = bimage;
                  }
               catch (Exception ex)
              {
                  MessageBox.Show("Select the valid file");
              }

          }
      }
 
Share this answer
 

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