2013-12-09 208 views
0

我使用WFA和C#,並試圖在包含圖像的圖片框頂部繪製一個橢圓。這裏是我的代碼,但目前它的繪圖在PB後面。在圖像的頂部繪圖WFA

private void Form1_Paint(object sender, PaintEventArgs e) 
{ 
    PictureBox pb=new PictureBox(); 
    pb.Location=new Point(10,25); 
    pb.BackgroundImage = Image.FromFile("fire2_clipped_rev_1.png"); 
    pb.Size = Image.FromFile("fire2_clipped_rev_1.png").Size; 
    Truck.TruckF(pb.Location, pb.CreateGraphics()); 
    pb.Invalidate(); 
    Controls.Add(pb); 
} 

static Image truckf = Image.FromFile("fire2_clipped_rev_1.png"); 

public static void TruckF(Point location, Graphics e) 
{ 
    Wheels(truckf.Size,location,e); 
} 

private static void Wheels(Size simage,Point location,Graphics e) 
{ 
    e.FillEllipse(Brushes.Black, location.X/6F, location.Y/1.43F, 20, 20); 
} 
+0

您正在創建一個新的PictureBox,每次你的表單描繪。你確定你要這麼做嗎? – geedubb

+0

它現在非常重要,它是應用程序的早期版本,我現在只需要知道如何在圖像上繪製圖像,如果需要的話,我也可以控制它 – user2390564

回答

0

您需要更改以處理窗體上的Load事件。此外,PictureBox是一個控件,你需要繪製一個圖像,並將其交給PictureBox以使其工作。試試這個:

private PictureBox pb; 

    private void Truck_Load(object sender, EventArgs e) 
    { 
     pb = new PictureBox(); 
     pb.Location = new Point(10, 25); 
     Image bg = Image.FromFile("fire2_clipped_rev_1.png"); 
     pb.BackgroundImage = bg; 
     pb.Size = bg.Size; 
     Bitmap img = new Bitmap(pb.Size.Width, pb.Size.Height); 
     pb.Image = img; 
     using (var g = Graphics.FromImage(img)) 
     { 
      Truck.TruckF(pb.Location, g); 
     } 
     Controls.Add(pb); 
    }