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

时间:2016-09-28 07:18:04

标签: c# .net oledb

保存数据后,它通过字符串显示错误不支持给定的路径格式,我做错了什么或者我将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(); }
}

1 个答案:

答案 0 :(得分:1)

        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加载,同时绘制和图像到画布