Can I use WPF assemblies in a web application?

I have an ASP.NET MVC 2 application for targeting .NET 4 that should be able to resize images on the fly and write them in response.

I have a code that does this and it works. I am using System.Drawing.dll.

However, I want to improve my code to not only resize the image, but I drop it from 24bpp to 4 shades of gray. I could not, for life, find code to do this with System.Drawing.dll.

But I found a bunch of WPF stuff. This is my working / sample code (executed in LinqPad).

// Load the original 24 bit image var bitmapImage = new BitmapImage(); bitmapImage.BeginInit(); bitmapImage.UriSource = new Uri(@"C:\Temp\Resized\18_appa2_015.png", UriKind.Absolute); //bitmapImage.DecodePixelWidth = 600; bitmapImage.EndInit(); // Create the destination image var formatConvertedBitmap = new FormatConvertedBitmap(); formatConvertedBitmap.BeginInit(); formatConvertedBitmap.Source = bitmapImage; formatConvertedBitmap.DestinationFormat = PixelFormats.Gray4; formatConvertedBitmap.EndInit(); // Encode and dump the image to disk var encoder = new PngBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(formatConvertedBitmap)); using (var fileStream = File.Create(@"C:\Temp\Resized\18_appa2_015_s2.png")) { encoder.Save(fileStream); } 

It uses System.Xaml.dll, WindowsBase.dll, PresentationCore.dll and PresentationFramework.dll. Namespaces used: System.Windows.Controls, System.Windows.Media and System.Windows.Media.Imaging.

Is there a problem using these namespaces in my web application? This does not seem right.

If someone knows how to reset the bit depth without all this WPF stuff (which I hardly understand, BTW), I would be delighted with this.

+7
asp.net-mvc wpf
source share
2 answers

No problems. You can easily use WPF to process images from the ASP.NET website. I have used WPF behind the scenes inside the website several times before and it works great.

The only problem I encountered is that many parts of WPF insist that the calling threads are STA threads. If your website uses MTA streams, you will receive an error message indicating that WPF needs STA streams. To fix this, use the STAThreadPool class that I posted in this answer .

+7
source share

My understanding (and I can't find the link right now) is that this is not officially supported. However, in practice, this works very well, and you will not be alone in using these libraries in a web application. Also, even if you can find a way to do this using System.Drawing, I find that officially this is also not supported in the web environment - although it is more widely used in this environment than WPF, which gives you an extra layer of reinsurance.

+1
source share

All Articles