2014-03-31 166 views
-2

我試圖在Windows窗體應用程序中繪製圖像。我畫了一些矩形而不是圖片,但我想用某種圖像替換它們。 我添加了整個代碼,問題出在draw函數中。任何人都可以幫忙嗎?在c中繪製圖像#

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.Threading; 
using System.Drawing.Drawing2D; 
using Microsoft.Win32; 



namespace kiralyno 
{ 
public partial class Form2 : Form 
{ 

    public static int p = 1; 

    public static int o = 1; 


    public float[] x1 = new float[80000]; 

    public float[] y1 = new float[80000]; 


    public string hely = " "; 
    public int db = 0; 

    public int[] v = new int[80000]; 
    public static string a; 
    public string s = " "; 

    public static int n = Form1.n; 
    public float x = 0, y = 0, w = 450/n, h = 450/n; 

    public int helyes(int i, int k) 
    { 
     for (int j = 1; j <= k - 1; j++) 
      if ((v[j] == i) || (Math.Abs(k - j) == Math.Abs(i - v[j]))) 
       return 0; 
     return 1; 
    } 

    public void kiir(int k) 
    { 
     for (int j = 1; j <= k; j++) 
     { 

      s += Convert.ToString(v[j]); 


      if (j != n) s += " "; 


     } 
     a = s; 
     megoldas.Items.Add(a); 

     s = ""; 
     a = ""; 


    } 


    public void back(int k) 
    { 

     for (int i = 1; i <= n; i++) 
     { 

      if (helyes(i, k) == 1) 
      { 




       v[k] = i; 
       if (k == n) 
       { 

        kiir(k); 
        draw(); 

        Thread.Sleep(500); 
        sakktabla(0, 0, w, h); 
        db++; 

       } 
       else back(k + 1); 


      } 

     } 
    } 


    public void draw() 
    { 

     // Image newImage = Image.FromFile("D:/Akos/Visual/Projects/kiralyno/kir.png"); 

     Graphics g = panel1.CreateGraphics(); 


     for (int m = 1; m <= n; m++) 
     { 


      g.DrawRectangle(new Pen(Brushes.Blue), x1[v[m]], y1[m], w, h); 
       g.FillRectangle(Brushes.Blue, x1[v[m]], y1[m], w, h); 

      Thread.Sleep(200); 
     } 

    } 


    public Form2() 
    { 


     InitializeComponent(); 
    } 


    public void sakktabla(float x, float y, float w, float h) 
    { 
     for (int i = 1; i <= n; i++) 
     { 

      for (int j = 1; j <= n; j++) 
      { 

       if ((i + j) % 2 == 0) 
       { 

        Graphics g = panel1.CreateGraphics(); 
        g.DrawRectangle(new Pen(Brushes.Black), x, y, w, h); 
        g.FillRectangle(Brushes.Black, x, y, w, h); 

        x1[p] = x; 

        x = x + 450/n; 


       } 
       else 
       { 
        Graphics g = panel1.CreateGraphics(); 
        g.DrawRectangle(new Pen(Brushes.White), x, y, w, h); 
        g.FillRectangle(Brushes.White, x, y, w, h); 


        x1[p] = x; 

        x = x + 450/n; 

       } 
       p++; 

      } 
      y1[o] = y; 
      for (int m = 1; m <= n; m++) 
      { 
       y1[n * m + o] = y1[o]; 

      } 
      o++; 


      y = y + 450/n; 

      x = 0; 

     } 
    } 


    private void panel1_Paint(object sender, PaintEventArgs e) 
    { 

     sakktabla(0, 0, w, h); 


    } 

    private void Form2_Deactivate(object sender, EventArgs e) 
    { 
     Application.Exit(); 

    } 



    private void megoldas_Click(object sender, EventArgs e) 
    { 
     label1.Text = ""; 
     back(1); 

     if (db == 0) megoldas.Items.Add("Nincs megoldas!"); 
     string c = Convert.ToString(db); 
     megoldas.Items.Add(c+" db megoldas van"); 
     megoldas.Enabled = false; 


    } 

    private void label1_Click(object sender, EventArgs e) 
    { 
     label1.Text = ""; 
     back(1); 

     if (db == 0) megoldas.Items.Add("Nincs megoldas!"); 
     string c = Convert.ToString(db); 
     megoldas.Items.Add(c + " db megoldas van"); 
     megoldas.Enabled = false; 

    } 

    private void pictureBox1_Click(object sender, EventArgs e) 
    { 

    } 

} 
} 
+0

什麼代替呢?你對自己的期望並不十分清楚。你也有變量n,x1,y1,w,h在這裏,它並不明顯。 – iandotkelly

+1

避免CreateGraphics。改用繪畫事件。你不需要一個新的筆,只需使用Pens.Blue。使用計時器而不是睡眠,這會阻止。 – LarsTech

+0

我改變了新的筆,它很好地工作,謝謝! – SpArRoWWW

回答

2

來源:http://msdn.microsoft.com/en-us/library/yws82c40%28v=vs.110%29.aspx

private void DrawImageRect(PaintEventArgs e) 
{ 

    // Create image. 
    Image newImage = Image.FromFile("SampImag.jpg"); 

    // Create rectangle for displaying image. 
    Rectangle destRect = new Rectangle(100, 100, 450, 150); 

    // Draw image to screen. 
    e.Graphics.DrawImage(newImage, destRect); 
} 
+1

感謝它幫了我很多! – SpArRoWWW

+0

沒問題。我不清楚爲什麼當我清楚地理解你的問題時,你爲什麼失敗了。 – Mitch

0

如果您確實需要自己繪製,請使用:DrawImage

但是,通常使用PictureBox通常更容易。

+0

如果我使用PictureBox如何在某些特定點上繪製多個圖像? – SpArRoWWW

+0

您可以使用位置和尺寸成員將圖片框移動到您想要的位置。對於多個圖像使用多個圖片框。 – noelicus