简体   繁体   English

从url创建imagebrush,bitmap和writeablebitmap

[英]Create imagebrush, bitmap and writeablebitmap from an url

Hey I have a windows phone 8.1 app using the silverlight API. 嘿,我有一个使用silverlight API的Windows Phone 8.1应用程序。 I am downloading this image from my blob storage. 我从blob存储中下载此图像。

要下载的图像

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. 图像来自以下链接: https:// [service] .blob.core.windows.net / [imagename] .png ,只需使用URI即可在多个浏览器中显示和下载图像。

I now want to use this as a imagebrush based on the imageuri from the blobstorage: 我现在想将它用作基于blobstorage的imageuri的图像画笔:

// 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, ie null. myOnlineImage未正确创建,至少我无法将图像转换为writeablebitmapimage(从转换中获取null异常),此外,图像刷是空的,即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? 如何基于https站点的URL创建imagebrush

I ended up solving the issue myself: 我自己最终解决了这个问题:

Remember to add using System.Runtime.InteropServices.WindowsRuntime; 记得using System.Runtime.InteropServices.WindowsRuntime;添加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);

This code works, both for the imagebrush and writeablebitmap 此代码适用于imagebrush和writeablebitmap

To create a bitmap from an image you have to start the initialization of the bitmap object prior to setting the URL. 要从图像创建位图,您必须在设置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;

You can do it with C# in the following way, replace the appropriate code with your fields. 您可以通过以下方式使用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")));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM