图像大小调整性能:System.Drawing中VS System.Windows.Media [英] Image Resizing Performance: System.Drawing vs System.Windows.Media

查看:171
本文介绍了图像大小调整性能:System.Drawing中VS System.Windows.Media的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个情况我需要调整大量的图像。这些图像存储作为目前文件系统上的.jpg文件,但我想到刚有字节[]在内存后来在该项目。源图像尺寸是可变的,但该输出应为3不同的预定尺寸。宽高比应予以保留,填充白色空间(即那座图像将被调整大小以适应多目标图像尺寸范围内,在左侧和右侧的白色大面积)的原始图像。

I've got a situation where I need to resize a large number of images. These images are stored as .jpg files on the file system currently, but I expect to just have byte[] in memory later on in the project. The source image size is variable, but the output should be 3 different predetermined sizes. Aspect ratios should be preserved, padding the original image with white space (ie, a really tall image would be resized to fit within the square target image size, with large areas of white on the left and right).

我初步建成面向项目NET 2.0,并使用System.Drawing中的类来执行负载/调整/保存。相关的代码包括:

I initially built the project targeting .NET 2.0, and using System.Drawing classes to perform the load/resize/save. Relevant code includes:

original = Image.FromFile(inputFile); //NOTE: Reused for each of the 3 target sizes
Bitmap resized = new Bitmap(size, size);
//Draw the image to a new image of the intended size
Graphics g = Graphics.FromImage(resized);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.Clear(Color.White);
g.DrawImage(original, center - width / 2f, center - height / 2f, width, height);
g.Dispose();
//Save the new image to the output path
resized.Save(outputFile, ImageFormat.Jpeg);



我想端口这个项目.NET 3.5,使用System.Windows.Media类,所以尝试以执行相同的功能。我懂了工作,但性能是可怕的;每幅图像的处理时间大约是50倍更长的时间。绝大多数的时间都花在加载图像。相关的代码包括:

I wanted to port this project to .NET 3.5, so tried using the System.Windows.Media classes to perform the same function. I got it working, however performance is terrible; processing time per image is about 50x longer. The vast majority of the time is spent loading the image. Relevant code includes:

BitmapImage original = new BitmapImage(); //Again, reused for each of the 3 target sizes
original.BeginInit();
original.StreamSource = new MemoryStream(imageData); //imageData is a byte[] of the data loaded from a FileStream
original.CreateOptions = BitmapCreateOptions.None;
original.CacheOption = BitmapCacheOption.Default;
original.EndInit(); //Here's where the vast majority of the time is spent
original.Freeze();

// Target Rect for the resize operation
Rect rect = new Rect(center - width / 2d, center - height / 2d, width, height);

// Create a DrawingVisual/Context to render with
DrawingVisual drawingVisual = new DrawingVisual();
using (DrawingContext drawingContext = drawingVisual.RenderOpen())
{
    drawingContext.DrawImage(original, rect);
}

// Use RenderTargetBitmap to resize the original image
RenderTargetBitmap resizedImage = new RenderTargetBitmap(
    size, size,                         // Resized dimensions
    96, 96,                             // Default DPI values
    PixelFormats.Default);              // Default pixel format
resizedImage.Render(drawingVisual);

// Encode the image using the original format and save the modified image
SaveImageData(resizedImage, outputFile);



难道我在这里做得不对,要采取这么多的时间?我刚刚使用,需要一个URI,同样的性能问题那里的BitmapImage构造尝试。任何人做过这样的事之前,知道如果有一个更高性能的头脑办法做到这一点?或者,我只是将需要仍然使用System.Drawing中?谢谢!

Am I doing something wrong here, to take so much time? I've tried just using the constructor on BitmapImage that takes a URI, same performance issue there. Anyone done anything like this before, know if there's a more performance-minded way to do this? Or am I just going to need to use System.Drawing still? Thanks!

推荐答案

和打字一切后,它发生,我认为我可以从MS加载符号系统。 Windows.Media类和步它是缓慢的。立刻发现的原因,并且将溶液。输入图像保存用颜色特征文件,并将其试图加载每个图象的颜色分布(从文件系统)。通过从BitmapCreateOptions.None上面代码切换到BitmapCreateOptions.IgnoreColorProfile,它不再这样做,并执行一样快System.Drawing中一样。

And after typing all that up, it occurred to me that I could load the symbols from MS for the System.Windows.Media classes, and step through where it was slow. Immediately found the cause, and the solution. The input images were saved with a color profile, and it was attempting to load that color profile (from the file system) of each image. By switching from BitmapCreateOptions.None to BitmapCreateOptions.IgnoreColorProfile in the code above, it no longer does that, and performs just as fast as System.Drawing did.

希望这有助于其他任何人运行到这个问题!

Hope this helps anyone else that runs into this problem!

这篇关于图像大小调整性能:System.Drawing中VS System.Windows.Media的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆