简体   繁体   中英

upload image in c# and show messagebox in wpf

I'm making a program which will upload only bitmaps . If the user tries to upload anything other extension it should popup an error message.

OpenFileDialog op = new OpenFileDialog();
op.Title = "Open Image";
op.Filter = "bmp files (*.bmp)|*.bmp";
if (op.ShowDialog() == true)
{
    image.Source = new BitmapImage(new Uri(op.FileName));
}
if ( op.ShowDialog() !== FilterEventArgs)
{
    MessageBox.Show ( your path doesn't bmp ); 
}

How can I correct this code and what is the right parameter to put in if statement to show the messagebox?

 Image image = new Image();
        OpenFileDialog op = new OpenFileDialog();
        op.Title = "Open Image";
        op.Filter = "bmp files (*.bmp)|*.bmp";

        bool bNotBmp = true;
        while (op.ShowDialog() == true && bNotBmp == true)
        {
           FileInfo FileInf = new FileInfo(op.FileName);
           string ImgExtension = FileInf.Extension;
           if (FileInf.Extension.ToString().ToLower() != ".bmp")
           {
               MessageBox.Show("Please upload only bmp file");
           }
           else
           {
               bNotBmp = false;
           }

        }

        MessageBox.Show("Write image or operation cancelled.");
OpenFileDialog op = new OpenFileDialog();
op.Title = "Open Image";
op.Filter = "bmp files (*.bmp)|*.bmp";

var result = op.ShowDialog();
if (result == DialogResult.OK)
{
    if(System.IO.Path.GetExtension(op.FileName).ToLower() == ".bmp"){
        image.Source = new BitmapImage(new Uri(op.FileName));
    }
    else{
        MessageBox.Show ("The file must have a .bmp extension"); 
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM