-1
イメージから透明ピクセルを切り抜くためにこのメソッドを書きました。CropTransparentPixelsメソッドのスピードアップに最適な方法
これは正常に動作するようですが、GetPixel
のために非常に遅いです - アルゴリズムのロジックをより速くする方法はありますか?
私はGetPixel
を高速(ただし安全ではない)アクセスコードに変更することができますが、そうするかもしれませんが、フルスキャンを避ける方法はあります。このアルゴリズムの背後にあるロジックをより速くする方法についてのアドバイスが必要です。
public Bitmap CropTransparentPixels(Bitmap originalBitmap)
{
// Find the min/max 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.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);
}
return newBitmap;
}
[Bitmap.Get/SetPixel too slow](https://stackoverflow.com/questions/1490000/)の重複が考えられます。 –
'GetPixel'を一切使用しないだけでなく、1ピクセルごとに' GetPixel'を行うのを避ける方法はありますか?私は4つのネストされたループを持っていると思っていましたが、それぞれの最小値と最大値に1つずつ...私はそれを正しくするのにはかなり近いです...私はそれが働くと、この質問を更新します。 –
私は 'Get/SetPixel'を避けることを勧めます。リンクされた記事を読んでください。 –