2012-10-28 55 views
2

我试图将图像导入到c#中的“图像”框中。将图像导入图像框

在XAML做的时候,我发现它只是

source = "file location" 

但是当我尝试在C#下面的代码

myimage.source = "image.png" 

我的形象是“图像”的名称框,它只是返回此错误:

Error: Cannot implicitly convert type 'string' to 'System.Windows.Media.ImageSource' 

寻找外核层时大家似乎只是用图片框引用,但我没有访问图片框,如果我没有,并指出节目“myimage”正在显示一个实时视频流。所以,我认为需要一个图像框。

回答

0

检查此link了。

在C#这将是:

Image myImage = new Image(); 
myImage.Source = new BitmapImage(new Uri("myPicture.jpg", UriKind.RelativeOrAbsolute)); 
+0

谢谢,虽然没有图像显示在“myImage”,但仍然删除了错误仍是由于某种原因。我尝试过使用.png和.jpg图片。 – difurious

+0

您应该使用Image对象。 如果你想把它放在图片框中,然后将它放在那里,myImage对象将不会显示,因为它只是一个处理图像的类。 – Lior

2
picBox.Image = Image.FromFile("image.png"); 

你可能要创建一个新的URI指向实际图像

1

使用openFileDialog1对象试试这个代码: 双击你的图片框和写:

private void imagePictureBox_Click(object sender, EventArgs e) 
    { 
     if (openFileDialog1.ShowDialog() == DialogResult.OK) 
     { 
      imagePictureBox.ImageLocation = openFileDialog1.FileName; 
     } 
    } 

如果您的图像是在'图像折叠呃'写入你的应用程序文件夹,只需写:

imagePictureBox.ImageLocation = "images/YourImage.png";