Converting Silverlight 4 InkPresenter omits uploaded images when converted to WriteableBitmap - silverlight-4.0

I have a web application that incorporates a Silverlight drawing pad. The strokes are saved as XAML in a database. Additionally, any text or moveableimage objects are also saved in the XAML. The moveableimage objects have as their source images being served from the same server and same domain as the Silverlight application.
In order to perform a scale up on the sketch pad contents, I use a WriteableBitmap:
ScaleTransform transform = new ScaleTransform();
transform.ScaleX = (double)10;
transform.ScaleY = (double)10;
WriteableBitmap fullSizeBinary = new WriteableBitmap(workCanvas, transform);
string fullSizeBinaryString = XamlHelper.GetBase64Jpg(fullSizeBinary);
XamlHelper is a custom class using ImageTools to convert the WriteableBitmap to JPEG. I have tried with and without the transform. The WriteableBitmap omits the child moveableimage objects and the resulting JPEG has all the strokes and text objects, but the moveableimage objects do not render. I have tried a variety of things thinking it was security, memory, distinct controller paths, etc. Nothing. Been banging my head against this for hours and I can't figure it out.
When the method containing the above code is invoked by the user clicking a button inside the Silverlight application, it works perfectly. When the method containing the above code is invoked programmatically (i.e., through a parameter passed into the Silverlight application from an MVC controller) it doesn't work.

Related

Databinding uri source of imagebrush

In my application i am binding several properties to a custom user control, and everything works fine, except the images are not showing. For binding i have used the following codes:
Categories.Add(new Models.Category { Name = "Pizza", Count= 4, ImageUri = new Uri("Images/pizza.png", UriKind.Relative) });
I have also tried with different urikinds but the images are never showing.
what could go wrong? The images are in my solutions Images folder.
Use the ms-appx URI scheme:
ImageUri = new Uri("ms-appx:///Images/pizza.png");
Take care to really write ///.
Also make sure that the Build Action of the image file is set to Content, as pointed out in the other answer. Setting Copy to Output Directory does however not seem to be necessary.
Be sure you set the properties of the image so that it is available during runtime. Sometimes if employing Design Time data in Blend, you will see the images in Design Time, but then nothing during run time. The reason is that the images were never deployed with the rest of the solution. Be sure the Build Action on each image is set to Content and I usually set the Copy to Output Directory to Copy if Newer.

Is it possible to tell a Windows 8.1 app not to scale up specific elements?

I am referencing external image urls for the source property of an image element in my app.
I have 3 versions of the image at 100, 140 and 180 scales e.g.
myimage.scale-100.jpg
myimage.scale-140.jpg
myimage.scale-180.jpg
If the images lived in the app you would normally put the source like the following and Windows works out which image to load based on the resolution scale of the device:
ms-appx:///Assets/Images/myimage.jpg
However as my 3 images live externally I am having to work out the resolution scale and then build up the correct source string so that the correct image is loaded e.g:
http://www.mywebsite.com/myimage.scale-180.jpg
This works, however, windows is taking my image e.g. the 180 scale one myimage.scale-180.jpg, and then scaling it up by a further 180%, it doesn't know that I have loaded an image at the correct 180% scale already and that it doesn't need to scale it up!
Is there a way of telling it not to scale up specific image elements?
Update (added code):
The xaml image element:
<Image Source="{Binding Image, Converter={StaticResource ImageToExternalImagePathConverter}}" HorizontalAlignment="Left" VerticalAlignment="Top" Stretch="None" />
The converter used on the image element to determine the external image path (it works out the scale and uses the binding to build up the correct string).
public object Convert(object value, Type targetType, object parameter, string language)
{
ResolutionScale resolutionScale = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().ResolutionScale;
string ImageScale = ".scale-100";
switch (resolutionScale)
{
case ResolutionScale.Scale140Percent:
ImageScale = ".scale-140";
break;
case ResolutionScale.Scale180Percent:
ImageScale = ".scale-180";
break;
}
//builds up the correct string e.g. http://www.mywebsite.com/myimage.scale-180.jpg
string externalPath = "http://www.mywebsite.com/" + (string)value + ImageScale + ".jpg";
return externalPath;
}
Update(added reference):
To further explain what I am currently doing see this link:
http://msdn.microsoft.com/en-us/library/windows/apps/hh465362.aspx
Manually load images based upon scale percentage at runtime If your
app is loading images at runtime using code, for example if you use
DirectX directly, not XAML or HTML to create your UI, use the
DisplayProperties.ResolutionScale property to determine the scale and
manually load images based upon scale percentage.
So that's what I am doing, but the problem is that Windows is scaling up the UI as it should to 140% or 180%, and that includes my manually loaded image. So if I manually load in an image that is already sized at 140%, it gets scaled up by Windows anyway and it DOES get physically bigger. If the images lived in the app package this problem wouldnt exist because Windows recognises the filename identifiers and doesnt scale them up (see below)
Use resource loading for bitmap images in the app package For bitmap
images stored in the app package, provide a separate image for each
scaling factor(100%, 140%, and 180%), and name your image files using
the "scale" naming convention described below. Windows loads the right
image for the current scale automatically.
How can I replicate the same behavior from Windows for images that live in the app package but for images that are external to the app and are loaded manually? Logically at the point at which I load the image in (code), I want to say to Windows, this image is already scaled correctly, don't upscale it by the resolution factor.
Try sizing grip property to false

How to save Image Control to local file in Windows 8?

I'm writing Client-Server application, in which I get item with Image Url. This Url is binded with Image control:
<Image Source{Binding ImageUrl} x:Name="img_avatar" Stretch="UniformToFill" Width="48" Height="48"/>
I want to save the image from Image.Source to the local folder without any methods which are using download operations.
Please help m someone !!!
There is no way to do what you ask about. When you set an Image control's source to a Uri - it gets converted to a BitmapImage which doesn't expose any API that would let you save the file or even extract a bitmap. If you want to avoid multiple downloads of the image you can download it as a file once and load a BitmapImage from the file instead of Uri.
I believe with a little bit of reflection hackery, it should be doable. But a lot better solutions exist, so yeah..
Either subclass Image or write attached property to handle logic. All you have to do, is just load image yourself into MemoryStream and create BitmapImage manually(behind subclassed Image or attached proeprty).
This way you can expose new property that gives direct access to MemoryStream, and from there you can save file to HDD without wasting precious memory.
Bonus points if you can also keep the Source="{Binding}" syntax while providing this functionality.

Monogame texture loading NullReferenceException

I have a working game where I am loading all the textures from a different class. It works perfectly fine for the PC version.
Now, I am trying to port it to win8 using Monogame.
If I load a texture from Game1.cs using .xnb file it works absolutely fine. However, when I try to load the same texture by using a different class, it doesn't. It gives me an NullReferenceException error on
GraphicsDevice.Clear(Color.Cornflowerblue);
HungryCoder:
I don't know how you are loading the texture from the class, however, right now MonoGame doesn't yet have a Content Pipeline like you were provided with in building and XNA Game for the PC (they are working on it), therefore, you cannot add graphic files in the same manner.
In my walkthrough of building a Windows 8 XNA game, I create my Shooter player graphic from my own Player class by passing into my Initialize function within the class the Content.Load<> with the Texture type and location:
player.Initialize(Content.Load("Graphics\player"), playerPosition);
Note the .xnb file is located in a folder Graphics within my project.
In my background, I have also created a parallaxing background from a my background class by passing in the full Content Manager to my Initialize method of my Background class as well.
bgLayer1 = new ParallaxingBackground();
bgLayer1.Initialize(Content, "Graphics\bgLayer1", GraphicsDevice.Viewport.Width, -1);
Both of these examples work within my project/game.
If you interested have posted the player code example as a part of blog tutorial series on MonoGame on Windows 8. As I complete the code for the background, I will also add the full code example for the background as well. Hope this helps.
http://blogs.msdn.com/tarawalker

How efficient is XAML parsing in WinRT / Win8?

When creating UserControls, it looks like the XAML is being parsed every time the control is initialized.
For example, when I create a UserControl, there's auto-generated code to initialize the component that looks like this:
public void InitializeComponent()
{
if (_contentLoaded)
return;
_contentLoaded = true;
global::Windows.UI.Xaml.Application.LoadComponent(this, new global::System.Uri("ms-appx:///Views/MyView.xaml"), global::Windows.UI.Xaml.Controls.Primitives.ComponentResourceLocation.Application);
}
This existed in Silverlight as well.
Creating the control once or twice is not a big deal, but if I have a large visual tree in each UserControl with Visual-States and bindings, and I'm creating it many times per application lifecycle, it would make sense to build the visual controls using C# to boost performance.
So, my question is:
Does the parser/framework "remember" the XAML file and avoid re-parsing it again on subsequent calls? That is, does it create a binary representation of the XAML so it doesn't have to read text all over again?
It's my understanding that XAML gets compiled into a binary form as a resource within your application. The runtime does not have to parse the text of the .xaml file, just as it does not have to parse your .cs code files.
The performance of instantiating the classes as declared with XAML is supposed to be on par with creating it in code.
Windows 8.1 xaml finally added XAML binary format :)
XAML Binary Format: The final signed appx will no longer contain text based markup as it will get converted into Binary.