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
base: main
Are you sure you want to change the base?
Conversation
Edge starting from RS2 is sharing images in a 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) + "'"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should use stream.ContentType
There was a problem hiding this comment.
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/")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
StartsWith
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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"));
}
{ | ||
try | ||
{ | ||
Uri localUri = new Uri("ms-appdata:///temp/folder/" + URIPath); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor things
- rename URIPath to uriPath (camelCase for parameters)
- use var for localUri and StorageFile (type name is repeated)
2239d7f
to
9bf6501
Compare
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