Cambiar el origen de la imagen en el código detrás - Wpf


Necesito establecer la fuente de la imagen dinámicamente, tenga en cuenta que mi imagen está en algún lugar de la red, aquí está mi código

BitmapImage logo = new BitmapImage();
logo.BeginInit();
logo.UriSource = new Uri(@"pack://application:,,,\\myserver\\folder1\\Customer Data\\sample.png");
logo.EndInit(); // Getting the exception here
ImageViewer1.Source = logo;

Excepción:

El prefijo URI no se reconoce

Author: Raz Luvaton, 2010-09-24

5 answers

Solo necesitas una línea:

ImageViewer1.Source = new BitmapImage(new Uri(@"\myserver\folder1\Customer Data\sample.png"));
 55
Author: timtos,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-01-08 19:37:30

Ninguna de las soluciones anteriores funcionó para mí. Pero esto lo hizo:

myImage.Source = new BitmapImage(new Uri(@"/Images/foo.png", UriKind.Relative));
 54
Author: Manindra Moharana,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-12-09 20:37:30

La sintaxis del paquete que está utilizando aquí es para una imagen que está contenida como un Recurso dentro de su aplicación, no para un archivo suelto en el sistema de archivos.

Simplemente desea pasar la ruta real a la UriSource:

logo.UriSource = new Uri(@"\\myserver\folder1\Customer Data\sample.png");
 5
Author: Wonko the Sane,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-09-24 12:59:40

Ninguno de los métodos funcionó para mí, ya que necesitaba extraer la imagen de una carpeta en lugar de agregarla a la aplicación. El siguiente código funcionó:

 TestImage.Source = GetImage("/Content/Images/test.png")

private static BitmapImage GetImage(string imageUri)
        {
            var bitmapImage = new BitmapImage();
            bitmapImage.BeginInit();
            bitmapImage.UriSource = new Uri("pack://siteoforigin:,,,/" + imageUri,             UriKind.RelativeOrAbsolute);
            bitmapImage.EndInit();
            return bitmapImage;
        } 
 2
Author: Harish Mitra,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-11-12 04:09:00

¡Están todos equivocados! ¿Por qué? Porque todo lo que necesitas es este código para funcionar:

(vista de imagen) / C# Img es: su cuadro de imagen

Mantenga esto como está, sin cambios ("ms-appx:///) este es el código no el nombre de su aplicación Imágenes es tu carpeta en tu proyecto puedes cambiarla. perro.png es su archivo en su carpeta, así como yo hago mi carpeta 'Imágenes' y archivo 'perro.png" Así que el uri es: "ms-appx: / / / Images / dog.png" y mi código :


private void Button_Click(object sender, RoutedEventArgs e)
    {
         img.Source = new BitmapImage(new Uri("ms-appx:///Images/dog.png"));
    }
 -1
Author: Ayman Sammoudi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-09-28 18:23:18