2014-03-27 84 views
0

我想繪製一個程序。我有基本的工作,以及記錄所有鼠標點擊的方式,以便我可以將它們繪製到位圖,然後保存它,除非它不工作,使用這種方法我可以重繪圖形,除非我試圖保存位圖圖像爲空的文件?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.Drawing.Imaging; 
namespace Paint_Program 
{ 
    public partial class PaintImg : Form 
    { 

    public Boolean veryimportantbool = false; 
    int drawcount = 0; 
    int brushsize = 16; 

    List<Point> pts = new List<Point>(); 
    Point recordpoint { get; set; } 

    public PaintImg() 
    { 
     InitializeComponent(); 
     //Cursor drawCursor = new Cursor("Pencil.cur"); 
     //this.Cursor = drawCursor; 


    } 


    private void Form1_Load(object sender, EventArgs e) 
    { 


     //Cursor.Position = new Point(this.Location.X - this.Width/2, this.Location.Y - this.Height/2) 
     UserControl1 userc1 = new UserControl1(); 
     userc1.Show(); 
     brushsize = Convert.ToInt16(label1.Text); 


    } 

    private void rectangleShape1_Click(object sender, EventArgs e) 
    { 

    } 

    private void Test_Click(object sender, EventArgs e) 
    { 
     //testing 
     //Console.WriteLine("X:" + mousex); 
     //onsole.WriteLine("Y: " + mousey); 
    } 

    private void Form1_SizeChanged(object sender, EventArgs e) 
    { 

    } 


    public void Form1_MouseClick(object sender, MouseEventArgs i) 
    { 

      drawcount = drawcount + 1; 
      System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Red); 
      System.Drawing.Graphics formGraphics; 
      formGraphics = this.CreateGraphics(); 
      formGraphics.FillRectangle(myBrush, new Rectangle(i.X, i.Y, brushsize, brushsize)); 
      myBrush.Dispose(); 
      formGraphics.Dispose(); 
      count.Text = Convert.ToString(drawcount); 


    } 

    private void Paint_Paint(object sender, PaintEventArgs e) 
    { 
     //this method will be used later when page resizing is added to the program. 

     //Graphics g = e.Graphics; 
     //Control control = (Control)sender; 
     //drawPoint = control.PointToScreen(new Point(MousePosition.X, MousePosition.Y)); 
     //g.DrawRectangle(System.Drawing.Pens.Red, drawPoint.X, drawPoint.Y, 1, 1); 

    } 

    private void newToolStripButton_Click(object sender, EventArgs e) 
    { 
     //SetupDoc newsetup = new SetupDoc(); 
     //newsetup.ShowDialog(); 
    } 

    private void PaintImg_MouseMove(object sender, MouseEventArgs e) 
    { 

    } 

    private void panel1_Paint(object sender, PaintEventArgs e) 
    { 

    } 

    private void panel1_Click(object sender, MouseEventArgs i) 
    { 
     drawcount = drawcount + 1; 
     recordpoint = new Point(i.X, i.Y); 
     pts.Add(recordpoint); 
     System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Red); 
     System.Drawing.Graphics formGraphics; 
     formGraphics = this.CreateGraphics(); 
     formGraphics.FillRectangle(myBrush, new Rectangle(i.X, i.Y, brushsize, brushsize)); 
     myBrush.Dispose(); 
     formGraphics.Dispose(); 
     count.Text = Convert.ToString(drawcount); 
    } 

    private void label1_Click(object sender, EventArgs e) 
    { 

    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     brushsize = brushsize + 1; 
     label1.Text = Convert.ToString(brushsize); 
    } 

    private void button2_Click(object sender, EventArgs e) 
    { 
     brushsize = brushsize - 1; 
     label1.Text = Convert.ToString(brushsize); 
    } 

    private void button3_Click(object sender, EventArgs e) 
    { 
     Image bmp = new Bitmap(this.Width - panel1.Width, this.Height - panel1.Height); 
     using (Graphics g = Graphics.FromImage(bmp)) 
     { 
      foreach (var recordpoint in pts) 
      { 

       g.FillRectangle(new SolidBrush(Color.Red), new Rectangle(Convert.ToInt16(recordpoint.X), Convert.ToInt16(recordpoint.Y), brushsize, brushsize)); 
      } 
     } 
     bmp.Save("testing.bmp"); 
    } 

} 
} 

如果有人知道我在做什麼錯了,請告訴MEE!

+0

調試你的代碼,並確定天氣或不點被填充。 –

回答

0

panel1_Click記錄您的pts集合中的座標,但Form1_MouseClick事件不會執行此操作。雖然這兩種方法都確實吸引了這些觀點。

您確定要添加到您期望的pts集合嗎?

+0

哦,Panel1_Click是一個我必須忘記刪除它的錯誤,但是當我將移動到列表中的部分添加到Form1_MouseClick時,它仍然不起作用。 – user3349095

0

檢查位圖大小

this.Width - panel1.Width,this.Height - panel1.Height 
+0

沒有那是正確的 - 我需要的位圖是用戶可以繪製的區域的大小,所以我得到了窗體的高度和寬度,並且使用了panel1(窗體中的一個控件)的高度和寬度,以便它與申請人的空間大小相同。 – user3349095

+0

我的意思是檢查高度或寬度爲零。 – prabhakaran