wpf 上传图片

using System;
using System.IO;

namespace puruite.Helps
{
    class Upload
    {
        public static string UploadImage(string fileName, Stream fileData)
        {
            // 获取file后缀
            string ext = Path.GetExtension(fileName);
            // 随机文件名
            string newFileName = DateTime.Now.ToFileTime().ToString() + FunctionHelper.RandStr(10);
            string currentPath = AppDomain.CurrentDomain.BaseDirectory;
            string savePath =  "/uploads";
            string fullSavePath = currentPath + savePath;
            if (!Directory.Exists(fullSavePath))
            {
                Directory.CreateDirectory(fullSavePath);
            }
            string fullPath = fullSavePath + "/" + newFileName + ext;
            string returnPath = savePath +"/" + newFileName + ext;
            BinaryReader bReader = new BinaryReader(fileData);
            FileStream fStream = new FileStream(fullPath, FileMode.CreateNew);
            BinaryWriter bWriter = new BinaryWriter(fStream);
            bWriter.Write(bReader.ReadBytes((int)fileData.Length));
            bWriter.Close();
            fStream.Close();
            return returnPath;
        }
    }
}

 

使用

OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.Multiselect = false;
            fileDialog.Filter = "图片|*.jpg;*.png;*.gif;*.bmp;*.jpeg";
            Nullable<bool> isShow = fileDialog.ShowDialog();
            if(isShow == true)
            {
                string fileName = fileDialog.FileName;
                BitmapImage image = new BitmapImage(new Uri(fileName));
                this.logoImage.Source = image;
                string newFile = Upload.UploadImage(fileName, fileDialog.OpenFile());
                this.logoTxt.Text = newFile;
            }

显示

 string logo = setting["logo"].ToString();
            this.logoTxt.Text = logo;
            if (logo !=  "")
            {
                string logoPath = AppDomain.CurrentDomain.BaseDirectory + logo;
                this.logoImage.Source = new BitmapImage(new Uri(logoPath, UriKind.RelativeOrAbsolute));
            }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值