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.
Chris
source share