簡體   English   中英

在 C# 中上傳圖像並在 wpf 中顯示消息框

[英]upload image in c# and show messagebox in wpf

我正在制作一個只能上傳位圖的程序。 如果用戶嘗試上傳任何其他擴展程序,它應該彈出一條錯誤消息。

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

如何更正此代碼以及在 if 語句中放入顯示消息框的正確參數是什么?

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM