Reputation: 23
After data saved which is it went through the string it shows an error The given path format is not supported, Did I do something wrong or I placed the url
converter codes in the wrong place?
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(); }
}
Upvotes: 0
Views: 1098
Reputation: 228
It is the
Image img = Image.FromFile(str);
that causes the problem. Image can not be loaded from Url, but must be file path.
In order to load image from url you have to
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);
}
Actually, you can completely omit
Image img = Image.FromFile(str);
pictureBox1.Image = img;
This part of code exactly does all the job
pictureBox1.ImageLocation = str;
Image.FromFile loads only from disk file, but setting ImageLocation can load from url and at the same time draws and image to the canvas
Upvotes: 1