locked
Sharing an image from my app RRS feed

  • Question

  • Hi,

    I am trying to share an image in my app using the share contract.

    The image is in an Image control which has been set to a WriteableBitmap. The WriteableBitmap has been generated by merging two images byte by byte.

    However I cannot understand how to convert this WriteableBitmap to a RandomAccessStreamReference that is required by the share contract.

    Please help!!

    thanks

    Thursday, December 13, 2012 10:51 AM

Answers

  • First point, did you saw this:

    Sharing content source app sample

    Sharing content target app sample

    In this sample:

    Sharing content source app sample


    you can see this:

        protected override bool GetShareContent(DataRequest request) 
            { 
                bool succeeded = false; 
     
                if (this.imageFile != null) 
                { 
                    DataPackage requestData = request.Data; 
                    requestData.Properties.Title = TitleInputBox.Text; 
                    requestData.Properties.Description = DescriptionInputBox.Text; // The description is optional. 
     
                    // It's recommended to use both SetBitmap and SetStorageItems for sharing a single image 
                    // since the target app may only support one or the other. 
     
                    List<IStorageItem> imageItems = new List<IStorageItem>(); 
                    imageItems.Add(this.imageFile); 
                    requestData.SetStorageItems(imageItems); 
     
                    RandomAccessStreamReference imageStreamRef = RandomAccessStreamReference.CreateFromFile(this.imageFile); 
                    requestData.Properties.Thumbnail = imageStreamRef; 
                    requestData.SetBitmap(imageStreamRef); 
                    succeeded = true; 
                } 
                else 
                { 
                    request.FailWithDisplayText("Select an image you would like to share and try again."); 
                } 
                return succeeded; 
            } 

    Where they use the IStorageItem what has the path file for the image.

    My suggest i, when you

    "The image is in an Image control which has been set to a WriteableBitmap"


    *if image is a file save in installation folder or local folder...  create a field that you can load that image to storage fil, and use when they what share information

    *if image is loaded from url, you can write the image in local folder or temp folder and save the storage file that is created.

    you can see this:

    Sharing a WriteableBitmap




    Sara Silva
    My blog | My Windows 8 Store Apps Samples
    Follow me in Twitter @saramgsilva

    My Windows 8 Store Apps: Female Pill | Galinho (Tic tac Toe) | 24
    My Windows Phone Apps

    (If my reply answers your question, please propose it as an answer because it will help other users)

    • Proposed as answer by Jesse Jiang Monday, December 17, 2012 9:02 AM
    • Marked as answer by rajeevcbhatia Tuesday, December 18, 2012 6:33 AM
    Thursday, December 13, 2012 1:17 PM

All replies

  • First point, did you saw this:

    Sharing content source app sample

    Sharing content target app sample

    In this sample:

    Sharing content source app sample


    you can see this:

        protected override bool GetShareContent(DataRequest request) 
            { 
                bool succeeded = false; 
     
                if (this.imageFile != null) 
                { 
                    DataPackage requestData = request.Data; 
                    requestData.Properties.Title = TitleInputBox.Text; 
                    requestData.Properties.Description = DescriptionInputBox.Text; // The description is optional. 
     
                    // It's recommended to use both SetBitmap and SetStorageItems for sharing a single image 
                    // since the target app may only support one or the other. 
     
                    List<IStorageItem> imageItems = new List<IStorageItem>(); 
                    imageItems.Add(this.imageFile); 
                    requestData.SetStorageItems(imageItems); 
     
                    RandomAccessStreamReference imageStreamRef = RandomAccessStreamReference.CreateFromFile(this.imageFile); 
                    requestData.Properties.Thumbnail = imageStreamRef; 
                    requestData.SetBitmap(imageStreamRef); 
                    succeeded = true; 
                } 
                else 
                { 
                    request.FailWithDisplayText("Select an image you would like to share and try again."); 
                } 
                return succeeded; 
            } 

    Where they use the IStorageItem what has the path file for the image.

    My suggest i, when you

    "The image is in an Image control which has been set to a WriteableBitmap"


    *if image is a file save in installation folder or local folder...  create a field that you can load that image to storage fil, and use when they what share information

    *if image is loaded from url, you can write the image in local folder or temp folder and save the storage file that is created.

    you can see this:

    Sharing a WriteableBitmap




    Sara Silva
    My blog | My Windows 8 Store Apps Samples
    Follow me in Twitter @saramgsilva

    My Windows 8 Store Apps: Female Pill | Galinho (Tic tac Toe) | 24
    My Windows Phone Apps

    (If my reply answers your question, please propose it as an answer because it will help other users)

    • Proposed as answer by Jesse Jiang Monday, December 17, 2012 9:02 AM
    • Marked as answer by rajeevcbhatia Tuesday, December 18, 2012 6:33 AM
    Thursday, December 13, 2012 1:17 PM
  • thanks for your help!!

    i finally managed to do it this way==>

    DataRequestDeferral deferral = args.Request.GetDeferral();

    WriteableBitmap wb = CommonVariables.clickedPhoto;
    StorageFile imageFile = await WriteableBitmapSaveExtensions.SaveToFile(wb);
    await imageFile.RenameAsync("xyz.png", NameCollisionOption.GenerateUniqueName);
    RandomAccessStreamReference imageStreamRef = RandomAccessStreamReference.CreateFromFile(imageFile);
    request.Data.Properties.Thumbnail = imageStreamRef;
    request.Data.SetBitmap(imageStreamRef);
    deferral.Complete();

    and the webcam on my machine is faulty...sometimes the dell webcam control also fails to load the camera

    Tuesday, December 18, 2012 6:33 AM