Printing a local file on Android via Xamarin.Forms - c#

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

Related

My Widget stops updating after some period of time

I have a widget made up of 2 TextViews, 2 ImageViews, and 2 TextClocks. I am trying to update the widget using a ScreenListener (code below) and an alarm. I want all views to update when the screen is turned On. Also, a repeating alarm is set when the screen is turned On. The alarm is used to trigger the update of the battery level indicator (an ImageView). The image for the battery level indicator is generated using the ProgressRing (code below). The alarm is cancelled when the screen is turned Off. The TextClocks take care of themselves.
Everything works when debugging using the emulator or my phone. The various components update when I turn the screen On and Off. However, when I install the widget on my phone, the widget stops updating after a period of time. Initially, I can turn the screen On and Off and everything updates. And, I can plug (or unplug) the phone and the alarm will update the battery level indicator. But, after some period of time (I think with the screen Off), updating stops. Turning the screen On no longer updates anything and, I beleive, the alarm is no longer set. (Note: The TextClock continue to work and show the correct time.)
When debugging, I have seen the instances of the ScreenLister and ProgressRing become null. So, I've included checks for this and make new instances when it happens. This doesn't seem like the right solution. Should this be occurring?
Is the system killing my widget? Is there a way around this, if so? Is my AppWidgetProvider somehow losing it's connection to the RemoteViews?
Thanks for any help. And, let me know if you need more information.
P.S. I'm currently building for Android 9.0. And, I'm new to Android programming.
AppWidgetProvider
namespace ClockCalBattery
{
[BroadcastReceiver(Enabled = true)]
[IntentFilter(new string[] { "android.appwidget.action.APPWIDGET_UPDATE", Intent.ActionUserPresent})]
[MetaData("android.appwidget.provider", Resource = "#xml/appwidgetprovider")]
public class CCBWidget : AppWidgetProvider, ScreenStateListener
{
public static ProgressRing progressRing = new ProgressRing();
private static String ACTION_UPDATE_BATTERY = "com.lifetree.clockcalbattery.UPDATE-BATTERY";
public static ScreenListener mScreenListener;
public static int n_alarms = 0;
public override void OnReceive(Context context, Intent intent)
{
base.OnReceive(context, intent);
if (intent.Action == ACTION_UPDATE_BATTERY)
{
update_batteryLevel(context);
if (mScreenListener == null)
{
mScreenListener = new ScreenListener(context);
mScreenListener.begin(this);
}
if (progressRing == null)
{
progressRing = new ProgressRing();
}
}
}
public override void OnEnabled(Context context)
{
base.OnEnabled(context);
if (mScreenListener == null)
{
mScreenListener = new ScreenListener(context);
mScreenListener.begin(this);
}
if (progressRing == null)
{
progressRing = new ProgressRing();
}
update_batteryLevel(Application.Context);
update_nextAlarm(Application.Context);
}
public override void OnDisabled(Context context)
{
base.OnDisabled(context);
}
public override void OnUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
{
base.OnUpdate(context, appWidgetManager, appWidgetIds);
mScreenListener = new ScreenListener(context);
progressRing = new ProgressRing();
var me = new ComponentName(context, Java.Lang.Class.FromType(typeof(CCBWidget)).Name);
var widgetView = BuildRemoteViews(context, appWidgetIds);
appWidgetManager.UpdateAppWidget(me, widgetView);
}
private RemoteViews BuildRemoteViews(Context context, int[] appWidgetIds)
{
RemoteViews widgetView = new RemoteViews(context.PackageName, Resource.Layout.CCBWidget);
update_TodaysDate(widgetView);
update_nextAlarm(context, widgetView);
update_batteryLevel(widgetView);
return widgetView;
}
private void update_TodaysDate(RemoteViews widgetView)
{
string today = DateTime.Today.ToString("D");
widgetView.SetTextViewText(Resource.Id.longDate, today);
}
private void update_nextAlarm(Context context, RemoteViews widgetView)
{
AlarmManager am = (AlarmManager)context.GetSystemService(Context.AlarmService);
AlarmManager.AlarmClockInfo alarmInfo = am.NextAlarmClock;
if (alarmInfo != null)
{
long time = alarmInfo.TriggerTime;
DateTime alarmDateTime = new DateTime(1970, 1, 1).AddMilliseconds(time).ToLocalTime();
string alarmTime = alarmDateTime.ToString("ddd ");
alarmTime += alarmDateTime.ToString("t");
widgetView.SetTextViewText(Resource.Id.textView_alarmTime, alarmTime);
widgetView.SetImageViewResource(Resource.Id.imageView_alarmIcon, Resource.Drawable.whiteOnBlack_clock);
}
else
{
widgetView.SetTextViewText(Resource.Id.textView_alarmTime, "");
widgetView.SetImageViewResource(Resource.Id.imageView_alarmIcon, Resource.Drawable.navigation_empty_icon);
}
}
private void update_nextAlarm(Context mContext)
{
RemoteViews widgetView = new RemoteViews(mContext.PackageName, Resource.Layout.CCBWidget);
update_nextAlarm(mContext, widgetView);
var me = new ComponentName(mContext, Java.Lang.Class.FromType(typeof(CCBWidget)).Name);
var awm = AppWidgetManager.GetInstance(mContext);
awm.UpdateAppWidget(me, widgetView);
}
private void update_batteryLevel(RemoteViews widgetView)
{
progressRing.setProgress((int)(Battery.ChargeLevel * 100.0));
progressRing.drawProgressBitmap();
widgetView.SetImageViewBitmap(Resource.Id.progressRing, progressRing.ring);
}
private void update_batteryLevel(Context mContext)
{
RemoteViews widgetView = new RemoteViews(mContext.PackageName, Resource.Layout.CCBWidget);
update_batteryLevel(widgetView);
var me = new ComponentName(mContext, Java.Lang.Class.FromType(typeof(CCBWidget)).Name);
var awm = AppWidgetManager.GetInstance(mContext);
awm.UpdateAppWidget(me, widgetView);
}
public static void turnUpdateAlarmOnOff(Context context, bool turnOn, int time)
{
AlarmManager alarmManager = (AlarmManager)context.GetSystemService(Context.AlarmService);
Intent intent = new Intent(context, typeof(CCBWidget));
intent.SetAction(ACTION_UPDATE_BATTERY);
PendingIntent pendingIntent = PendingIntent.GetBroadcast(context, 0, intent, 0);
if (turnOn)
{
// Add extra 1 sec because sometimes ACTION_BATTERY_CHANGED is called after the first alarm
alarmManager.SetInexactRepeating(AlarmType.ElapsedRealtime, SystemClock.ElapsedRealtime() + 1000, time * 1000, pendingIntent);
n_alarms++;
}
else
{
alarmManager.Cancel(pendingIntent);
n_alarms--;
}
}
public void onScreenOn()
{
if (mScreenListener == null)
{
mScreenListener = new ScreenListener(Application.Context);
mScreenListener.begin(this);
}
if (progressRing == null)
{
progressRing = new ProgressRing();
}
update_batteryLevel(Application.Context);
update_nextAlarm(Application.Context);
turnUpdateAlarmOnOff(Application.Context, true, 60);
}
public void onScreenOff()
{
turnUpdateAlarmOnOff(Application.Context, false, 1);
}
public void onUserPresent()
{
//Console.WriteLine("onUserPresent");
}
}
}
ScreenListener
namespace ClockCalBattery
{
public class ScreenListener
{
private Context mContext;
private ScreenBroadcastReceiver mScreenReceiver;
private static ScreenStateListener mScreenStateListener;
public ScreenListener(Context context)
{
mContext = context;
mScreenReceiver = new ScreenBroadcastReceiver();
}
/**
* screen BroadcastReceiver
*/
private class ScreenBroadcastReceiver : BroadcastReceiver
{
private String action = null;
public override void OnReceive(Context context, Intent intent)
{
action = intent.Action;
if (Intent.ActionScreenOn == action)
{ // screen on
mScreenStateListener.onScreenOn();
}
else if (Intent.ActionScreenOff == action)
{ // screen off
mScreenStateListener.onScreenOff();
}
else if (Intent.ActionUserPresent == action)
{ // unlock
mScreenStateListener.onUserPresent();
}
}
}
/**
* begin to listen screen state
*
* #param listener
*/
public void begin(ScreenStateListener listener)
{
mScreenStateListener = listener;
registerListener();
getScreenState();
}
/**
* get screen state
*/
private void getScreenState()
{
PowerManager manager = (PowerManager)mContext
.GetSystemService(Context.PowerService);
if (manager.IsInteractive)
{
if (mScreenStateListener != null)
{
mScreenStateListener.onScreenOn();
}
}
else
{
if (mScreenStateListener != null)
{
mScreenStateListener.onScreenOff();
}
}
}
/**
* stop listen screen state
*/
public void unregisterListener()
{
mContext.UnregisterReceiver(mScreenReceiver);
}
/**
* regist screen state broadcast
*/
private void registerListener()
{
IntentFilter filter = new IntentFilter();
filter.AddAction(Intent.ActionScreenOn);
filter.AddAction(Intent.ActionScreenOff);
filter.AddAction(Intent.ActionUserPresent);
mContext.ApplicationContext.RegisterReceiver(mScreenReceiver, filter);
//mContext.RegisterReceiver(mScreenReceiver, filter);
}
public interface ScreenStateListener
{// Returns screen status information to the caller
void onScreenOn();
void onScreenOff();
void onUserPresent();
}
}
}
ProgressRing
namespace ClockCalBattery
{
public class ProgressRing
{
private int max = 100;
public int progress;
private Path path = new Path();
Color color = new Color(50, 50, 255, 255);
private Paint paint;
private Paint mPaintProgress;
private RectF mRectF;
private Paint batteryLevelTextPaint;
private Paint batteryStateTextPaint;
private String batteryLevelText = "0%";
private String batteryStateText = null;
private BatteryState bs;
private Rect textBounds = new Rect();
private int centerY;
private int centerX;
private float swipeAndgle = 0;
public Bitmap ring;
private int bitmapWidth = 70;
private int bitmapHeight = 70;
public ProgressRing()
{
progress = -1;
paint = new Paint();
paint.AntiAlias = true;
paint.StrokeWidth = 1;
paint.SetStyle(Paint.Style.Stroke);
paint.Color = color;
mPaintProgress = new Paint();
mPaintProgress.AntiAlias = true;
mPaintProgress.SetStyle(Paint.Style.Stroke);
mPaintProgress.StrokeWidth = 5;
mPaintProgress.Color = color;
batteryLevelTextPaint = new Paint();
batteryLevelTextPaint.AntiAlias = true;
batteryLevelTextPaint.SetStyle(Paint.Style.Fill);
batteryLevelTextPaint.Color = color;
batteryLevelTextPaint.StrokeWidth = 1;
batteryStateTextPaint = new Paint();
batteryStateTextPaint.AntiAlias = true;
batteryStateTextPaint.SetStyle(Paint.Style.Fill); ;
batteryStateTextPaint.Color = color;
batteryStateTextPaint.StrokeWidth = 1;
//batteryStateTextPaint.SetTypeface(Typeface.Create(Typeface.Default, TypefaceStyle.Bold));
Init_ring();
}
private void Init_ring()
{
int viewWidth = bitmapWidth;
int viewHeight = bitmapHeight;
float radius = (float)(bitmapHeight / 2.0);
path.Reset();
centerX = viewWidth / 2;
centerY = viewHeight / 2;
path.AddCircle(centerX, centerY, radius, Path.Direction.Cw);
float smallCirclRadius = radius - (float)(0.1 * radius);
path.AddCircle(centerX, centerY, smallCirclRadius, Path.Direction.Cw);
//mRectF = new RectF(0, 0, viewWidth, viewHeight);
mRectF = new RectF(centerX - smallCirclRadius, centerY - smallCirclRadius, centerX + smallCirclRadius, centerY + smallCirclRadius);
batteryLevelTextPaint.TextSize = radius * 0.5f;
batteryStateTextPaint.TextSize = radius * 0.30f;
}
internal void setProgress(int progress)
{
this.progress = progress;
int percentage = progress * 100 / max;
swipeAndgle = percentage * 360 / 100;
batteryLevelText = percentage + "%";
batteryStateText = null;
bs = Battery.State;
if (bs == BatteryState.Charging)
batteryStateText = "Charging";
else if (percentage > 99)
batteryStateText = "Full";
else if (percentage < 15)
batteryStateText = "Low";
}
internal void drawProgressBitmap()
{
ring = Bitmap.CreateBitmap(bitmapWidth, bitmapHeight, Bitmap.Config.Argb8888);
Canvas c = new Canvas(ring);
byte r = 0;
byte g = 0;
byte b = 0;
byte a = 255;
hls2rgb(swipeAndgle * (120.0 / 360.0), 1, 128, ref r, ref g, ref b);
paint.Color = new Color(r, g, b, a);
mPaintProgress.Color = new Color(r, g, b, a);
batteryLevelTextPaint.Color = new Color(r, g, b, a);
batteryStateTextPaint.Color = new Color(r, g, b, a);
c.DrawArc(mRectF, 270, swipeAndgle, false, mPaintProgress);
drawTextCentred(c);
}
private void drawTextCentred(Canvas c)
{
batteryLevelTextPaint.GetTextBounds(batteryLevelText, 0, batteryLevelText.Length, textBounds);
c.DrawText(batteryLevelText, centerX - textBounds.ExactCenterX(), centerY - textBounds.ExactCenterY(), batteryLevelTextPaint);
if (batteryStateText != null)
{
batteryStateTextPaint.GetTextBounds(batteryStateText, 0, batteryStateText.Length, textBounds);
c.DrawText(batteryStateText, centerX - textBounds.ExactCenterX(), centerY - textBounds.ExactCenterY() + 15, batteryStateTextPaint);
}
}
public void hls2rgb(double color_wheel_angle, double tlen, byte ilum,
ref byte color_r, ref byte color_g, ref byte color_b)
{
double rlum, rm1, rm2;
if (ilum > 255) ilum = 255;
if (ilum < 0) ilum = 0;
if (tlen > 1) tlen = 1;
if (tlen < 0) tlen = 0;
rlum = (double)ilum / 255;
if (rlum < 0.5)
rm2 = rlum * (1.0 + tlen);
else
rm2 = rlum + tlen - (rlum * tlen);
rm1 = (2.0 * rlum) - rm2;
if (tlen == 0)
{
color_r = ilum;
color_g = ilum;
color_b = ilum;
}
else
{
color_g = (byte)(value(rm1, rm2, color_wheel_angle + 120) * 255);
color_b = (byte)(value(rm1, rm2, color_wheel_angle) * 255);
color_r = (byte)(value(rm1, rm2, color_wheel_angle - 120) * 255);
}
return;
}
public double value(double r1, double r2, double angle)
{
double value;
angle = angle - 120;
if (angle > 360) angle = angle - 360;
if (angle < 0) angle = angle + 360;
if (angle < 60)
value = r1 + (r2 - r1) * angle / 60;
else if (angle < 180)
value = r2;
else if (angle < 240)
value = r1 + (r2 - r1) * (240 - angle) / 60;
else
value = r1;
return (value);
}
}
}

How can I get and wait for external process to complete?

I have rewritten a paginator to display header and footer for my documents. Everything seems to work until I save it as xps Document :
public void SaveAsXps(string fileName, FlowDocument document, string DocumentTitle, string DocumentFooter)
{
document.PageHeight = 1122.5 - 30;
document.PageWidth = 793.7 - 30;
using (Package container = Package.Open(fileName + ".xps", FileMode.Create))
{
using (XpsDocument xpsDoc = new XpsDocument(container, CompressionOption.Maximum))
{
XpsSerializationManager rsm = new XpsSerializationManager(new XpsPackagingPolicy(xpsDoc), false);
DocumentPaginator paginator = ((IDocumentPaginatorSource)document).DocumentPaginator;
paginator = new VisualPaginator(paginator, new Size(793.7, 1122.5), new Size(30, 30), DocumentTitle, DocumentFooter);
rsm.SaveAsXaml(paginator);
}
}
}
I give you the paginator :
public class VisualPaginator : DocumentPaginator
{
string m_DocumentTitle;
string m_DocumentFooter;
private Size pageSize;
private Size margin;
private readonly DocumentPaginator paginator;
private Typeface typeface;
public override Size PageSize
{
get { return pageSize; }
set { pageSize = value; }
}
public override bool IsPageCountValid
{
get
{
return paginator.IsPageCountValid;
}
}
public override int PageCount
{
get
{
return paginator.PageCount;
}
}
public override IDocumentPaginatorSource Source
{
get
{
return paginator.Source;
}
}
public VisualPaginator(DocumentPaginator paginator, Size pageSize, Size margin, string DocumentTitle, string DocumentFooter)
{
PageSize = pageSize;
this.margin = margin;
this.paginator = paginator;
m_DocumentTitle = DocumentTitle;
m_DocumentFooter = DocumentFooter;
this.paginator.PageSize = new Size(PageSize.Width - margin.Width * 2,
PageSize.Height - margin.Height * 2);
}
public void DrawFunction(DrawingVisual content, string drawContent, Point point)
{
try
{
using (DrawingContext ctx = content.RenderOpen())
{
if (typeface == null)
{
typeface = new Typeface("Times New Roman");
}
FormattedText text = new FormattedText(drawContent, CultureInfo.CurrentCulture,
FlowDirection.LeftToRight, typeface, 14, Brushes.Black,
VisualTreeHelper.GetDpi(content).PixelsPerDip);
Thread.Sleep(300);
ctx.DrawText(text, point);
}
}
catch (Exception)
{
throw;
}
}
public override DocumentPage GetPage(int pageNumber)
{
DocumentPage page = paginator.GetPage(pageNumber);
// Create a wrapper visual for transformation and add extras
ContainerVisual newpage = new ContainerVisual();
//Title
DrawingVisual pagetitle = new DrawingVisual();
DrawFunction(pagetitle, m_DocumentTitle, new Point(paginator.PageSize.Width / 2 - 100, -96 / 4));
//Page Number
DrawingVisual pagenumber = new DrawingVisual();
DrawFunction(pagenumber, "Page " + (pageNumber + 1), new Point(paginator.PageSize.Width - 200, paginator.PageSize.Height - 100));
//Footer
DrawingVisual pagefooter = new DrawingVisual();
DrawFunction(pagefooter, m_DocumentFooter, new Point(paginator.PageSize.Width / 2 - 100, paginator.PageSize.Height - 100));
DrawingVisual background = new DrawingVisual();
using (DrawingContext ctx = background.RenderOpen())
{
ctx.DrawRectangle(new SolidColorBrush(Color.FromRgb(240, 240, 240)), null, page.ContentBox);
}
newpage.Children.Add(background); // Scale down page and center
ContainerVisual smallerPage = new ContainerVisual();
smallerPage.Children.Add(page.Visual);
//smallerPage.Transform = new MatrixTransform(0.95, 0, 0, 0.95,
// 0.025 * page.ContentBox.Width, 0.025 * page.ContentBox.Height);
newpage.Children.Add(smallerPage);
newpage.Children.Add(pagetitle);
newpage.Children.Add(pagenumber);
newpage.Children.Add(pagefooter);
newpage.Transform = new TranslateTransform(margin.Width, margin.Height);
return new DocumentPage(newpage, PageSize, page.BleedBox, page.ContentBox);
}
}
The code execution leads to a break point due to external process unless I use a Thread.Sleep(300) instruction. I think the program has to wait for some external process to complete but I have no Idea what processes are involve here and what I can do to wait for them to fix the problem without using a Thread.Sleep() which is a very bad practice.
Any help or clues would be gladly appreciate.

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();
}
}
}

Capture screenshot of current web site in Universal Apps

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;
}

Luxand API in Visual Studio 2010?

How can I use Luxand API to get to work in visual studio 2010? I need to detect points of chin in a given face, can I do it with any other API?
I have tried this sample code:
OpenFileDialog openFileDialog1 = new OpenFileDialog();
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
FSDK.CImage image = new FSDK.CImage(openFileDialog1.FileName);
// resize image to fit the window width
double ratio = System.Math.Min((pictureBox1.Width + 0.4) / image.Width,
(pictureBox1.Height + 0.4) / image.Height);
image = image.Resize(ratio);
Image frameImage = image.ToCLRImage();
Graphics gr = Graphics.FromImage(frameImage);
FSDK.TFacePosition facePosition = image.DetectFace();
if (0 == facePosition.w)
MessageBox.Show("No faces detected", "Face Detection");
else
{
int left = facePosition.xc - facePosition.w / 2;
int top = facePosition.yc - facePosition.w / 2;
gr.DrawRectangle(Pens.LightGreen, left, top, facePosition.w, facePosition.w);
FSDK.TPoint[] facialFeatures = image.DetectFacialFeaturesInRegion(ref facePosition);
int i = 0;
foreach (FSDK.TPoint point in facialFeatures)
gr.DrawEllipse((++i > 2) ? Pens.LightGreen : Pens.Blue, point.x, point.y, 3, 3);
gr.Flush();
}
// display image
pictureBox1.Image = frameImage;
pictureBox1.Refresh();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Exception");
}
}
I get this error:
Could not load file or assembly 'xquisite.application.exe' or one of its dependencies. This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded.
what is your settings for your target? Any CPU ? Try x86 for Runtime !
have you add to your app.config <startup useLegacyV2RuntimeActivationPolicy="true"/>
?
These two thinks i forgot, were the reason of my errors which was the same as yours.
here is a piece of my code:
private void DetectFace()
{
var failerCounter = 0;
var cameraHandler = 0;
try
{
const int failerLimit = 2;
int failerLimitFaceDetection = Properties.Settings.Default.NotDetectedLimit;
float similarityMinimum = Properties.Settings.Default.SimilarityLimit;
var r = FSDKCam.OpenVideoCamera(ref CameraName, ref cameraHandler);
if (r != FSDK.FSDKE_OK)
{
MessageBox.Show(StringHelper.ErrorCamera);
}
FSDK.SetFaceDetectionParameters(
Properties.Settings.Default.DetectionHandleArbitaryRotations,
Properties.Settings.Default.DetectionDetermineFaceRotationAngle,
Properties.Settings.Default.DetectionInternalResizeWidth);
FSDK.SetFaceDetectionThreshold(Properties.Settings.Default.DetectionFaceDetectionThreshold);
while (IsFaceDetectionActive)
{
var imageHandle = 0;
if (FSDK.FSDKE_OK != FSDKCam.GrabFrame(cameraHandler, ref imageHandle))
{
Application.Current.Dispatcher.Invoke(delegate { }, DispatcherPriority.Background);
continue;
}
var image = new FSDK.CImage(imageHandle);
var frameImage = image.ToCLRImage();
FaceContent = frameImage;
var gr = Graphics.FromImage(frameImage);
var facePosition = image.DetectFace();
IsFaceDetected = facePosition.w != 0;
if (!IsFaceDetected)
{
if (failerCounter++ > failerLimitFaceDetection)
{
failerCounter = 0;
OnFaceNotDetected();
}
}
// if a face is detected, we detect facial features
if (IsFaceDetected)
{
var facialFeatures = image.DetectFacialFeaturesInRegion(ref facePosition);
SmoothFacialFeatures(ref facialFeatures);
FaceTemplate = image.GetFaceTemplate();
// Similarity = 0.5f -> fin the right value ....
IsFaceRecognized = FaceMetricHandler.LooksLike(FaceTemplate, similarityMinimum).Any();
if (IsFaceRecognized)
{
foreach (var match in FaceMetricHandler.LooksLike(FaceTemplate, similarityMinimum))
{
failerCounter = 0;
GreetingMessage = match.Name;
IsFaceDetectionActive = false;
OnFaceRecognized();
break;
}
}
else
{
if (failerCounter++ > failerLimit)
{
failerCounter = 0;
IsFaceDetectionActive = false;
OnFaceNotRecognized();
}
}
if (IsFaceFrameActive)
{
gr.DrawRectangle(Pens.Red, facePosition.xc - 2*facePosition.w/3,
facePosition.yc - facePosition.w/2,
4*facePosition.w/3, 4*facePosition.w/3);
}
}
else
{
ResetSmoothing();
}
FaceContent = frameImage;
GC.Collect();
Application.Current.Dispatcher.Invoke(delegate { }, DispatcherPriority.Background);
}
}
catch(Exception e)
{
logger.Fatal(e.Message);
InitializeCamera();
}
finally
{
FSDKCam.CloseVideoCamera(cameraHandler);
FSDKCam.FinalizeCapturing();
}
}
BTW, you can use x64 with win64\FaceSDK.NET.dll

Categories