2016-09-28 489 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文件路徑鏈接鍵入?似乎不能將它歸檔,我使用的方法只支持瀏覽功能 –

相關問題