Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create imagebrush, bitmap and writeablebitmap from an url

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?

like image 615
JTIM Avatar asked Nov 22 '15 12:11

JTIM


People also ask

How to use software bitmap in imagebrush?

You can also use SoftwareBitmapSource to set a SoftwareBitmap as the ImageSource for an ImageBrush. You can create a SoftwareBitmap from an existing WriteableBitmap by calling SoftwareBitmap.CreateCopyFromBuffer and supplying the PixelBuffer property of the WriteableBitmap to set the pixel data.

How to get a bitmap from URL in Android app?

This example demonstrates how do I get a bitmap from Url in android app. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml. Step 3 – Copy and paste an image (.png/.jpg/.jpeg) into res/drawable

How do I write an image to a bitmap encoder?

Call FlushAsync to cause the encoder to write the image data to the specified file. You can specify additional encoding options when you create the BitmapEncoder by creating a new BitmapPropertySet object and populating it with one or more BitmapTypedValue objects representing the encoder settings.

How do I create a softwarebitmap from an existing writeablebitmap?

You can create a SoftwareBitmap from an existing WriteableBitmap by calling SoftwareBitmap.CreateCopyFromBuffer and supplying the PixelBuffer property of the WriteableBitmap to set the pixel data. The second argument allows you to request a pixel format for the newly created WriteableBitmap.


2 Answers

I ended up solving the issue myself:

Remember to add 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

like image 85
JTIM Avatar answered Oct 19 '22 05:10

JTIM


To create a bitmap from an image you have to start the initialization of the bitmap object prior to setting the 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;
like image 3
Brian from state farm Avatar answered Oct 19 '22 04:10

Brian from state farm