How to format a URI when binding an image in Silverlight?

I could not find the answer to this question.

I have a database in which there are paths to it ("images / myimage.jpg"). These images exist on my asp.net site, where I also host SL. I want to bind these images to a ListBox control so that the image is displayed.

I read that since I have a string value, "images / myimage.jpg", I need to convert it to a BitMap image. I have done this:

xaml:

 <Image Source="{Binding ImageFile, Converter={StaticResource ImageConverter}}"/>

ImageConverter Class:

    public object Convert(object value, Type targetType,
                                  object parameter, CultureInfo culture)
            {
                try
                {
                    Uri source= new Uri(value.ToString());
                    return new BitmapImage(source);
                }
                catch(Exception ex)
                {
                    return new BitmapImage();
                }
            }

I get an error when creating the URI, "The format of the URI cannot be determined." What am I doing wrong? If I create a Uri that looks like this: http: // localhost: 49723 / images / myimage.jpg , it works fine.

"images/myimage.jpg" ?

+3
5

Silverlight , (), WPF. XAP, .

XAP , .

URI Silverlight .

+3

, , XAP, .

//Get the root path for the XAP
string src = Application.Current.Host.Source.ToString();

//Get the application root, where 'ClientBin' is the known dir where the XAP is
string appRoot = src.Substring(0,src.IndexOf("ClientBin")); 

//Create the image / uri
BitmapImage img = new BitmapImage();
img.UriSource = new Uri(appRoot + "/Images/myImage.png", UriKind.Relative);

?

+5

, ( , . .) , , :

Uri source = new Uri("Path/Image.jpg", UriKind.Relative);

, XAP, . , Fiddler Web Dev Helper .

+1

http://www.silverlightexamples.net/post/How-to-Get-Files-From-Resources-in-Silverlight-20.aspx

, "SLapplicationName; component/mypathtoimage/image.png"

using System.Windows.Resources;      // StreamResourceInfo
using System.Windows.Media.Imaging;  // BitmapImage
....

StreamResourceInfo sr = Application.GetResourceStream(
    new Uri("SilverlightApplication1;component/MyImage.png", UriKind.Relative));
BitmapImage bmp = new BitmapImage();
bmp.SetSource(sr.Stream); 
+1

You can simply write a method that gives the full server address (protocol: // server: port /) and use it to create absolute URLs:

public class Helper{
    public static string ServerAddress{
        get{
            if (_server == "")
                _server = _ServerAddress();
            return _server;
        }
    }

     private static string _ServerAddress(){
        HttpContext ctx = HttpContext.Current;
        if (ctx == null) return "";

        HttpRequest request = ctx.Request;
        if (request == null) return "";

      string srvr = request.ServerVariables["SERVER_NAME"];
      string port = string.IsNullOrEmpty(request.ServerVariables["SERVER_PORT"])?
            "" : ":" + request.ServerVariables["SERVER_PORT"];
      string protocol = "http://";//request.ServerVariables["SERVER_PROTOCOL"];
      return string.Format("{0}{1}{2}{3}", protocol, srvr, port, 
                   request.ApplicationPath);
    }
}    

and change your line of the converter method:

Uri source= new Uri(value.ToString());

to

if(!string.IsNullOrEmpty(value.ToString()))
   Uri source= new Uri(Helper.WebAddress + value.ToString());
0
source

Source: https://habr.com/ru/post/1714358/


All Articles