Alternative to System.Drawing.Bitmap in Silverlight which is compatible with WPF - wpf

I'm trying to cross compile some libraries for WPF and Silverlight.
Currently one of the libraries depends on System.Drawing.Bitmap which is not available in Silverlight.
It's a class that represents a device specific image format,
and holds a reference to a Bitmap instance in order to display this image
in a WPF application.
What could I use instead of the Bitmap class in order to hold a reference
to an Image which I can display in both silverlight and wpf ? (cannot be file based, must be in memory).

System.Windows.Media.Imaging for WPF and Silverlight. This one shows how to work with it in memory: Silverlight 4.0: How to convert byte[] to image?

The closest you will get is the WriteableBitmap class but these classes a quite different between frameworks. If you are looking to create a common library to use in both your WPF and Silverlight applications you will probably need to create an abstract that your applications code to and then write two different implementations of the abstract.
If both chunks of code return something tha,t at least lexically, is a "WriteableBitmap" then you might be able to integrate that into common code.
Worth looking at for the Silverlight side of the implementation would be WriteableBitmapEx.

Related

Exporting an image struct from native code to managed

I have a native codebase that creates an image every 50 ms. I want to use WPF to render this image into a WPF GUI view.
The image is an hbitmap constructed from a data structure of type boost::numeric::ublas::matrix. i.e. I start with a matrix of float values, then I create a HBITMAP, and then draw this HBITMAP using the device context of MFC. But now, I want to replace my MFC GUI with a WPF GUI, because it looks nicer. I plan to use C++/CLI to achieve the interop.
My question is as follows : How should I setup the transfer of my image across the interop boundary?
Should I transfer it over while it is still a structure of floats, or shall I transfer after I created the bitmap? I have heard marshaling is a big drain on efficiency, and so must be handled very carefully. Is there a type that is common to managed and native world, that can be used for this?
I am a complete newbie to interoping, any other related resources you share will be greatly appreciated.
Or, is there a way to completely avoid the transfer? - by drawing from native code into a WPF GUI view
My application is very performance sensitive, any alternative you suggest that achieves good performance will be greatly appreciated.
I've been through the same options and found that - at least for .NET 4.5 - the InteropBitmap is the best way to go, as long as your image stays the same size (e.g. for something like video streaming).
In brief, you create a memory mapped file (either in using C++/CLI or C# with p/invoke), and use that as the source of pixel data. The InteropBitmap is created over that same memory mapped file, and you can use it as an ImageSource in WPF.
In my application, a background thread updates the MMF and invalidates the InteropBitmap and the WPF front end just binds to the image source as normal.

Best practice for using resources in a WPF project

I know and use two methods to store and access resources on application scope:
Properties\Resources.resx
create a folder and place for example images there, setting their build mode to Resource
What is the difference between the two methods in terms of performance and complexity, when should each be used and how are the resources best consumed in WPF and VB or C# code in each way?
Thanks in advance,
Julian
The "natural" way of referencing resources like images in a WPF project is your second option. You can use a relative URI to point to the image and WPF will lazy load it. You can reference resources in other assemblies using pack URI syntax.
Using Resources.resx will code-generate properties that loads resources when referenced. Resources can be strings, images, icons or a byte arrays. Using {x:Static} in XAML allows you to reference the static properties generated by the code-generator but often you will need a converter to convert the resource type into a type usable by WPF.
There is some support for localization using Resources.resx and if you want to provide a multi-lingual application you could store the translated strings in Resources.resx. However, WPF localization as described by Microsoft is not based on Resources.resx.
For images, the second option is much easier. For strings, the first option is probably easier but instead you could stay in XAML and create a ResourceDictionary.

Silverlight 4 WriteableBitmap to Bitmap

This is my first Silverlight app and my first go at C#. I have a C# class library that I access from Silverlight using COM. The C# library has a method that takes a Bitmap as an argument, however from what I can see Silverlight only has a WritableBitmap. Is there a way to convert a WritableBitmap to a Bitmap in Silverlight? Some other answers I have read give functions for the conversion, but the functions all return a Bitmap, which obviously throws an error when I try and build. Can anyone help?
Finally got it working by converting the WritableBitmap into a byte array, passing it through and then building the bitmap again on the other side.
I want to make sure I am understanding you correctly, you are using Silverlight to talk to a C# windows library using COM, the c# libraru is using classes that Silverlight does not have any references to, correct?
If this is the case, I would immediately look into using WPF instead of Silverlight, can you do this? The Bitmap class is really just a wrapper for a GDI+ image, which is why you wont really be able to use it in Silverlight.

Guidelines for Sharing code between Silverlight and WPF

I want to share as much code as possible between Silverlight and WPF.
My lowest level handles the difference between calling the database directly and calling a web service.
My data layer on top of that is compiled with both .NET and Silverlight.
My GUI layer is currently designed as a WPF Control Library.
For sivlerlight...
Is it possible to reuse my XAML for my silverlight class library?
Do I need to create a "Silverlight Application" or can I just display controls from my class library directly?
What things do I need to be careful about? (For example, I already learned that Silverlight can't use DataTables in web service calls.)

Is it possible to use Kit3D in WPF? If so, how?

I have a Silverlight application that uses Kit3D and I want to convert it to WPF.
How would I do that?
Should I do that?
You could try downloading the source code from its Codeplex repository and recompiling with WPF as the target. No guarantees this will work without modifications, though, since WPF is not a complete superset of Silverlight. But according to the author's description on his web page, Kit3D is modeled after WPF's built-in Media3D namespace, so you could just use that directly rather than bother with a 3rd-party library.

Resources