Autor Beitrag
Chryzler
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1097
Erhaltene Danke: 2



BeitragVerfasst: Fr 07.12.07 17:36 
Servus! :wave:

folgendes Problem: *snip* ich muss Flächen in einem Image einfärben, also per FloodFill. Unter Delphi eigentlich kein Problem, wohl aber unter .NET. Die FillRegion-Methode scheint das richtige zu sein, aber dann muss man die Fläche als Region angeben. Und für die braucht man anscheinend einen GraphicsPath. Und genau da hab ich meine Probleme. Im Internet find ich dazu so gut wie nix, auf The Code Project steht nur, wie man seinen eigenen FloodFill-Algorithmus implementiert. Soweit bin ich schon:
ausblenden C#-Quelltext
1:
2:
3:
Graphics graphics = Graphics.FromImage(pictureBox1.Image);
GraphicsPath graphicsPath = new GraphicsPath( // ... ???
graphics.FillRegion(Brushes.Blue, new Region(graphicsPath));


Also hat jemand ne Idee wie ich aus meiner Fläche eine Region bekomm, damit ich die einfärben kann. Danke!

Chryzler

Moderiert von user profile iconNarses: Beitrag der aktuellen Lage angepasst. ;)
Chryzler Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1097
Erhaltene Danke: 2



BeitragVerfasst: Sa 08.12.07 13:28 
So, ich habs jetzt doch noch hingekriegt, allerdings nicht per FillRegion, sondern per PInvoke von den GDI-Zeichenmethoden. Aber falls jemand weiß wie das mit FillRegion geht, dann bitte hier posten.
ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
//...
private void button1_Click(object sender, EventArgs e)
{
    Graphics graphics = Graphics.FromHwnd(Handle);
    graphics.DrawEllipse(Pens.Gray, new Rectangle(5050200200));
    graphics.FloodFill(new Point(1010), Color.Blue, FloodFillType.Surface);
}
//...

public enum FloodFillType
{
    Border,
    Surface
}

public static class Extensions
{
    [DllImport("gdi32.dll")]
    private static extern IntPtr SelectObject(IntPtr hdc, IntPtr hgdiobj);
    [DllImport("gdi32.dll")]
    private static extern IntPtr CreateSolidBrush(int crColor);
    [DllImport("gdi32.dll")]
    private static extern bool ExtFloodFill(IntPtr hdc, int nXStart, int nYStart, int crColor, uint fuFillType);
    [DllImport("gdi32.dll")]
    private static extern bool DeleteObject(IntPtr hObject);
    [DllImport("gdi32.dll")]
    public static extern int GetPixel(IntPtr hdc, int x, int y);

    public static void FloodFill(this Graphics graphics, Point point, Color color, FloodFillType type)
    {
        IntPtr dc = graphics.GetHdc();
        IntPtr brush = CreateSolidBrush(ColorTranslator.ToWin32(color));
        IntPtr previousBrush = SelectObject(dc, brush);
        ExtFloodFill(dc, point.X, point.Y, GetPixel(dc, point.X, point.Y), type == FloodFillType.Border ? (uint)0 : 1);
        SelectObject(dc, previousBrush);
        DeleteObject(brush);
        graphics.ReleaseHdc(dc);
    }
}

(Ich hoffe mal das ist so in Ordnung dass ich das hier jetzt poste.. :?)
Chryzler Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1097
Erhaltene Danke: 2



BeitragVerfasst: Di 11.12.07 21:25 
user profile iconChryzler hat folgendes geschrieben:
So, ich habs jetzt doch noch hingekriegt, ...

Hingekriegt ist übertrieben. Die Sache funktioniert perfekt wenn ich direkt auf die Form zeichne:
ausblenden C#-Quelltext
1:
2:
3:
Graphics graphics = Graphics.FromHwnd(Handle);  
graphics.DrawEllipse(Pens.Gray, new Rectangle(5050200200));  
graphics.FloodFill(new Point(1010), Color.Blue, FloodFillType.Surface);

Sobald ich aber auf ein Bitmap im Speicher zeichnen möchte, klappt gar nix mehr:
ausblenden C#-Quelltext
1:
2:
3:
4:
Bitmap bitmap = new Bitmap(512512);
Graphics graphics = Graphics.FromImage(bitmap);  
graphics.DrawEllipse(Pens.Gray, new Rectangle(5050200200));  
graphics.FloodFill(new Point(1010), Color.Blue, FloodFillType.Surface);

Es wird sogar auf das Bitmap gezeichnet, doch die FloodFill-Methode malt entweder das komplette Bild mit der Farbe aus (so als ob es keine Grenzen erkennen könnte), oder gar nichts. Hab auch schon alle Möglichkeiten mit PixelFormat im Bitmap-Konstruktor ausprobiert, das Ergebnis ist das gleiche.

Wäre super wenn jemand ne Lösung wüsste oder eine Idee hat, an was es liegen könnte. :)

Chryzler