如何保存图片在我的WPF应用程序 [英] How To Save Image in My WPF Application

查看:160
本文介绍了如何保存图片在我的WPF应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的WPF应用程序我不能够救我的卡夹的应用程序中的图像。 。下面是我使用的代码



 打开文件对话框OFD =新的OpenFileDialog(); 
ofd.Filter =图像文件(* JPG; * .JPEG,* .gif和* .BMP)| * .JPG * .JPEG,* .gif和* .BMP;
如果(ofd.ShowDialog()== DialogResult.OK)
{

字符串的文件路径= ofd.FileName;
File.Copy(ofd.FileName,Application.StartupPath +\\snaps\\+ ofd.SafeFileName,真正的);
photoTextBox.Text = ofd.SafeFileName;
pictureBox1.Image = Image.FromFile(ofd.FileName);

}


解决方案

代码打开文件浏览器

 字符串文件路径; 
//浏览按钮
私人无效button4_Click(对象发件人,RoutedEventArgs E)
{
打开文件对话框打开=新的OpenFileDialog();
open.Multiselect = FALSE;
open.Filter =图像文件(* JPG; * .JPEG,* .gif和* .BMP)| * .JPG * .JPEG,* .gif和* .BMP;
布尔?结果= open.ShowDialog();

如果(结果==真)
{
=文件路径open.FileName; //商店在文本框
的ImageSource imgsource原始路径=新的BitmapImage(新的URI(文件路径)); //只显示文件图像,当我们浏览它
Clientimg.Source = imgsource;
}
}

和以下是用于保存文件<代码/ p>

 私有静态字符串GetDestinationPath(字符串的文件名,文件夹名字符串)
{
字符串appStartPath = System.IO。 Path.GetDirectoryName(Process.GetCurrentProcess()MainModule.FileName);

appStartPath =的String.Format(appStartPath +\\ {0} \\+文件名,文件夹名);
返回appStartPath;
}



如何使用它

 字符串名称= System.IO.Path.GetFileName(文件路径); 
字符串=的DestinationPath GetDestinationPath(姓名,YourFolderName);

File.Copy(文件路径,的DestinationPath,真正的);


In My WPF application i am not able to save image inside my application in snap folder. Below is the code i am using.

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

        }

解决方案

Code to open the file browser

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

And below is the code used for saving the file

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

How to use it

string name = System.IO.Path.GetFileName(filepath);
string destinationPath = GetDestinationPath(name,"YourFolderName");

File.Copy(filepath, destinationPath, true);

这篇关于如何保存图片在我的WPF应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆