2017-03-17 93 views

回答

3

我提出以下方法:

  1. 讀取輸入圖像保存原始大小
  2. 刪除使用trim
  3. 拉伸(現在更小)圖像的透明*區域到原來的大小

*如果樣本圖片中的黃色區域實際上是透明的,您可以在以下代碼中輸入fuzz = 0,否則您必須調整值e確保刪除所有不需要的區域。

string srcImageFullPath = "c:\input.png"; 
int fuzz = 0; 
string destImageFullPath = "c:\output.png"; 

// Read image from file 
using (MagickImage image = new MagickImage(srcImageFullPath)) 
{ 
    //save height/width of the original image 
    int height = image.Page.Height; 
    int width = image.Page.Width; 

    //set fuzz percentage 
    image.ColorFuzz = new ImageMagick.Percentage(fuzz); 

    //trim borders 
    image.Trim(); 

    //resize image to original size 
    MagickGeometry size = new MagickGeometry(width, height); 
    size.IgnoreAspectRatio = true; 
    image.Resize(size); 

    // Save the result 
    image.Write(destImageFullPath); 
} 

在下面的圖片你可以調整右側後看到左邊的原始圖像,並且圖像:

enter image description here

  1. Trim移除了任何具有與您角落中像素相同顏色的邊框(請參閱here的細節)
  2. 由於您的示例圖像中的黃色邊框不是由單一顏色組成,因此您可以使用Fuzz刪除「相似」的顏色(更多信息here)。如前所述,如果您的邊框是透明的,只需留下fuzz = 0