2010-11-04 59 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.Windows.Forms; 
using TouchlessLib; 

namespace WebCam2 
{ 
    public partial class Form1 : Form 
    { 
     TouchlessMgr ngr = new TouchlessMgr(); 
     Bitmap _overlay; 
     public Form1() 
     { 
      InitializeComponent(); 
      foreach (Camera c in ngr.Cameras) 
      { 
       listBox1.Items.Add(c); 
       listBox1.SelectedValueChanged += new EventHandler(listBox1_SelectedValueChanged); 

      } 
     } 

     void listBox1_SelectedValueChanged(object sender, EventArgs e) 
     { 
      ngr.CurrentCamera = (Camera) listBox1.SelectedItem; 
      ngr.CurrentCamera.OnImageCaptured += c_OnImageCaptured; 
     } 

     void c_OnImageCaptured(object sender, CameraEventArgs e) 
     { 
      pictureBox1.Image = ngr.CurrentCamera.GetCurrentImage(); 
     } 

     private void pictureBox1_Click(object sender, EventArgs e) 
     { 


     } 

     public EventHandler<CameraEventArgs> cam_OnImageCaptured { get; set; } 

     private void pictureBox1_MouseDown(object sender, MouseEventArgs e) 
     { 
      List <Graphics> list = new List <Graphics>(); 
      ngr.RefreshCameraList(); 
      Graphics g = Graphics.FromImage(pictureBox1.Image); 
      Brush redBrush = new SolidBrush(Color.Red); 
      Pen pen = new Pen(redBrush,3); 
      for (int i = 0; i < pictureBox1.Width; i = (pictureBox1.Width/3)+i) 
      { 
       for (int y = 0; y < pictureBox1.Height; y = (pictureBox1.Height/3) + y) 
       { 
        g.DrawRectangle(pen, i, y, pictureBox1.Width/3, pictureBox1.Height/3); 
       } 
      } 
      g.Dispose(); 


     } 

    } 
} 

此代码的工作只是有时不是每次,我不知道什么是错的代码。 我也想分裂成一个3x3矩阵的图像,但我不知道如何。

请帮忙!

输出(第一时间没有框架,第二次右):

ImageLink的:

http://www.imagebanana.com/view/4j58i05z/Unbenannt2.png

回答

0

g.Dispose

+0

你是我的英雄^^,谢谢 – user422039 2010-11-04 18:07:58

+0

你知道如何将位图分成几块吗? – user422039 2010-11-04 18:08:19

+0

为演示文稿的目的,我已经添加一个按钮到一个窗体和第二个picturebox,整数从1-9周期。显然你不需要这样做。这里是代码: – Kell 2010-11-05 09:56:49

0

后添加pictureBox1.Invalidate();为demonstartino目的,我添加了一个按钮转换为表单和第二个图片框,并使用从1-9开始的整数。显然你不需要这样做。这里是代码:

 private void button1_Click(object sender, EventArgs e) 
    { 
     mLastRect++; 
     if (mLastRect > 9) 
      mLastRect = 0; 
     Bitmap part = new Bitmap(pictureBox1.Image.Width/3, pictureBox1.Image.Height/3); 
     Graphics g = Graphics.FromImage(part); 
     Rectangle partRect = new Rectangle(0, 0, part.Width, part.Height); 
     Rectangle sourceRect = GetRect(mLastRect); 
     g.DrawImage(pictureBox1.Image, partRect, sourceRect, GraphicsUnit.Pixel); 
     pictureBox2.Image = part; 
    } 

    private Rectangle GetRect(int rectNo) 
    { 
     int rectLeft = (rectNo % 3) * (pictureBox1.Image.Width/3); 
     int rectTop = (rectNo/3) * (pictureBox1.Image.Height/3); 
     return new Rectangle(rectLeft, rectTop, pictureBox1.Image.Width/3, pictureBox1.Image.Height/3); 
    }