using (Bitmap bitmap = new Bitmap(bildAlt)) { Point min = new Point(bitmap.Width, bitmap.Height); Point max = new Point(0, 0);
for (int x = 0; x < bitmap.Width; x++) { for (int y = 0; y < bitmap.Height; y++) { Color pixelCol = bitmap.GetPixel(x, y);
if (!(pixelCol.R >= 250 && pixelCol.G >= 250 && pixelCol.B >= 250)) { 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; } } }
Rectangle cropRectangle = new Rectangle(min.X, min.Y, max.X - min.X, max.Y - min.Y); Bitmap bitmap2 = new Bitmap(cropRectangle.Width, cropRectangle.Height); bitmap2.SetResolution(bitmap.HorizontalResolution, bitmap.VerticalResolution); Graphics g = Graphics.FromImage(bitmap2); g.DrawImage(bitmap, 0, 0, cropRectangle, GraphicsUnit.Pixel);
bitmap2.Save(bildNeu, codec, para); }
So sollte es funktionieren... Die Resolution ist ein möglicher Fallstrick, wenn die Auflösung (DPI) der beiden Bitmaps nicht übereinstimmt, kommt das Bild u.U. nur vergrößert im Ziel-Bitmap an.
Sorry, die Quelle hatte ich nicht mehr. Den Code hatte ich in einem meiner Projekte drin, und einfach von dort rauskopiert. Das anpassen der Auflösung fehlt in dem Link auch, da war ich am Anfang drübergestolpert, weil der Code einfach nicht funktioniert hatte.