Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update C# Share Target example to allow resourceMap images to show up in web view. #670

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

ahhlun
Copy link

@ahhlun ahhlun commented Jun 7, 2017

Update C# Share Target example to allow resourceMap images to show up in web view.
In RS2, Edge has a new feature 'Saved Tabs' that is sharing images out as resourceMap

await reader.LoadAsync((uint)stream.Size);
byte[] byteArray = new byte[stream.Size];
reader.ReadBytes(byteArray);
String base64String = "<img src ='data:image/gif;base64," + Convert.ToBase64String(byteArray) + "'";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should use stream.ContentType

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

foreach (KeyValuePair<string, RandomAccessStreamReference> item in this.sharedResourceMap)
{
var stream = await item.Value.OpenReadAsync();
if (!stream.ContentType.Contains("image/"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

StartsWith

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

byte[] byteArray = new byte[stream.Size];
reader.ReadBytes(byteArray);
String base64String = "<img src ='data:image/gif;base64," + Convert.ToBase64String(byteArray) + "'";
String replaceTarget = "<img src=" + item.Key;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could the key be quoted in the original HTML? Does this need to be case-sensitive? What if there are other attributes between the img and the src?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

String base64String = "<img src ='data:image/gif;base64," + Convert.ToBase64String(byteArray) + "'";
String replaceTarget = "<img src=" + item.Key;

htmlFragment = htmlFragment.Replace(replaceTarget, base64String);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be better to use a stream resolver? Then you won't run into trouble if the image is large and exceeds the maximum length of a data: URI.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haven't used a stream resolver before, hmm any pointers?
Going to have a look at the XamlWebView example

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not entirely sure that was what you want.

I also have an alternate solution to this if this wasn't what you wanted.

                    if (!String.IsNullOrEmpty(htmlFragment))
                    {
                        if (this.sharedResourceMap.Count > 0)
                        {
                            foreach (KeyValuePair<string, RandomAccessStreamReference> item in this.sharedResourceMap)
                            {
                                try
                                {
                                    using (var resourceStream = await item.Value.OpenReadAsync())
                                    {
                                        var tempFile = await folder.CreateFileAsync(Path.GetFileName(item.Key), CreationCollisionOption.OpenIfExists);
                                        using (var stream = await tempFile.OpenStreamForWriteAsync())
                                        {
                                            await resourceStream.AsStreamForRead().CopyToAsync(stream);
                                        }
                                        htmlFragment = htmlFragment.Replace(item.Key, tempFile.Name);
                                    }
                                }
                                catch (Exception ex)
                                {
                                    htmlFragment = htmlFragment.Replace(item.Key, @"ms-appx-web:///Assets/error.png");
                                }
                            }
                        }

                        AddContentValue("HTML: ");
                        ShareWebView.Visibility = Visibility.Visible;

                        var html = "<html><body>" + htmlFragment + "</body></html>";

                        var file = await folder.CreateFileAsync("html.html", CreationCollisionOption.OpenIfExists);
                        await FileIO.WriteTextAsync(file, html);

                        ShareWebView.Navigate(new Uri("ms-appdata:///temp/folder/html.html"));
                    }

@microsoft microsoft deleted a comment from msftclas Sep 26, 2017
@microsoft microsoft deleted a comment from msftclas Sep 26, 2017
{
try
{
Uri localUri = new Uri("ms-appdata:///temp/folder/" + URIPath);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor things

  1. rename URIPath to uriPath (camelCase for parameters)
  2. use var for localUri and StorageFile (type name is repeated)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants