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.
Related
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.
Currently I'm trying to do create an out of browser application that captures the whole screen. Not just the Silverlight application.
Now I know that this is not possible with plain Silverlight & C#, but you need to do some dllimports and stuff, it needs to run OOB and you need an elevated application. That is no problem at all.
I found some code on the internet, but I'm stuck. Since Silverlight doesn't have a System.Drawing namespace and thus no Bitmap. Now I can't cast my GDI+ pointer to something I can display in my Silverlight application.
The code I'm using is from this link: http://www.codeproject.com/KB/cs/DesktopCaptureWithMouse.aspx
Can anybody kick me in the right direction? ;)
Seems that it is not possible without a lot of hacking with native code.
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.
I'm new in Silverlight, and I'm looking for something like a drawing brush on Silverlight without success. I saw Tamir Khason blog on building a drawing brush in Silverlight and i download the code without any success
i'm taking about the next link:
http://www.codeproject.com/KB/silverlight/Silverlight-DrawingBrush.aspx
Can someone find the way to implement the DrawingBrush???
Thanks.
Eyal
There is no direct support.
You need to write code with the WritableBitmap class available in Silverlight 3.
We were able to create a VisualBrush kind of thing using the WritableBitmap.
When working with Windows forms and images, I deal with System.Drawing.Image descendats.
When I work with WPF I deal with System.Windows.Media.Imaging.BitmapSource class.
Is there a UI framework independent way of referencing a memory image?
Maybe it would be a Intptr or byte array?
Thank you.
You should be able to get them both to work with a MemoryStream pointing at a byte array (makes sense to me anyway!)
Haven't tried the WinForms way, but you can certainly do that with WPF.