많은 수의 이미지의 크기를 조정해야하는 상황이 있습니다. 이 이미지는 현재 파일 시스템에 .jpg 파일로 저장되어 있지만 나중에 프로젝트의 메모리에 byte []가 있어야합니다. 원본 이미지 크기는 가변적이지만 출력은 미리 정해진 3 가지 크기 여야합니다. 가로 세로 비율은 원본 이미지를 공백으로 채 웁니다 (즉, 높이가 큰 이미지는 정사각형 타겟 이미지 크기에 맞도록 크기가 조정되며 왼쪽 및 오른쪽에 흰색이 넓게 나타납니다).이미지 크기 조정 성능 : System.Drawing 대 System.Windows.Media
처음에는 .NET 2.0을 대상으로 프로젝트를 빌드하고 System.Drawing 클래스를 사용하여로드/크기 조정/저장을 수행했습니다. 관련 코드가 포함
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 배 더 길다. 대다수의 시간은 이미지를로드하는 데 소비됩니다. 관련 코드는 다음을 포함합니다 :
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을 사용해야할까요? 감사!
팁 주셔서 감사합니다! – Jan