繁体   English   中英

错误System.NotSupportedException:不支持给定路径的格式

[英]Error System.NotSupportedException : The given path's format is not supported

保存数据后,它通过字符串显示错误。不支持给定的路径格式,我做错了还是我把url转换器代码放在错误的地方?

try
{
     connect.Open();
     OleDbCommand command = new OleDbCommand();
     command.Connection = connect;
     command.CommandText = string.Format("insert into RegisterItem([Name], [Url],[Description], [Price]) values('{0}','{1}','{2}','{3}')", 
                                      ItemName.Text, 
                                      ItemUrl.Text, 
                                      ItemDescription.Text, 
                                      ItemPrice.Text);

     command.ExecuteNonQuery();
     MessageBox.Show("Data Saved");

     txtID1.Text = txtUsername.Text;
     txtName1.Text = ItemName.Text;
     txtDescription1.Text = ItemDescription.Text;
     txtPrice1.Text = ItemPrice.Text;
     ItemName.Text = "";
     ItemDescription.Text = "";
     ItemPrice.Text = "";
     string str = ItemUrl.Text;
     pictureBox1.ImageLocation = str;
     string str1 = textBox1.Text;
     Image img = Image.FromFile(str);
     pictureBox1.Image = img;
}
catch (Exception ex)
{
     MessageBox.Show("Error " + ex);
}
finally
{
     if (Connect != null) { connect.Close(); }
}

它是

        Image img = Image.FromFile(str);

导致问题。 无法从网址加载图片,但图片必须是文件路径。

为了从url加载图像,您必须

        WebRequest wr = WebRequest.Create("https://pixabay.com/static/uploads/photo/2015/10/01/21/39/background-image-967820_960_720.jpg");
        Image img;
        using (var rs = wr.GetResponse().GetResponseStream())
        {
            img = Image.FromStream(rs);
        }

实际上,您可以完全省略

 Image img = Image.FromFile(str);
 pictureBox1.Image = img;

这部分代码完全可以完成所有工作

 pictureBox1.ImageLocation = str;

Image.FromFile仅从磁盘文件加载,但设置ImageLocation可以从url加载,同时将图像绘制到画布上

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM