在Web应用程序中使用WPF程序集是否可以?

Chr*_*ris 7 asp.net wpf asp.net-mvc

我有一个针对.NET 4的ASP.NET MVC 2应用程序,需要能够动态调整图像大小并将其写入响应.

我有代码,这样做,它的工作原理.我正在使用System.Drawing.dll.

但是,我想增强我的代码,这样不仅可以调整图像大小,而且还可以将其从24bpp降低到4bit灰度.在我的生活中,我无法找到有关如何使用System.Drawing.dll执行此操作的代码.

但我确实发现了一堆WPF的东西.这是我的工作/示例代码(在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);
}
Run Code Online (Sandbox Code Playgroud)

它使用System.Xaml.dll,WindowsBase.dll,PresentationCore.dll和PresentationFramework.dll.使用的名称空间是:System.Windows.Controls,System.Windows.Media和System.Windows.Media.Imaging.

在我的Web应用程序中使用这些命名空间有什么问题吗?这似乎不对.

如果有人知道如何在没有所有这些WPF的东西(我几乎不理解,BTW)下降深度,我会很高兴看到它.

Ray*_*rns 7

没问题.您可以从ASP.NET网站轻松使用WPF进行图像处理.我曾经多次在网站的幕后使用过WPF,效果很好.

我遇到的一个问题是WPF的许多部分坚持调用线程是STA线程.如果您的网站使用MTA线程,则会收到错误消息,告知您WPF需要STA线程.要解决此问题,请使用我在此答案中发布的STAThreadPool类.