Capture screenshot of current web site in Universal Apps - c#

Here is how I can capture screen shot of current webpage, but sometimes it is capturing only visible area of webview (what user actually see).
But I'm not sure what was wrong.
I think var heightString = await WebView.InvokeScriptAsync("eval", new[] { "document.body.scrollHeight.toString()" }) was finish too late - but program should wait in if (!int.TryParse(heightString, out height)) to complete... Am I wrong?
private async Task CaptureWebView()
{
int width;
int height;
var originalWidth = WebView.Width;
var originalHeight = WebView.Height;
// ask the content its width
var widthString = await WebView.InvokeScriptAsync("eval", new[] { "document.body.scrollWidth.toString()" });
// ask the content its height
var heightString = await WebView.InvokeScriptAsync("eval", new[] { "document.body.scrollHeight.toString()" });
if (!int.TryParse(widthString, out width))
{
throw new Exception("Unable to get page width");
}
if (!int.TryParse(heightString, out height))
{
throw new Exception("Unable to get page height");
}
// resize the webview to the content
WebView.Width = width;
WebView.Height = height;
await DoCapture("captured.png");
WebView.Width = originalWidth;
WebView.Height = originalHeight;
Painter.Width = width;
Painter.Height = height;
var i = await LoadCaptured("captured.png");
Painter.Fill = i;
}
Test repository on Github

At the end problem itself was in WebView.CapturePreviewToStreamAsync() method. I used it to capture full web screen shot, but it didn't work very well. But then I found WebViewBrush and realized that is the right way.
So here is final code snippet or GitHub
private async Task CaptureWebView()
{
int width;
int height;
var originalWidth = WebView.ActualWidth;
var originalHeight = WebView.ActualHeight;
var widthString = await WebView.InvokeScriptAsync("eval", new[] { "document.body.scrollWidth.toString()" });
var heightString = await WebView.InvokeScriptAsync("eval", new[] { "document.body.scrollHeight.toString()" });
if (!int.TryParse(widthString, out width))
{
throw new Exception("Unable to get page width");
}
if (!int.TryParse(heightString, out height))
{
throw new Exception("Unable to get page height");
}
// resize the webview to the content
WebView.Width = width;
WebView.Height = height;
var brush = new WebViewBrush();
brush.SetSource(WebView);
Painter.Width = width;
Painter.Height = height;
Painter.Fill = brush;
}

Related

Problem with loading image from bytes array

I encountered strange behaviour when loading image from byte array aquired from stream. Most images are correct, I would say 99% of them. But i saw so far two times something like this below. Image in the middle is shown like a set of random pixels, not the real image.
Image can be loaded correctly in other client application (sliverlight) but i can't show that.
In WPF client i have something like that:
Does someone had issue like this? Or any idea what might cause it? Maybe i should look on server side (image is show in other, older client and code to load that is known to me).
Code looks like that:
public async Task<ImageCreatePictureBox> CreatePictureBox(IBaseObj pmIBaseObj, IObj pmObj)
{
var lcContainer = new Grid
{
Width = pmObj.PresObj.Width,
Height = pmObj.PresObj.Height
};
var gridBorder = new Border();
CustomImg imageControl = null;
FrameworkElement lcFrameworkElement = lcContainer;
if (!(pmIBaseObj is ImageBaseObj lcImageBaseObj))
return new ImageCreatePictureBox(null, lcContainer, lcFrameworkElement, pmObj);
if (!lcImageBaseObj.CustomBitMap)
{
try
{
var bitmapImage = BitmapImageHelper.ByteArrayToBitmapSource(lcImageBaseObj.Image, lcImageBaseObj.ImageWidth, lcImageBaseObj.ImageHeight);
imageControl = new CustomImg(pmIBaseObj.Width, pmIBaseObj.Height, pmObj)
{
Source = bitmapImage,
Height = lcImageBaseObj.Height,
Width = lcImageBaseObj.Width
};
}
catch (Exception e)
{
var src = new BitmapImage(new Uri(Helper.GetPathToImage("dummy")));
imageControl = new CustomImg(pmIBaseObj.Width, pmIBaseObj.Height, pmObj)
{
Source = src,
Height = lcImageBaseObj.Height,
Width = lcImageBaseObj.Width
};
Helper.WriteToDebug(e);
}
}
else
{
try
{
var bitmapImage = BitmapImageHelper.ByteArrayToBitmapSource(lcImageBaseObj.Image, lcImageBaseObj.ImageWidth, lcImageBaseObj.ImageHeight);
imageControl = new CustomImg(pmIBaseObj.Width, pmIBaseObj.Height, pmObj)
{
Source = bitmapImage,
Height = lcImageBaseObj.Height,
Width = lcImageBaseObj.Width
};
}
catch (Exception e)
{
if (imageControl == null)
imageControl = new CustomImg(pmIBaseObj.Width, pmIBaseObj.Height, pmObj);
var src = new BitmapImage(new Uri(Helper.GetPathToImage("dummy")));
imageControl = new CustomImg(pmIBaseObj.Width, pmIBaseObj.Height, pmObj)
{
Source = src,
Height = lcImageBaseObj.Height,
Width = lcImageBaseObj.Width
};
Helper.WriteToDebug(e);
}
}
(...)
return new ImageCreatePictureBox(imageControl, lcContainer, lcFrameworkElement, pmObj);
}
public static BitmapSource ByteArrayToBitmapSource(Byte[] BArray, int imgWidth, int imgHeight)
{
try
{
var width = imgWidth;
var height = imgHeight;
var dpiX = 90d;
var dpiY = 90d;
var pixelFormat = PixelFormats.Pbgra32;
var bytesPerPixel = (pixelFormat.BitsPerPixel + 7) / 8;
var stride = bytesPerPixel * width;
var bitmap = BitmapSource.Create(width, height, dpiX, dpiY, pixelFormat, null, BArray, stride);
return bitmap;
}
catch (Exception e)
{
return BytesToBitmapImage(BArray);
}
}

UWP WebView Printing shows the printed page blurred

There are many complaints online (such as this one and this one) about the printing of a webpage in WebVeiw where the printed page only shows the current view on the screen and not the entire page. By going through some code from online searches, I was able to print the entire page using the following code. But the printed page has tried to print it all in one page with a scrollbar enabled. Moreover, the printed content is very blurred (as shown below).
Question: How can we make the printed content not blurred while still printing the entire page with scrollbar enable?
Website used in the WebView: Wikipedia
Reference: WebView to WebViewBrush
MainPage.xaml:
<Page
x:Class="UWP_WebViewPrinting.MainPage"
....>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<WebView x:Name="wvTest" Source="https://en.wikipedia.org/wiki/Universal_Windows_Platform" Grid.Row="1" Margin="0,61,0,0"/>
<Button x:Name="btnTest" Content="Test" Grid.Row="0" VerticalAlignment="Top" Click="btnTest_Click"/>
<Rectangle x:Name="RectangleToPrint" Grid.Row="1"/>
</Grid>
</Page>
MainPage.xaml.cs:
private async void btnTest_Click(object sender, RoutedEventArgs e)
{
//Step 1: use WebViewBrush to render the content of webview into the Rectangle
int width;
int height;
// get the total width and height
var widthString = await wvTest.InvokeScriptAsync("eval", new[] { "document.body.scrollWidth.toString()" });
var heightString = await wvTest.InvokeScriptAsync("eval", new[] { "document.body.scrollHeight.toString()" });
if (!int.TryParse(widthString, out width))
{
throw new Exception("Unable to get page width");
}
if (!int.TryParse(heightString, out height))
{
throw new Exception("Unable to get page height");
}
// resize the webview to the content
wvTest.Width = width;
wvTest.Height = height;
WebViewBrush b = new WebViewBrush();
b.SourceName = "wvTest";
b.Redraw();
RectangleToPrint.Fill = b;
//Step 2: Then print the rectangle
if (PrintManager.IsSupported())
{
try
{
// Show print UI
await PrintManager.ShowPrintUIAsync();
}
catch
{
// Printing cannot proceed at this time
ContentDialog noPrintingDialog = new ContentDialog()
{
Title = "Printing error",
Content = "\nSorry, printing can' t proceed at this time.",
PrimaryButtonText = "OK"
};
await noPrintingDialog.ShowAsync();
}
}
else
{
// Printing is not supported on this device
ContentDialog noPrintingDialog = new ContentDialog()
{
Title = "Printing not supported",
Content = "\nSorry, printing is not supported on this device.",
PrimaryButtonText = "OK"
};
await noPrintingDialog.ShowAsync();
}
}
#region Register for printing
protected override void OnNavigatedTo(NavigationEventArgs e)
{
// Register for PrintTaskRequested event
printMan = PrintManager.GetForCurrentView();
printMan.PrintTaskRequested += PrintTaskRequested;
// Build a PrintDocument and register for callbacks
printDoc = new PrintDocument();
printDocSource = printDoc.DocumentSource;
printDoc.Paginate += Paginate;
printDoc.GetPreviewPage += GetPreviewPage;
printDoc.AddPages += AddPages;
}
#endregion
#region Showing the print dialog
private void PrintTaskRequested(PrintManager sender, PrintTaskRequestedEventArgs args)
{
// Create the PrintTask.
// Defines the title and delegate for PrintTaskSourceRequested
var printTask = args.Request.CreatePrintTask("Print", PrintTaskSourceRequrested);
// Handle PrintTask.Completed to catch failed print jobs
printTask.Completed += PrintTaskCompleted;
}
private void PrintTaskSourceRequrested(PrintTaskSourceRequestedArgs args)
{
// Set the document source.
args.SetSource(printDocSource);
}
#endregion
#region Print preview
private void Paginate(object sender, PaginateEventArgs e)
{
// As I only want to print one Rectangle, so I set the count to 1
printDoc.SetPreviewPageCount(1, PreviewPageCountType.Final);
}
private void GetPreviewPage(object sender, GetPreviewPageEventArgs e)
{
// Provide a UIElement as the print preview.
printDoc.SetPreviewPage(e.PageNumber, this.RectangleToPrint);
}
#endregion
#region Add pages to send to the printer
private void AddPages(object sender, AddPagesEventArgs e)
{
printDoc.AddPage(this.RectangleToPrint);
// Indicate that all of the print pages have been provided
printDoc.AddPagesComplete();
}
#endregion
#region Print task completed
private async void PrintTaskCompleted(PrintTask sender, PrintTaskCompletedEventArgs args)
{
// Notify the user when the print operation fails.
if (args.Completion == PrintTaskCompletion.Failed)
{
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
{
ContentDialog noPrintingDialog = new ContentDialog()
{
Title = "Printing error",
Content = "\nSorry, failed to print.",
PrimaryButtonText = "OK"
};
await noPrintingDialog.ShowAsync();
});
}
}
#endregion
Printed PDF: Available here
UPDATE:
A response from #Faywang - MSFT seems quite promising. I tried it as follows:
Created a method call PrintWebView() with the code from the OnPrintButtonClick() event from here
Added all the other methods/events shown in my post above.
In my btnTest_Click(...), I modified the code from user #Faywang as follows
.
List<Rectangle> allpages = await GetWebPages(wvTest, new Windows.Foundation.Size(750d, 950d));
//print these pages
foreach (Rectangle rectangle in allpages)
PrintWebView();
When the app runs in default mode it shows count for allpages to be 7 and the above foreach calls PrintWebView() 7 times. If I put the app screen in max mode, the count for allpages is 3 and the foreach calls PrintWebView() 3 times (as expected). In both cases I was expecting the last iteration of the loop will bring the print dialog and print all pages 7 (or 3 depending on default or max mode of the app's screen). Instead, the debugger went to the catch block of the PrintWebView() method whose code, as stated earlier, is taken from here
Question: Did I follow the above steps correctly? If not, what would be a better way of doing it?
When you compress and redraw the webview to the rectangle, it will become blurred, it's better to display the webview in mutiple pages. First I give the width and height of printed page as 750, 950, and then according to the scale to cacluate how many pages it needs to be. After that, you can print these Rectangles. For example:
private async void btnTest_Click(object sender, RoutedEventArgs e)
{
allPages = await GetWebPages(wvTest, new Windows.Foundation.Size(750d, 950d));
//print these pages
}
async Task<List<Windows.UI.Xaml.Shapes.Rectangle>> GetWebPages(Windows.UI.Xaml.Controls.WebView webView, Windows.Foundation.Size page)
{
// ask the content its width
var _WidthString = await webView.InvokeScriptAsync("eval",
new[] { "document.body.scrollWidth.toString()" });
int _ContentWidth;
if (!int.TryParse(_WidthString, out _ContentWidth))
throw new Exception(string.Format("failure/width:{0}", _WidthString));
webView.Width = _ContentWidth;
// ask the content its height
var _HeightString = await webView.InvokeScriptAsync("eval",
new[] { "document.body.scrollHeight.toString()" });
int _ContentHeight;
if (!int.TryParse(_HeightString, out _ContentHeight))
throw new Exception(string.Format("failure/height:{0}", _HeightString));
webView.Height = _ContentHeight;
// how many pages will there be?
var _Scale = page.Width / _ContentWidth;
var _ScaledHeight = (_ContentHeight * _Scale);
var _PageCount = (double)_ScaledHeight / page.Height;
_PageCount = _PageCount + ((_PageCount > (int)_PageCount) ? 1 : 0);
// create the pages
var _Pages = new List<Windows.UI.Xaml.Shapes.Rectangle>();
for (int i = 0; i < (int)_PageCount; i++)
{
var _TranslateY = -page.Height * i;
var _Page = new Windows.UI.Xaml.Shapes.Rectangle
{
Height = page.Height,
Width = page.Width,
Margin = new Windows.UI.Xaml.Thickness(5),
Tag = new Windows.UI.Xaml.Media.TranslateTransform { Y = _TranslateY },
};
_Page.Loaded += async (s, e) =>
{
var _Rectangle = s as Windows.UI.Xaml.Shapes.Rectangle;
var _Brush = await GetWebViewBrush(webView);
_Brush.Stretch = Windows.UI.Xaml.Media.Stretch.UniformToFill;
_Brush.AlignmentY = Windows.UI.Xaml.Media.AlignmentY.Top;
_Brush.Transform = _Rectangle.Tag as Windows.UI.Xaml.Media.TranslateTransform;
_Rectangle.Fill = _Brush;
};
_Pages.Add(_Page);
}
return _Pages;
}
async Task<WebViewBrush> GetWebViewBrush(Windows.UI.Xaml.Controls.WebView webView)
{
// resize width to content
var _OriginalWidth = webView.Width;
var _WidthString = await webView.InvokeScriptAsync("eval",
new[] { "document.body.scrollWidth.toString()" });
int _ContentWidth;
if (!int.TryParse(_WidthString, out _ContentWidth))
throw new Exception(string.Format("failure/width:{0}", _WidthString));
webView.Width = _ContentWidth;
// resize height to content
var _OriginalHeight = webView.Height;
var _HeightString = await webView.InvokeScriptAsync("eval",
new[] { "document.body.scrollHeight.toString()" });
int _ContentHeight;
if (!int.TryParse(_HeightString, out _ContentHeight))
throw new Exception(string.Format("failure/height:{0}", _HeightString));
webView.Height = _ContentHeight;
// create brush
var _OriginalVisibilty = webView.Visibility;
webView.Visibility = Windows.UI.Xaml.Visibility.Visible;
var _Brush = new WebViewBrush
{
SourceName = webView.Name,
Stretch = Windows.UI.Xaml.Media.Stretch.Uniform
};
_Brush.Redraw();
// reset, return
webView.Width = _OriginalWidth;
webView.Height = _OriginalHeight;
webView.Visibility = _OriginalVisibilty;
return _Brush;
}

c# uwp : dispatch webview navigation and capture on each avaible logical cpu

i'm learning code and doing an app that capture google streetview screenshost. Many screenshots. I want to find a way to parallel the queries.
Here the code for single query at time :
// prepare the html code string in a dictionary (I use street view in a iframe)
Dictionary<int, string> htmlCode= SetUpURLListHD();
// prepare the webview and display it on a grid
WebView saveWebView = new WebView();
saveWebView.Width = Width;
saveWebView.Height = Height;
TopGrid.Children.Add(saveWebView);
// navigate to string, wait a moment until street view completely loaded, do a capture, save it to file m.jpg with custom function
for (var m = 0; m < htmlCode.Count; m++)
{
saveWebView.NavigateToString(htmlCode[m]);
await Task.Delay(2000);
await saveWebView.CapturePreviewToStreamAsync(stream);
await SaveSSAsync(stream, m);
}
I have 16 logical cpu, 64Go of ram, and a fiber connection. So, I want to do until 16 queries at time.
For that, I think I need to write 16 functions to create 16 webviews :
async Task ProcessURLHD1Async(string url, int i, int width, int height, ulong firstFrameSize)
{
try
{
WebView saveWebView1 = new WebView();
saveWebView1.Width = width;
saveWebView1.Height = height;
TopGrid.Children.Add(saveWebView1);
...
Second function :
async Task ProcessURLHD2Async(string url, int i, int width, int height, ulong firstFrameSize)
{
try
{
WebView saveWebView2 = new WebView();
saveWebView2.Width = width;
saveWebView2.Height = height;
TopGrid.Children.Add(saveWebView2);
Etc...
I don't know if it's the right way to do that.
I'm now trying to execute the 16 functions at the same time, and when one ended, start again with next html code (next street view mage coded in the dictionary).
For now, no result.
Any help is welcome.
Thank you !
ps : it's my first message after weeks of coding, thanks for all people who help on this site, very useful for me !
EDIT : Ok ! I found ! It's ugly, but it's fast ! :
// below namespace :
public static class DispatcherTaskExtensions
{
public static async Task<T> RunTaskAsync<T>(this CoreDispatcher dispatcher,
Func<Task<T>> func, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal)
{
var taskCompletionSource = new TaskCompletionSource<T>();
await dispatcher.RunAsync(priority, async () =>
{
try
{
taskCompletionSource.SetResult(await func());
}
catch (Exception ex)
{
taskCompletionSource.SetException(ex);
}
});
return await taskCompletionSource.Task;
}
// There is no TaskCompletionSource<void> so we use a bool that we throw away.
public static async Task RunTaskAsync(this CoreDispatcher dispatcher,
Func<Task> func, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal) =>
await RunTaskAsync(dispatcher, async () => { await func(); return false; }, priority);
}
//Call function on click
private async void GetHDMT_Click(object sender, RoutedEventArgs e)
{
if (outputFolder == null)
{
var folderPicker = new Windows.Storage.Pickers.FolderPicker();
folderPicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.Desktop;
folderPicker.FileTypeFilter.Add("*");
outputFolder = await folderPicker.PickSingleFolderAsync();
if (outputFolder != null)
{
// Application now has read/write access to all contents in the picked folder
// (including other sub-folder contents)
Windows.Storage.AccessCache.StorageApplicationPermissions.
FutureAccessList.AddOrReplace("PickedFolderToken", outputFolder);
}
else
{
}
}
if (mylat.Count > 0)
{
await GetImagesAsyncHD();
}
}
private async Task GetImagesAsyncHD()
{
// Make a list of html code with street view iframe.
var Width = 4000;
var Height = 2000;
var urls = SetUpURLListHD(Width, Height);
WebView saveWebView0 = new WebView
{
Width = Width,
Height = Height
};
TopGrid.Children.Add(saveWebView0);
WebView saveWebView1 = new WebView
{
Width = Width,
Height = Height
};
TopGrid.Children.Add(saveWebView1);
WebView saveWebView2 = new WebView
{
Width = Width,
Height = Height
};
TopGrid.Children.Add(saveWebView2);
WebView saveWebView3 = new WebView
{
Width = Width,
Height = Height
};
TopGrid.Children.Add(saveWebView3); WebView
saveWebView4 = new WebView
{
Width = Width,
Height = Height
};
TopGrid.Children.Add(saveWebView4);
WebView saveWebView5 = new WebView
{
Width = Width,
Height = Height
};
TopGrid.Children.Add(saveWebView5);
WebView saveWebView6 = new WebView
{
Width = Width,
Height = Height
};
TopGrid.Children.Add(saveWebView6);
WebView saveWebView7 = new WebView
{
Width = Width,
Height = Height
};
TopGrid.Children.Add(saveWebView7); WebView
saveWebView8 = new WebView
{
Width = Width,
Height = Height
};
TopGrid.Children.Add(saveWebView8);
WebView saveWebView9 = new WebView
{
Width = Width,
Height = Height
};
TopGrid.Children.Add(saveWebView9);
WebView saveWebView10 = new WebView
{
Width = Width,
Height = Height
};
TopGrid.Children.Add(saveWebView10);
WebView saveWebView11 = new WebView
{
Width = Width,
Height = Height
};
TopGrid.Children.Add(saveWebView11);
WebView saveWebView12 = new WebView
{
Width = Width,
Height = Height
};
TopGrid.Children.Add(saveWebView12);
WebView saveWebView13 = new WebView
{
Width = Width,
Height = Height
};
TopGrid.Children.Add(saveWebView13);
WebView saveWebView14 = new WebView
{
Width = Width,
Height = Height
};
TopGrid.Children.Add(saveWebView14);
WebView saveWebView15 = new WebView
{
Width = Width,
Height = Height
};
TopGrid.Children.Add(saveWebView15);
List<Task> tasks = new List<Task>();
Task t0 = Task.Run(async () =>
{
for (var j=0; j<urls.Count; j=j+16)
await ProcessURLHD0Async(urls[j], j, Width, Height, firstFrameSize, saveWebView0, outputFolder);
});
tasks.Add(t0);
Task t1 = Task.Run(async () =>
{
for (var j = 1; j < urls.Count; j = j + 16)
await ProcessURLHD0Async(urls[j], j, Width, Height, firstFrameSize, saveWebView0, outputFolder);
});
tasks.Add(t1);
Task t2 = Task.Run(async () =>
{
for (var j = 2; j < urls.Count; j = j + 16)
await ProcessURLHD0Async(urls[j], j, Width, Height, firstFrameSize, saveWebView0, outputFolder);
});
tasks.Add(t2);
Task t3 = Task.Run(async () =>
{
for (var j = 3; j < urls.Count; j = j + 16)
await ProcessURLHD0Async(urls[j], j, Width, Height, firstFrameSize, saveWebView0, outputFolder);
});
tasks.Add(t3);
Task t4 = Task.Run(async () =>
{
for (var j = 4; j < urls.Count; j = j + 16)
await ProcessURLHD0Async(urls[j], j, Width, Height, firstFrameSize, saveWebView0, outputFolder);
});
tasks.Add(t4);
Task t5 = Task.Run(async () =>
{
for (var j = 5; j < urls.Count; j = j + 16)
await ProcessURLHD0Async(urls[j], j, Width, Height, firstFrameSize, saveWebView0, outputFolder);
});
tasks.Add(t5);
Task t6 = Task.Run(async () =>
{
for (var j = 6; j < urls.Count; j = j + 16)
await ProcessURLHD0Async(urls[j], j, Width, Height, firstFrameSize, saveWebView0, outputFolder);
});
tasks.Add(t6);
Task t7 = Task.Run(async () =>
{
for (var j = 7; j < urls.Count; j = j + 16)
await ProcessURLHD0Async(urls[j], j, Width, Height, firstFrameSize, saveWebView0, outputFolder);
});
tasks.Add(t7);
Task t8 = Task.Run(async () =>
{
for (var j = 8; j < urls.Count; j = j + 16)
await ProcessURLHD0Async(urls[j], j, Width, Height, firstFrameSize, saveWebView0, outputFolder);
});
tasks.Add(t8);
Task t9 = Task.Run(async () =>
{
for (var j = 9; j < urls.Count; j = j + 16)
await ProcessURLHD0Async(urls[j], j, Width, Height, firstFrameSize, saveWebView0, outputFolder);
});
tasks.Add(t9);
Task t10 = Task.Run(async () =>
{
for (var j = 10; j < urls.Count; j = j + 16)
await ProcessURLHD0Async(urls[j], j, Width, Height, firstFrameSize, saveWebView0, outputFolder);
});
tasks.Add(t10);
Task t11 = Task.Run(async () =>
{
for (var j = 11; j < urls.Count; j = j + 16)
await ProcessURLHD0Async(urls[j], j, Width, Height, firstFrameSize, saveWebView0, outputFolder);
});
tasks.Add(t11);
Task t12 = Task.Run(async () =>
{
for (var j = 12; j < urls.Count; j = j + 16)
await ProcessURLHD0Async(urls[j], j, Width, Height, firstFrameSize, saveWebView0, outputFolder);
});
tasks.Add(t12);
Task t13 = Task.Run(async () =>
{
for (var j = 13; j < urls.Count; j = j + 16)
await ProcessURLHD0Async(urls[j], j, Width, Height, firstFrameSize, saveWebView0, outputFolder);
});
tasks.Add(t13);
Task t14 = Task.Run(async () =>
{
for (var j = 14; j < urls.Count; j = j + 16)
await ProcessURLHD0Async(urls[j], j, Width, Height, firstFrameSize, saveWebView0, outputFolder);
});
tasks.Add(t14);
Task t15 = Task.Run(async () =>
{
for (var j = 15; j < urls.Count; j = j + 16)
await ProcessURLHD0Async(urls[j], j, Width, Height, firstFrameSize, saveWebView0, outputFolder);
});
tasks.Add(t15);
await Task.WhenAll(tasks);
}
public static async Task ProcessURLHD0Async(string url, int i, int width, int height, ulong firstFrameSize, WebView saveWeb, StorageFolder folder)
{
try
{
var dispatcher = CoreApplication.MainView.CoreWindow.Dispatcher;
await dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
saveWeb.NavigateToString(url);
});
await Task.Delay(3000);
var kk = 0;
var pixelised = true;
while (pixelised == true)
{
ulong thissize = await CaptureAndSave(saveWeb, i, folder);
if ((thissize > 0.7 * firstFrameSize) || kk > 5)
{
pixelised = false;
}
else
{
await Task.Delay(1000);
kk = kk + 1;
}
}
}
catch (Exception)
{
throw;
}
}
public static async Task<ulong> CaptureAndSave(WebView webv, int i, StorageFolder folder)
{
ulong size = 0;
SoftwareBitmap softwareBitmap = null;
InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream();
BitmapDecoder decoder = null;
// Changes to imageElement must happen on the UI thread.
await webv.Dispatcher.RunTaskAsync(async () =>
{
await webv.CapturePreviewToStreamAsync(stream);
decoder = await BitmapDecoder.CreateAsync(stream);
softwareBitmap = await decoder.GetSoftwareBitmapAsync();
});
StorageFile file_Save = await folder.CreateFileAsync(i + ".jpg", CreationCollisionOption.ReplaceExisting);
if (file_Save != null)
{
using (var streamF = await file_Save.OpenAsync(FileAccessMode.ReadWrite))
{
//StorageFile file_Save = await outputFolder.CreateFileAsync(i + "_" + equals + "_" + size + ".jpg", CreationCollisionOption.ReplaceExisting);
//< encoder to save >
BitmapEncoder encoder = await BitmapEncoder.CreateAsync(Windows.Graphics.Imaging.BitmapEncoder.JpegEncoderId, streamF);
encoder.SetSoftwareBitmap(softwareBitmap);
await encoder.FlushAsync();
//</ encoder to save >
//-</ Save Bitmap as File >-
}
}
var basicProperties = await file_Save.GetBasicPropertiesAsync();
size = basicProperties.Size;
while (size < 21)
{
await Task.Delay(100);
basicProperties = await file_Save.GetBasicPropertiesAsync();
size = basicProperties.Size;
}
stream.Dispose();
softwareBitmap.Dispose();
return size;
}
Results : 118 capture (total = 139Mo) in 20 sec against 278 without paralleling !!! 5 GB of ram used at max.
Edit 2 :
To fast... there is an error in above code, I always use Savewebview0, when I correct (Savewebview1, Savewebview2....) the results is... 400 seconds...
All the work is do by the main thread I think, I need to create webviews in others threads, but I think it's not possible...
If anyone has a idea... :(
Ok, after many trials, I have good results.
The code is here for who want to use it :
public static class DispatcherTaskExtensions
{
public static async Task<T> RunTaskAsync<T>(this CoreDispatcher dispatcher,
Func<Task<T>> func, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal)
{
var taskCompletionSource = new TaskCompletionSource<T>();
await dispatcher.RunAsync(priority, async () =>
{
try
{
taskCompletionSource.SetResult(await func());
}
catch (Exception ex)
{
taskCompletionSource.SetException(ex);
}
});
return await taskCompletionSource.Task;
}
// There is no TaskCompletionSource<void> so we use a bool that we throw away.
public static async Task RunTaskAsync(this CoreDispatcher dispatcher,
Func<Task> func, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal) =>
await RunTaskAsync(dispatcher, async () => { await func(); return false; }, priority);
}
Above code below namespace delcaration.
private async void GetHDMT2_Click(object sender, RoutedEventArgs e)
{
if (outputFolder == null)
{
var folderPicker = new Windows.Storage.Pickers.FolderPicker();
folderPicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.Desktop;
folderPicker.FileTypeFilter.Add("*");
outputFolder = await folderPicker.PickSingleFolderAsync();
if (outputFolder != null)
{
Windows.Storage.AccessCache.StorageApplicationPermissions.
FutureAccessList.AddOrReplace("PickedFolderToken", outputFolder);
}
else
{
}
}
if (mylat.Count > 0)
{
await GetImagesAsyncHD2();
}
}
private async Task GetImagesAsyncHD2()
{
// Make a dictionary with html code string (with street view iframe inside .
var Width = 4000;
var Height = 2000;
Dictionary<int,string> urls = SetUpURLListHD(Width, Height);
//Code to estimate time for completely loading the first page (compute a time to use in await task.delay, and the size of first frame nextly used to know if image is pixelised or not (street view fisnish loading or not)
SoftwareBitmap softwareBitmap = null;
InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream();
BitmapDecoder decoder = null;
var k = 0;
WebView saveWebView = new WebView();
saveWebView.Width = Width;
saveWebView.Height = Height;
TopGrid.Children.Add(saveWebView);
var stopwatch = new Stopwatch();
stopwatch.Start();
long elapsed_time=0;
saveWebView.NavigateToString(urls[0]);
await Task.Delay(10);
bool equals = false;
bool findeq = false;
int t = 0;
int countTrue = 0;
bool checka = false;
List<byte[]> bytes400a = null;
List<byte[]> bytes400b = null;
await saveWebView.CapturePreviewToStreamAsync(stream);
decoder = await BitmapDecoder.CreateAsync(stream);
softwareBitmap = await decoder.GetSoftwareBitmapAsync();
bytes400a = GetPixelsP(softwareBitmap);
while (countTrue < 20)
{
await Task.Delay(50);
if (checka)
{
await saveWebView.CapturePreviewToStreamAsync(stream);
decoder = await BitmapDecoder.CreateAsync(stream);
softwareBitmap = await decoder.GetSoftwareBitmapAsync();
bytes400a = GetPixelsP(softwareBitmap);
checka = false;
}
else
{
await saveWebView.CapturePreviewToStreamAsync(stream);
decoder = await BitmapDecoder.CreateAsync(stream);
softwareBitmap = await decoder.GetSoftwareBitmapAsync();
bytes400b = GetPixelsP(softwareBitmap);
checka = true;
}
t = t + 1;
equals = EqualsByte(bytes400a, bytes400b);
if (equals)
{
countTrue = countTrue + 1;
}
if ((equals) && (findeq == false))
{
k = t;
elapsed_time = stopwatch.ElapsedMilliseconds;
SlidetimeCompute.Value = elapsed_time;
findeq = true;
}
if (equals == false) { findeq = false; }
}
stopwatch.Stop();
var elapsed_timeForTest = stopwatch.ElapsedMilliseconds;
await SaveSSAsync(softwareBitmap, "0");
ulong firstFrameSize = await GetSizePicAsync("0");
TopGrid.Children.Remove(saveWebView);
stream.Dispose();
softwareBitmap.Dispose();
double timeTowait = (double)elapsed_time;
timeTowait = Math.Round(timeTowait / 4);
// prepare somes grids
var nj = 7;
List<Grid> grids = new List<Grid>();
Grid tmpgrid0 = new Grid();
grids.Add(tmpgrid0);
Grid tmpgrid1 = new Grid();
grids.Add(tmpgrid1);
Grid tmpgrid2 = new Grid();
grids.Add(tmpgrid2);
Grid tmpgrid3 = new Grid();
grids.Add(tmpgrid3);
Grid tmpgrid4 = new Grid();
grids.Add(tmpgrid4);
Grid tmpgrid5 = new Grid();
grids.Add(tmpgrid5);
Grid tmpgrid6 = new Grid();
grids.Add(tmpgrid6);
Grid tmpgrid7 = new Grid();
grids.Add(tmpgrid7);
Grid tmpgrid8 = new Grid();
grids.Add(tmpgrid8);
Grid tmpgrid9 = new Grid();
grids.Add(tmpgrid9);
Grid tmpgrid10 = new Grid();
grids.Add(tmpgrid10);
Grid tmpgrid11 = new Grid();
grids.Add(tmpgrid11);
Grid tmpgrid12 = new Grid();
grids.Add(tmpgrid12);
Grid tmpgrid13 = new Grid();
grids.Add(tmpgrid13);
Grid tmpgrid14 = new Grid();
grids.Add(tmpgrid14);
Grid tmpgrid15 = new Grid();
grids.Add(tmpgrid15);
Grid tmpgrid16 = new Grid();
grids.Add(tmpgrid16);
// add tasks
List<Task> tasks = new List<Task>();
for (var i = 1; i < nj+1; i++)
{
TopGrid.Children.Add(grids[i]);
await Task.Delay(1000);
tasks.Add(await AddTaskAsync(i, nj, Width, Height, firstFrameSize,outputFolder, grids[i], urls, timeTowait)
);
}
// await all tasks
await Task.WhenAll(tasks);
}
public static async Task<Task> AddTaskAsync(int i, int nj, int width, int height, ulong firstFrameSize, StorageFolder folder, Grid grid, Dictionary<int, string> urls, double timeTowait)
{
var t0 = Task.Run(async () =>
{
for (var j = i; j < urls.Count; j = j + nj)
await ProcessURLHD1Async(urls[j], j, width, height, firstFrameSize, folder, grid, timeTowait);
});
await Task.Delay(200);
return t0;
}
public static async Task ProcessURLHD1Async(string url, int i, int width, int height, ulong firstFrameSize, StorageFolder folder, Grid grid, double timeTowait)
{
try
{
var Dispatcher = CoreApplication.MainView.CoreWindow.Dispatcher;
await grid.Dispatcher.RunTaskAsync(async () =>
{
var saveWeb1 = new WebView(WebViewExecutionMode.SeparateThread)
{
Width = width,
Height = height
};
grid.Children.Add(saveWeb1);
saveWeb1.NavigateToString(url);
//give street view some delay to load
await Task.Delay((int)timeTowait*5);
var kk = 0;
var pixelised = true;
SoftwareBitmap softwareBitmap = null;
InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream();
BitmapDecoder decoder = null;
while (pixelised == true)
{
await saveWeb1.CapturePreviewToStreamAsync(stream);
decoder = await BitmapDecoder.CreateAsync(stream);
softwareBitmap = await decoder.GetSoftwareBitmapAsync();
StorageFile file_Save = await folder.CreateFileAsync(i + ".jpg", CreationCollisionOption.ReplaceExisting);
if (file_Save != null)
{
using (var streamF = await file_Save.OpenAsync(FileAccessMode.ReadWrite))
{
BitmapEncoder encoder = await BitmapEncoder.CreateAsync(Windows.Graphics.Imaging.BitmapEncoder.JpegEncoderId, streamF);
encoder.SetSoftwareBitmap(softwareBitmap);
await encoder.FlushAsync();
}
}
var basicProperties = await file_Save.GetBasicPropertiesAsync();
ulong thissize = basicProperties.Size;
while (thissize < 21)
{
await Task.Delay(100);
basicProperties = await file_Save.GetBasicPropertiesAsync();
thissize = basicProperties.Size;
}
if ((thissize > 0.7 * firstFrameSize) || kk > 10)
{
pixelised = false;
}
else
{
await Task.Delay((int)timeTowait*2);
kk = kk + 1;
}
}
stream.Dispose();
softwareBitmap.Dispose();
grid.Children.Clear();
});
}
catch (Exception)
{
throw;
}
}
Best results is with 7 task in parallel. Why 7 ? Don't know.
With single task, 118 captures (141 Mo) in 304 sec, with 7 tasks 58 secs !
Now, I need to find a way to adapt the code to different hardware and internet connection.

Printing a local file on Android via Xamarin.Forms

I need to use the print dialog via Forms. I have found an solution on iOS but the android implementation is giving me problems.
As far as i can see it is not possible to call the Android print manager and parse a file to it.
It can only be a Android.Views.View, is that true?
To do that would be my ideal solution.
I have tried to convert my content (A webview showing a local pdf) to this android view but this seems also not really to work but i am out off my depths here.
in the code below i try to convert a forms.webview to an android.view and then parse it to the print manager.
This produce the print dialog but with a black white page.
var size = new Rectangle(webview.X, webview.Y, webview.Width, webview.Height);
var vRenderer = Xamarin.Forms.Platform.Android.Platform.CreateRenderer(webview);
var viewGroup = vRenderer.ViewGroup;
vRenderer.Tracker.UpdateLayout();
var layoutParams = new Android.Views.ViewGroup.LayoutParams((int)size.Width, (int)size.Height);
viewGroup.LayoutParameters = layoutParams;
webview.Layout(size);
viewGroup.Layout(0, 0, (int)webview.WidthRequest, (int)webview.HeightRequest);
var printMgr = (Android.Print.PrintManager)Forms.Context.GetSystemService(Android.Content.Context.PrintService);
var docAdt = new Droid.GenericPrintAdapter(Forms.Context, viewGroup);
printMgr.Print("test", docAdt, null);
The next is the "GenericPrintAdapter"
public class GenericPrintAdapter : PrintDocumentAdapter
{
View view;
Context context;
PrintedPdfDocument document;
float scale;
public GenericPrintAdapter(Context context, View view)
{
this.view = view;
this.context = context;
}
public override void OnLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes,
CancellationSignal cancellationSignal, LayoutResultCallback callback, Bundle extras)
{
document = new PrintedPdfDocument(context, newAttributes);
CalculateScale(newAttributes);
var printInfo = new PrintDocumentInfo
.Builder("MyPrint.pdf")
.SetContentType(PrintContentType.Document)
.SetPageCount(1)
.Build();
callback.OnLayoutFinished(printInfo, true);
}
void CalculateScale(PrintAttributes newAttributes)
{
int dpi = Math.Max(newAttributes.GetResolution().HorizontalDpi, newAttributes.GetResolution().VerticalDpi);
int leftMargin = (int)(dpi * (float)newAttributes.MinMargins.LeftMils / 1000);
int rightMargin = (int)(dpi * (float)newAttributes.MinMargins.RightMils / 1000);
int topMargin = (int)(dpi * (float)newAttributes.MinMargins.TopMils / 1000);
int bottomMargin = (int)(dpi * (float)newAttributes.MinMargins.BottomMils / 1000);
int w = (int)(dpi * (float)newAttributes.GetMediaSize().WidthMils / 1000) - leftMargin - rightMargin;
int h = (int)(dpi * (float)newAttributes.GetMediaSize().HeightMils / 1000) - topMargin - bottomMargin;
scale = Math.Min((float)document.PageContentRect.Width() / w, (float)document.PageContentRect.Height() / h);
}
public override void OnWrite(PageRange[] pages, ParcelFileDescriptor destination,
CancellationSignal cancellationSignal, WriteResultCallback callback)
{
PrintedPdfDocument.Page page = document.StartPage(0);
page.Canvas.Scale(scale, scale);
view.Draw(page.Canvas);
document.FinishPage(page);
WritePrintedPdfDoc(destination);
document.Close();
document.Dispose();
callback.OnWriteFinished(pages);
}
void WritePrintedPdfDoc(ParcelFileDescriptor destination)
{
var javaStream = new Java.IO.FileOutputStream(destination.FileDescriptor);
var osi = new OutputStreamInvoker(javaStream);
using (var mem = new MemoryStream())
{
document.WriteTo(mem);
var bytes = mem.ToArray();
osi.Write(bytes, 0, bytes.Length);
}
}
}
I have now a "working" solution.
This gets the current Android.Webkir.WebView and create the printAdapter from that.
Thanks to #SushiHangover for pointing me in the right direction.
Its a bit of a hack but works for my needs.
If anyone else ever needs this i have included both the Android and iOS code.
#if __ANDROID__
var vRenderer = Xamarin.Forms.Platform.Android.Platform.GetRenderer(webview);
var viewGroup = vRenderer.ViewGroup;
for (int i = 0; i < viewGroup.ChildCount; i++)
{
if (viewGroup.GetChildAt(i).GetType().Name == "WebView")
{
var AndroidWebView = viewGroup.GetChildAt(i) as Android.Webkit.WebView;
var tmp = AndroidWebView.CreatePrintDocumentAdapter("print");
var printMgr = (Android.Print.PrintManager)Forms.Context.GetSystemService(Android.Content.Context.PrintService);
printMgr.Print("print", tmp, null);
break;
}
}
#elif __IOS__
var printInfo = UIKit.UIPrintInfo.PrintInfo;
printInfo.Duplex = UIKit.UIPrintInfoDuplex.LongEdge;
printInfo.OutputType = UIKit.UIPrintInfoOutputType.General;
printInfo.JobName = "AppPrint";
var printer = UIKit.UIPrintInteractionController.SharedPrintController;
printer.PrintInfo = printInfo;
printer.PrintingItem = Foundation.NSData.FromFile(pdfPath);
printer.ShowsPageRange = true;
printer.Present(true, (handler, completed, err) =>
{
if (!completed && err != null)
{
System.Diagnostics.Debug.WriteLine("Printer Error");
}
});
#endif

How to save WebViewBrush as image? (UWP / Universal)

Summary: I have a collection FrameworkElements (basically web view brushes drawn on rectanlges), and I'd like to save each of these as a PNG file in my UWP app.
More details: I followed the example at https://stackoverflow.com/a/17222629/2884981 to split the content of a WebView into separate "pages".
I've put the main bits of code at the bottom.
At the bottom of GetWebPages() I have return pages;
At this point I have a list of all the "pages".
What I'd like to do, is then convert each of those pages into an image (so by the end of it I'd have a collection of PNG files, for instance).
Does anyone know how I can do this? Thanks in advance.
public async Task<WebViewBrush> GetWebViewBrush(WebView webView)
{
// resize width to content
double originalWidth = webView.Width;
var widthString = await webView.InvokeScriptAsync("eval", new[] { "document.body.scrollWidth.toString()" });
int contentWidth;
if (!int.TryParse(widthString, out contentWidth))
{
throw new Exception(string.Format("failure/width:{0}", widthString));
}
webView.Width = contentWidth;
// resize height to content
double originalHeight = webView.Height;
var heightString = await webView.InvokeScriptAsync("eval", new[] { "document.body.scrollHeight.toString()" });
int contentHeight;
if (!int.TryParse(heightString, out contentHeight))
{
throw new Exception(string.Format("failure/height:{0}", heightString));
}
webView.Height = contentHeight;
// create brush
var originalVisibilty = webView.Visibility;
webView.Visibility = Windows.UI.Xaml.Visibility.Visible;
WebViewBrush brush = new WebViewBrush
{
SourceName = webView.Name,
Stretch = Stretch.Uniform
};
brush.Redraw();
// reset, return
webView.Width = originalWidth;
webView.Height = originalHeight;
webView.Visibility = originalVisibilty;
return brush;
}
And:
public async Task<IEnumerable<FrameworkElement>> GetWebPages(WebView webView, Windows.Foundation.Size page)
{
// ask the content its width
var widthString = await webView.InvokeScriptAsync("eval", new[] { "document.body.scrollWidth.toString()" });
int contentWidth;
if (!int.TryParse(widthString, out contentWidth))
{
throw new Exception(string.Format("failure/width:{0}", widthString));
}
webView.Width = contentWidth;
// ask the content its height
var heightString = await webView.InvokeScriptAsync("eval", new[] { "document.body.scrollHeight.toString()" });
int contentHeight;
if (!int.TryParse(heightString, out contentHeight))
{
throw new Exception(string.Format("failure/height:{0}", heightString));
}
webView.Height = contentHeight;
// how many pages will there be?
double scale = page.Width / contentWidth;
double scaledHeight = (contentHeight * scale);
double pageCount = (double) scaledHeight / page.Height;
pageCount = pageCount + ((pageCount > (int) pageCount) ? 1 : 0);
// create the pages
var pages = new List<Windows.UI.Xaml.Shapes.Rectangle>();
for (int i = 0; i < (int)pageCount; i++)
{
var translateY = -page.Height * i;
var rectanglePage = new Windows.UI.Xaml.Shapes.Rectangle
{
Height = page.Height,
Width = page.Width,
Margin = new Thickness(5),
Tag = new TranslateTransform { Y = translateY },
};
rectanglePage.Loaded += (async (s, e) =>
{
var subRectangle = s as Windows.UI.Xaml.Shapes.Rectangle;
var subBrush = await GetWebViewBrush(webView);
subBrush.Stretch = Stretch.UniformToFill;
subBrush.AlignmentY = AlignmentY.Top;
subBrush.Transform = subRectangle.Tag as TranslateTransform;
subRectangle.Fill = subBrush;
});
pages.Add(rectanglePage);
}
return pages;
}
I'd like to save each of these as a PNG file in my UWP app.
You can get all the rectangles and show them in the ItemsControl in the NavigationCompleted event of WebView like this:
private async void webView_NavigationCompleted(WebView sender, WebViewNavigationCompletedEventArgs args)
{
MyWebViewRectangle.Fill = await GetWebViewBrush(webView);
MyPrintPages.ItemsSource = await GetWebPages(webView, new Windows.Foundation.Size(842, 595));
}
Then in a button click event you can save all the items as .png images like this:
private async void Savetoimage_Clicked(object sender, RoutedEventArgs e)
{
var piclib = Windows.Storage.KnownFolders.PicturesLibrary;
foreach (var item in MyPrintPages.Items)
{
var rect = item as Rectangle;
RenderTargetBitmap renderbmp = new RenderTargetBitmap();
await renderbmp.RenderAsync(rect);
var pixels = await renderbmp.GetPixelsAsync();
var file = await piclib.CreateFileAsync("webview.png", Windows.Storage.CreationCollisionOption.GenerateUniqueName);
using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite))
{
BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream);
byte[] bytes = pixels.ToArray();
encoder.SetPixelData(BitmapPixelFormat.Bgra8,
BitmapAlphaMode.Ignore,
(uint)rect.Width, (uint)rect.Height,
0, 0, bytes);
await encoder.FlushAsync();
}
}
}

Categories