参数异常未处理,路径不是合法形式 [英] Argument exception was unhandled , path is not in legal form

查看:79
本文介绍了参数异常未处理,路径不是合法形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            open.Filter = "Image Files(*.jpeg;*.bmp;*.png;*.jpg)|*.jpeg;*.bmp;*.png;*.jpg";
            if (open.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = open.FileName;
            }
           
            string image = textBox4.Text;
            Bitmap bmp = new Bitmap(image);
            FileStream fs = new FileStream(image, FileMode.Open, FileAccess.Read);
            byte[] bimage = new byte[fs.Length];
            fs.Read(bimage, 0, Convert.ToInt32(fs.Length));
            fs.Close();
            
        }

推荐答案





如果你看一下 OpenFileDialog类 [ ^ ]你会看到你应该为 FileDialog.InitialDirectory Property [ ^ ]。



在你的例子中:

Hi,

If you have a look at the documentation for the OpenFileDialog Class[^] you'll see that you should be setting a value for the FileDialog.InitialDirectory Property [^].

In your example:
private void button1_Click(object sender, EventArgs e)
        {
            string AppPath = Directory.GetCurrentDirectory();
            OpenFileDialog open = new OpenFileDialog();
            open.InitialDirectory = "c:\\" ;
            open.Filter = "Image Files(*.jpeg;*.bmp;*.png;*.jpg)|*.jpeg;*.bmp;*.png;*.jpg";
            if (open.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = open.FileName;
                System.IO.File.Copy(open.FileName, Path.Combine(AppPath, Path.GetFileName(open.FileName)), true);
            }
        }





[edit]

添加了如何获取的基本示例使用 File.Copy方法将所选文件复制到应用程序文件夹中[ ^ ]

[/ edit]



[edit]
Added basic example of how to copy selected file into application folder using File.Copy Method[^]
[/edit]


如果用户选择确定,则将所选路径存储在 textBox1 中。然后,您尝试根据 textBox4 的值打开文件,这几乎肯定不包含有效的文件路径。



如果用户选择取消,你仍然试图根据 textBox4 的值打开文件。



更改代码以使用正确的路径,并且只有在用户选择确定时才处理路径:

If the user selects "OK", you're storing the selected path in textBox1. You're then trying to open the file based on the value of textBox4, which almost certainly doesn't contain a valid file path.

If the user selects "Cancel", you're still trying to open the file based on the value of textBox4.

Change your code to use the correct path, and only process the path if the user selects "OK":
private void button1_Click(object sender, EventArgs e)
{
    using (OpenFileDialog open = new OpenFileDialog())
    {
        open.Filter = "Image Files(*.jpeg;*.bmp;*.png;*.jpg)|*.jpeg;*.bmp;*.png;*.jpg";
        if (open.ShowDialog() == DialogResult.OK)
        {
            textBox1.Text = open.FileName;
            
            // TODO: Do something with these...
            Bitmap bitmap = new Bitmap(open.FileName);
            byte[] bimage = File.ReadAllBytes(open.FileName);
        }
    }
}





此外,帮自己一个忙,给你的控件有意义的名字。您可能还记得 textBox1 是图像路径, textBox42 是frood-name now ,但是当你几周后回到你的代码时,你会忘记这些控件所代表的内容。



Also, do yourself a favour and give your controls meaningful names. You might remember that textBox1 is the image path and textBox42 is the frood-name now, but when you come back to your code in a few weeks' time, you'll have forgotten what these controls represent.


这根据我的需要工作得很好。张贴其他人可能会得到帮助

private void button1_Click(对象发件人,EventArgs e)

{

openFileDialog1.Filter =jpg文件( * .jpg)| * .jpg |所有文件(*。*)| *。*;





OpenFileDialog openImage = new OpenFileDialog();

DialogResult result = openImage.ShowDialog();

if(result == DialogResult.OK)

{

//在图片框中加载图片并正常设置图片的大小模式属性。

pictureBox1.Load(openImage.FileName);

pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;

//在文本框中存储图像的源位置。

textBox1.Text = openImage.FileName;

// Retrive图像的高度和宽度,并存储在OriginalImageSize变量中。

int imgWidth = pic tureBox1.Image.Width;

int imgHeight = pictureBox1.Image.Height;

OriginalImageSize = new Size(imgWidth,imgHeight);

string imgSize =宽度+ imgWidth +px高度+ imgHeight +px;



//图像宽度大于102或高度大于76然后仅调整大小

if(pictureBox1.Image.Width> 102 || pictureBox1.Image.Height> 76)

{

Image scaledImage = ScaleByPercent(pictureBox1.Image,10);

pictureBox1.Image = scaledImage; //在图片框中显示该图像。

//重复图像的宽度和高度。

imgSize =宽度+ scaledImage.Width +px高度+ scaledImage .Height +px;

pictureBox1.Image.Save(@ path,ImageFormat.Jpeg);

}

else

{

imgSize =Width+ pictureBox1.Image.Width +px Height+ pictureBox1.Image.Height +px;

}

pictureBox1.Image.Save(@ path,ImageFormat.Jpeg);



}





}
THIS WORKED FINE AS PER MY NEEDS. POSTING IT SO OTHERS MIGHT GET HELP
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "jpg files (*.jpg)|*.jpg|All files (*.*)|*.*";


OpenFileDialog openImage = new OpenFileDialog();
DialogResult result = openImage.ShowDialog();
if (result == DialogResult.OK)
{
//Load image in picture box and set size mode property of image as normal.
pictureBox1.Load(openImage.FileName);
pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
//Store source location of image in textbox.
textBox1.Text = openImage.FileName;
//Retrive height and width of image and store in OriginalImageSize variable.
int imgWidth = pictureBox1.Image.Width;
int imgHeight = pictureBox1.Image.Height;
OriginalImageSize = new Size(imgWidth, imgHeight);
string imgSize = "Width " + imgWidth + " px Height " + imgHeight + " px";

//image width greater than 102 or height greater than 76then only Resize
if (pictureBox1.Image.Width > 102 || pictureBox1.Image.Height > 76)
{
Image scaledImage = ScaleByPercent(pictureBox1.Image, 10);
pictureBox1.Image = scaledImage; //Display that image in picture box.
//Retrive width and height of image.
imgSize = "Width " + scaledImage.Width + " px Height " + scaledImage.Height + " px";
pictureBox1.Image.Save("@path", ImageFormat.Jpeg);
}
else
{
imgSize = "Width " + pictureBox1.Image.Width + " px Height " + pictureBox1.Image.Height + " px";
}
pictureBox1.Image.Save("@path",ImageFormat.Jpeg);

}


}


这篇关于参数异常未处理,路径不是合法形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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