我試圖從圖像中刪除所有白色或透明像素,留下實際圖像(裁剪)。我已經嘗試了一些解決方案,但似乎沒有任何工作。任何建議,或我會花一夜寫圖像裁剪代碼?C# - 裁剪透明/空白
回答
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
的屬性Width
和Height
也很慢。 LockBits
將是正確的方法 - StackOverflow上有很多示例。
謝謝,這讓我在那裏一半。唯一的問題是,如果圖像大於指定大小,我也想調整圖像大小。還有更多的代碼可以放在評論框中,但原始圖片只佔最終圖片的一半左右。該代碼在http://pastebin.com/He3S8aCH – Echilon 2010-07-18 09:00:11
我調換了兩個變量:P。問題修復後,很快就會有博客文章(將在此鏈接)。 – Echilon 2010-07-18 18:33:55
我想我也搞砸了。由於白色像素的R,G和B值分別爲255(RGB爲0,0, 0是黑色的)。如果原始版本適合您,那麼它可能只會裁剪透明(和黑色)像素。 – MusiGenesis 2010-07-19 01:05:10
在WPF中,我們有一個WriteableBitmap類。這是你在找什麼?如果是這種情況,請看看http://blogs.msdn.com/b/jgalasyn/archive/2008/04/17/using-writeablebitmap-to-display-a-procedural-texture.aspx
每像素檢查應該做的伎倆。掃描每一行,從頂部找到空行&底部,掃描每一行以找到左邊的&正確的約束(這可以用一行或一列完成)。當找到約束時 - 將圖像的一部分複製到另一個緩衝區。
所以,你想要做的是找到頂部,最左邊的非白色/透明像素和底部,最右邊非白色/透明像素。這兩個座標將給你一個矩形,然後你可以提取。
// 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);
}
偉大的解決方案!我刪除了|| pixelColor.A <255'使其工作。 – krlzlx 2017-04-10 10:46:54
我發現了一種在大約10分鐘內批量修剪幾千個.jpg文件的方法,但是我沒有在代碼中完成。我使用了Snag-It Editor的Convert功能。我不知道這是否是一種選擇,如果您需要修改一次或者您的需求正在進行,但是對於軟件的價格(這並不是很多),我認爲這是一個體面的解決方法。 (我不工作或代表的TechSmith。)
喬伊
- 1. 裁剪圖像 - 透明
- 2. 修剪透明PNG周圍的空白
- 3. 在C#中裁剪圖像的空白#
- 4. 裁剪多餘的空白
- 5. 如何在裁剪HTML5 Canvas/JavaScript後從圖像中刪除透明空白?
- 6. 用載波來裁剪透明像素
- 7. ImageResizer白色空間裁剪器裁剪出界
- 8. 使用php刪除透明區域(裁剪透明)使用php
- 9. 從圖像中裁剪空白
- 10. Python/Tkinter - 根據空白剪裁畫布?
- 11. 裁剪圖像後的空白窗口
- 12. Svg對象 - 空白/透明/白色
- 13. 裁剪從圖像在C#中的空白
- 14. Ruby Rmagick:裁剪/「修剪」圖像的過多(白色)空間
- 15. 使用PHP從透明的16x16圖像裁剪非透明像素
- 16. 使用jQuery自動裁剪圖像空白空間
- 17. 如何刪除的ImageView的空白空間不裁剪他們
- 18. 從自定義圖形裁剪透明空間保存時查看
- 19. 裁剪/剪裁Sprite
- 20. 切C,(不裁剪)
- 21. 圖像裁剪c#
- 22. 渲染上的透明圓圖像和裁剪它
- 23. 裁剪透明像素,但保留偏移
- 24. 爲什麼啓用透明度會導致裁剪問題?
- 25. 如何用Qt中的非透明圖形項控制裁剪?
- 26. 如何將QImage裁剪到其不透明區域?
- 27. 核心圖形 - 如何從UIImage中裁剪非透明像素?
- 28. C#複製透明PNG到剪貼板
- 29. OpenGL - 3D空間裁剪
- 30. 更正Three.js中的透視裁剪
所以有誰的夢想的一遍又一遍的汽車無編寫代碼的同一行的其他人呢?我以爲我是唯一一個:-) – 2010-07-17 11:54:32
如果你至少詳細介紹一種方法,並解釋它如何工作,它會幫助社區。 – 2010-07-17 11:58:43