首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在我的WPF应用程序中保存图像

如何在我的WPF应用程序中保存图像
EN

Stack Overflow用户
提问于 2012-12-31 17:33:58
回答 1查看 8.3K关注 0票数 0

在我的WPF应用程序中,我无法将图像保存在我的应用程序的snap文件夹中。下面是我使用的代码。

代码语言:javascript
复制
OpenFileDialog ofd = new OpenFileDialog();
        ofd.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp"; 
        if (ofd.ShowDialog() == DialogResult.OK)
        {

            string filepath = ofd.FileName;
            File.Copy(ofd.FileName, Application.StartupPath + "\\snaps\\" + ofd.SafeFileName,true);
            photoTextBox.Text= ofd.SafeFileName;
            pictureBox1.Image = Image.FromFile(ofd.FileName);

        }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-01-09 19:27:11

用于打开文件浏览器的代码

代码语言:javascript
复制
 string filepath;
        //browse Button
        private void button4_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            open.Multiselect = false;    
            open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
            bool? result = open.ShowDialog();

            if (result == true)
            {
                filepath = open.FileName; // Stores Original Path in Textbox    
                ImageSource imgsource = new BitmapImage(new Uri(filepath)); // Just show The File In Image when we browse It
                Clientimg.Source = imgsource;  
            }
        }

下面是用于保存文件的代码

代码语言:javascript
复制
private static String GetDestinationPath(string filename, string foldername)
        {
            String appStartPath = System.IO.Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);

            appStartPath = String.Format(appStartPath + "\\{0}\\" + filename, foldername);
            return appStartPath;
        }

如何使用它

代码语言:javascript
复制
string name = System.IO.Path.GetFileName(filepath);
string destinationPath = GetDestinationPath(name,"YourFolderName");

File.Copy(filepath, destinationPath, true);
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14099869

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档