7

Hey I have a windows phone 8.1 app using the silverlight API. I am downloading this image from my blob storage.

Image to be downloaded

The image is coming from a link like this: https://[service].blob.core.windows.net/[imagename].png and the image can be showned and downloaded in multiple browsers, just using the URI.

I now want to use this as a imagebrush based on the imageuri from the blobstorage:

// If we have a returned SAS.
                BitmapImage myOnlineImage = new BitmapImage();
                myOnlineImage.UriSource = new Uri(uploadImage.ImageUri, UriKind.RelativeOrAbsolute);
                //ImageOnlineTest.Source = myOnlineImage;
                var imageBrush = new ImageBrush
                {
                    ImageSource = myOnlineImage,
                    Stretch = Stretch.None
                };
                var source = FindChildShieldCanvas(CanvasImage, imageBrush);

                WriteableBitmap wbm = new WriteableBitmap((BitmapImage)myOnlineImage);
                ImageOnlineTest.Source = wbm;

The myOnlineImage is not created correctly, at least I cannot convert the image to a writeablebitmapimage (getting a null exception from the conversion), and in addition the imagebrush is empty, i.e. null. But as far as I know this is the way to do it?

So basicly

How do I create an imagebrush based on an url to a https site?

4

3 回答 3

4

我最终自己解决了这个问题:

记得添加using System.Runtime.InteropServices.WindowsRuntime;

// If we have a returned SAS.
                BitmapImage myOnlineImage = new BitmapImage();

                //myOnlineImage.UriSource = new Uri(uploadImage.ImageUri, UriKind.RelativeOrAbsolute);
                using (var webCLient = new Windows.Web.Http.HttpClient())
                {
                    webCLient.DefaultRequestHeaders.Add("User-Agent", "bot");
                    var responseStream = await webCLient.GetBufferAsync(new Uri(uploadImage.ImageUri, UriKind.RelativeOrAbsolute));
                    var memoryStream = new MemoryStream();//responseStream.ToArrayAsStream().ReadAsync());

                    memoryStream.Write(responseStream.ToArray(), 0, responseStream.ToArray().Length);
                    memoryStream.Position = 0;
                    myOnlineImage.SetSource(memoryStream);

                }
                //ImageOnlineTest.Source = myOnlineImage;
                var imageBrush = new ImageBrush
                {
                    ImageSource = myOnlineImage,
                    Stretch = Stretch.None
                };
                var source = FindChildShieldCanvas(CanvasImage, imageBrush);

                WriteableBitmap wbm = new WriteableBitmap((BitmapImage)myOnlineImage);

此代码适用于 imagebrush 和 writeablebitmap

于 2015-11-24T17:16:22.697 回答
3

要从图像创建位图,您必须在设置 URL 之前启动位图对象的初始化。

    BitmapImage myOnlineImage = new BitmapImage();
    myOnlineImage.BeginInit();
    myOnlineImage.UriSource = new Uri(uploadImage.ImageUri, UriKind.RelativeOrAbsolute);
    myOnlineImage.EndInit();

    var imageBrush = new ImageBrush
    {
        ImageSource = myOnlineImage,
        Stretch = Stretch.None
    };
    var source = FindChildShieldCanvas(CanvasImage, imageBrush);

    WriteableBitmap wbm = new WriteableBitmap((BitmapImage)myOnlineImage);
    ImageOnlineTest.Source = wbm;
于 2015-11-24T15:49:41.540 回答
1

您可以通过以下方式使用 C# 执行此操作,将相应的代码替换为您的字段。

Rectangle rectangle = new Rectangle();
rectangle.StrokeThickness = 10;
rectangle.Height = 200;
rectangle.Width = 100;
rectangle.SetValue(Canvas.LeftProperty, 100d);
rectangle.SetValue(Canvas.TopProperty, 100d);
rectangle.Fill = new ImageBrush(new BitmapImage(new Uri(@"C:\User\xiaorui.dong\Pictures\profile.jpeg")));
于 2015-12-01T11:27:52.027 回答