2017-02-12 41 views
2

我需要縮小包含文本的多個圖像。由於文本需要縮小,以保留文本的銳利邊緣而不平滑。我的第一次嘗試如下:C#WPF需要快速縮小包含文本的圖像

RenderOptions.SetBitmapScalingMode(upgradeCard, BitmapScalingMode.HighQuality); 
upgradeCard.Height(resizedHeight); 
upgradeCard.Width(resizedWidth); 

結果太模糊,文本很難閱讀。然而,它確實非常快。然後我試了一下:

public static class ImageResizer 
{ 
    public static Image Resize(Image image, Size size) 
    { 
     if (image == null || size.IsEmpty) 
      return null; 

     var resizedImage = new Bitmap(size.Width, size.Height, image.PixelFormat); 
     resizedImage.SetResolution(image.HorizontalResolution, image.VerticalResolution); 

     using (var graphics = Graphics.FromImage(resizedImage)) 
     { 
      var location = new Point(0, 0); 
      graphics.CompositingMode = CompositingMode.SourceCopy; 
      graphics.CompositingQuality = CompositingQuality.HighQuality; 
      graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; 
      graphics.SmoothingMode = SmoothingMode.None; 
      graphics.DrawImage(image, new Rectangle(location, size), 
           new Rectangle(location, image.Size), GraphicsUnit.Pixel); 
     } 

     return resizedImage; 
    } 
} 

這個效果非常好,幾乎和Photoshop Bicubic Sharper一樣好。不幸的是它也很慢。我需要的方式太慢了。

是否有任何其他方式可以產生第二種方法的結果,但速度相當快?

+0

你可以看到這個[博客](https://blogs.msdn.microsoft.com/dotnet/2017/01/19/net-core-image-processing/),它比較了幾個圖像處理庫性能和調整後的圖像質量。但結果並不令人驚訝 - 它們要麼速度很快,要麼產生高質量的圖像,但一次不能同時出現。 –

回答

0

沒有您的圖像的例子很難給出可靠的建議。

例如,您的圖像中已有多少對比度?你減少他們的因素是什麼?

您可以嘗試最近的鄰域縮放比例,它可以非常快,然後嘗試使用高斯濾波器或類似方法將輸出稍微模糊。如果這太混亂了,你也可以嘗試使用柔和模糊進行線性縮放。