2016-09-28 61 views
0

保存的数据经过字符串后会显示错误给定的路径格式不受支持,我做错了什么或者我把url转换器代码放入错误的地方?错误System.NotSupportedException:不支持给定路径的格式

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

'ItemUrl'中有什么?我们看不到你的电脑:) – Pikoh

+0

这是一个文本框,你输入的URL和图片将被加载到pictureBox1 –

+1

但是引起异常的'ItemUrl.Text'值是什么? – slawekwin

回答

1

这是

 Image img = Image.FromFile(str); 

引起该问题。图像无法从Url加载,但必须是文件路径。

为了从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加载并同时加载d原稿和图像到画布上

+0

真的吗?原因之前,我改变了其他代码,这工作得很好,它加载时,直接链接url –

+0

有无论如何这种方法的工作?由像postimage.org –

+0

这样的网站转换后直接链接加载通过像C:\ Users \ Teronkee \ Pictures \ 98b288f6-e204-40fe-a4f3-cb83a21f4be3.jpg文件路径链接键入?似乎不能将它归档,我使用的方法只支持浏览功能 –

相关问题