Can not convert some Silverlight control to Image

Dec 30, 2010 at 10:18 AM

I using ImageTool to convert a Grid to Image, It can work fine, but some controls in Grid can not converted, ex: Textbox, CheckBox, RadioButton...

When I try convert a Textbox, It throw a exception like this

Postcondition failed: !IsFilled || Contract.Result<int>() > 0

My convert code like this: var img = printElement.ToImage(); // printElement is a Textbox.

So, I very appreciate if someone can help me.

Thank you.

Coordinator
Dec 30, 2010 at 10:31 AM
Hi BruceDo,
this is new for me,
can you try to convert the textbox to a writeablebitmap and see what
happens? How large is the resulting image?

Regard,
sebastian
Jan 4, 2011 at 6:13 AM
Edited Jan 4, 2011 at 9:02 AM

I can convert to WriteableBitmap, but there's no pixel in property "Pixels    {int[0]}    int[]".

I don't know why, please help me this issue.

There are many dll in ImageToolPakage, and I just use 3 of them: ImageTools, ImageTools.IO.Jpeg, ImageToolsUtils. Do I need add any dll.

I create a simple sample project and it can convert fine.

But in actual project, I use Prism to load views and also use Activator to create view. I think the problem here.

In each view implemented a interface return a UIElement (or FrameworkElement), and at PrintView, I collect all UIElement by create instance of Views () to print.

So, I very appreciate if someone can help me.

Thank you

Jan 4, 2011 at 11:52 AM
Edited Jan 4, 2011 at 12:52 PM

This is sample code that throw exception

 var txt = new TextBox();

 var img = txt.ToImage(); //Postcondition failed: !IsFilled || Contract.Result<int>() > 0

I wonder why I can do like this with TextBlock, but can not do with other control?.....

Thank you.

Jan 5, 2011 at 2:17 AM

I so tired to solve this problem, but until now I don't have any solution yet.

I'm very appreciate if someone can help me solve this issue. I took two days to find solution and nothing.

I think any experience person know what's happening in my case.

Thank you

 

Apr 28, 2011 at 8:01 AM

Hi,

I resolved it by giving an explicit height and width to the control. In my case it was a canvas with children. When I set the width and height for canvas it worked.

Coordinator
Apr 28, 2011 at 6:12 PM
This is normal and More a problem of the writeable bitmap class

Von: abhilashkk
Gesendet: Donnerstag, 28. April 2011 09:01
An: sebastianstehle@gmx.de
Betreff: Re: Can not convert some Silverlight control to Image [imagetools:239987]

From: abhilashkk

Hi,

I resolved it by giving an explicit height and width to the control. In my case it was a canvas with children. When I set the width and height for canvas it worked.

May 17, 2011 at 11:36 AM

Am I crazy!? how does this work

 var txt = new TextBox();

 var img = txt.ToImage();

all I get is :  'System.Windows.Controls.TextBox' does not contain a definition for 'ToImage' and no extension method 'ToImage' accepting a first argument of type 'System.Windows.Controls.TextBox' could be found (are you missing a using directive or an assembly reference?)

same is with all other controls.

where do you get ToImage() function from!?

And can someone tell me how to convert canvas to image?

Example on the front page does not compile.

Coordinator
May 23, 2011 at 8:26 AM

Read this: http://imagetools.codeplex.com/wikipage?title=Write%20the%20content%20of%20a%20canvas%20to%20a%20file

 

You need to import ImageTools.Utils to get this extension method.

Nov 27, 2012 at 1:04 PM
Edited Nov 27, 2012 at 1:19 PM

OK, I found a solution!! Or at least why it I get the error "Postcondition failed: !IsFilled || Contract.Result<int>() > 0"

Looking at the following code:

 

/* register the JPG and PNG encoder so the file extensions are supported */
Encoders.AddEncoder<JpegEncoder>();
Encoders.AddEncoder<PngEncoder>();

/* wait till the control is actually loaded in the front-end */
OuterBorder.Loaded += (s, e) => {
    /* convert to an 'ExtendedImage', write it as a stream to the file specified by the user */
    ImageExtensions.ToImage(OuterBorder).WriteToStream(saveDlg.OpenFile(), saveDlg.SafeFileName);

    /* close the HACK window */
    PreviewControlWindow.Close();

    /* we close the window before it can animate, so this is another HACK?! to circumvent the window */
    Application.Current.RootVisual.SetValue(Control.IsEnabledProperty, true);
};

/* show the control so it can be "Loaded" */
PreviewControlWindow.Execute(OuterBorder);

 

I don't know how else to resolve this, but if I comment out the HACKS, I get the error or a partial or blank or black output.

The results otherwise are great, these are silverlight limitations, which I would love to know how else to resolve.

Nov 25, 2013 at 2:05 AM
Apply this .net image converter which can convert any image or document form to commonly used image format without affecting the original image contents contained, like Textbox, CheckBox, RadioButton, charts, graphs.