2010-07-17 72 views
5

我試圖從圖像中刪除所有白色或透明像素,留下實際圖像(裁剪)。我已經嘗試了一些解決方案,但似乎沒有任何工作。任何建議,或我會花一夜寫圖像裁剪代碼?C# - 裁剪透明/空白

+0

所以有誰的夢想的一遍又一遍的汽車無編寫代碼的同一行的其他人呢?我以爲我是唯一一個:-) – 2010-07-17 11:54:32

+1

如果你至少詳細介紹一種方法,並解釋它如何工作,它會幫助社區。 – 2010-07-17 11:58:43

回答

3
public Bitmap CropBitmap(Bitmap original) 
{ 
    // determine new left 
    int newLeft = -1; 
    for (int x = 0; x < original.Width; x++) 
    { 
     for (int y = 0; y < original.Height; y++) 
     { 
      Color color = original.GetPixel(x, y); 
      if ((color.R != 255) || (color.G != 255) || (color.B != 255) || 
       (color.A != 0)) 
      { 
       // this pixel is either not white or not fully transparent 
       newLeft = x; 
       break; 
      } 
     } 
     if (newLeft != -1) 
     { 
      break; 
     } 

     // repeat logic for new right, top and bottom 

    } 

    Bitmap ret = new Bitmap(newRight - newLeft, newTop - newBottom); 
    using (Graphics g = Graphics.FromImage(ret) 
    { 
     // copy from the original onto the new, using the new coordinates as 
     // source coordinates for the original 
     g.DrawImage(...); 
    } 

    return ret 
} 

請注意,這個功能會很慢,因爲髒東西。 GetPixel()的速度令人難以置信,並且訪問循環內的Bitmap的屬性WidthHeight也很慢。 LockBits將是正確的方法 - StackOverflow上有很多示例。

+1

謝謝,這讓我在那裏一半。唯一的問題是,如果圖像大於指定大小,我也想調整圖像大小。還有更多的代碼可以放在評論框中,但原始圖片只佔最終圖片的一半左右。該代碼在http://pastebin.com/He3S8aCH – Echilon 2010-07-18 09:00:11

+0

我調換了兩個變量:P。問題修復後,很快就會有博客文章(將在此鏈接)。 – Echilon 2010-07-18 18:33:55

+0

我想我也搞砸了。由於白色像素的R,G和B值分別爲255(RGB爲0,0, 0是黑色的)。如果原始版本適合您,那麼它可能只會裁剪透明(和黑色)像素。 – MusiGenesis 2010-07-19 01:05:10

2

每像素檢查應該做的伎倆。掃描每一行,從頂部找到空行&底部,掃描每一行以找到左邊的&正確的約束(這可以用一行或一列完成)。當找到約束時 - 將圖像的一部分複製到另一個緩衝區。

9

所以,你想要做的是找到頂部,最左邊的非白色/透明像素和底部,最右邊非白色/透明像素。這兩個座標將給你一個矩形,然後你可以提取。

// Load the bitmap 
    Bitmap originalBitmap = Bitmap.FromFile("d:\\temp\\test.bmp") as Bitmap; 

    // Find the min/max non-white/transparent pixels 
    Point min = new Point(int.MaxValue, int.MaxValue); 
    Point max = new Point(int.MinValue, int.MinValue); 

    for (int x = 0; x < originalBitmap.Width; ++x) 
    { 
    for (int y = 0; y < originalBitmap.Height; ++y) 
    { 
     Color pixelColor = originalBitmap.GetPixel(x, y); 
     if (!(pixelColor.R == 255 && pixelColor.G == 255 && pixelColor.B == 255) 
     || pixelColor.A < 255) 
     { 
     if (x < min.X) min.X = x; 
     if (y < min.Y) min.Y = y; 

     if (x > max.X) max.X = x; 
     if (y > max.Y) max.Y = y; 
     } 
    } 
    } 

    // Create a new bitmap from the crop rectangle 
    Rectangle cropRectangle = new Rectangle(min.X, min.Y, max.X - min.X, max.Y - min.Y); 
    Bitmap newBitmap = new Bitmap(cropRectangle.Width, cropRectangle.Height); 
    using (Graphics g = Graphics.FromImage(newBitmap)) 
    { 
    g.DrawImage(originalBitmap, 0, 0, cropRectangle, GraphicsUnit.Pixel); 
    } 
+0

偉大的解決方案!我刪除了|| pixelColor.A <255'使其工作。 – krlzlx 2017-04-10 10:46:54

0

我發現了一種在大約10分鐘內批量修剪幾千個.jpg文件的方法,但是我沒有在代碼中完成。我使用了Snag-It Editor的Convert功能。我不知道這是否是一種選擇,如果您需要修改一次或者您的需求正在進行,但是對於軟件的價格(這並不是很多),我認爲這是一個體面的解決方法。 (我不工作或代表的TechSmith。)

喬伊