Perhaps I'm doing it wrong but I don't think WPF or GDI+ classes are intended to process large images on a server. I have an app that needs to transform many large tiff files to different formats and sizes. Thumbnails and previews of these files are generated with the wpf classes and cached on disk so they are not a big deal.
My problem开发者_开发百科 comes with the other higher res transformations which are not being cached atm. I'm thinking of using ImageMagick to replace the wpf for this part and see if there's a performance gain but while I'm at it I'd like to see if you guys know of an alternative to the wpf an gdi+ classes to process large image files.
Well, which one are you using (which classes)? The Windows Imaging Component (WIC) as available through PresentationCore
and WindowsBase
are quite capable, if used properly.
I wouldn't go with GDI+ i.e. System.Drawing because that's legacy and slow.
If you just need raw processing power have your tried scaling out and distributing the work load? It's can be done quite easily (to some extent) even without investing in new hardware.
This is my main method with timers and all, it appears that the WIC stuff needs roughly 3 seconds to boot the first run takes about 3 seconds longer than the rest. The timings are then fairly consistent. I'm memory buffering to avoid the time it might take to pull or put the file from or to disk.
for (int i = 0; i < 20; i++)
{
var total = new Stopwatch();
var read = new Stopwatch();
var process = new Stopwatch();
total.Start();
using (var inputStream = new FileStream(@"C:\Projects\ConsoleApplication6\ConsoleApplication6\monster.jpg", FileMode.Open))
{
read.Start();
var bytes = new byte[inputStream.Length];
inputStream.Read(bytes, 0, bytes.Length);
read.Stop();
process.Start();
var bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = new MemoryStream(bytes);
bitmapImage.DecodePixelWidth = 800;
bitmapImage.EndInit();
using (var outputStream = new MemoryStream())
{
var jpegEncoder = new JpegBitmapEncoder();
var frame = BitmapFrame.Create(bitmapImage);
jpegEncoder.Frames.Add(frame);
jpegEncoder.Save(outputStream);
process.Stop();
File.WriteAllBytes(@"C:\Projects\ConsoleApplication6\ConsoleApplication6\monster" + i + ".jpg", outputStream.ToArray());
}
}
total.Stop();
Console.WriteLine("{0:0.000} ms ({1:0.000} ms / {2:0.000} ms)", total.Elapsed.TotalMilliseconds, read.Elapsed.TotalMilliseconds, process.Elapsed.TotalMilliseconds);
}
精彩评论