2017-02-26 21 views
0

大きな画像内に小さな画像が含まれる領域を含むRectangle(rec)があります。この小さな画像をPictureboxに表示したい。しかし、私が実際にやっていることは、333x324という大きな画像の画像検出器として小さな画像を使用することです。だから、私がしたいのは、小さい方の画像矩形の座標を使ってから、矩形の左側から始めて、333の幅と324の高さで外に向かってPictureboxに描画することです。Graphics.DrawImageで描画領域を拡張する方法C#

現在、私のコードは動作しますが、検出目的で使用されていた小さな画像しか表示されません。小さな画像+ 300幅と+ 300高さを表示したい。

私はこのコードを何時間も手抜きしました。誰かが私を助けることができるなら、私はそれをとても高く評価します!

クラスのための私のコード:

public static class Worker 
{ 

    public static void doWork(object myForm) 
    { 
     //infinitely search for maps 
     for (;;) 
     { 


      //match type signature for Threading 
      var myForm1 = (Form1)myForm; 

      //capture screen 
      Bitmap currentBitmap = new Bitmap(CaptureScreen.capture()); 

      //detect map 
      Detector detector = new Detector(); 
      Rectangle rec = detector.searchBitmap(currentBitmap, 0.1); 

      //if it actually found something 
      if(rec.Width != 0) 
      { 
       // Create the new bitmap and associated graphics object 
       Bitmap bmp = new Bitmap(rec.X, rec.Y); 
       Graphics g = Graphics.FromImage(bmp); 

       // Draw the specified section of the source bitmap to the new one 
       g.DrawImage(currentBitmap, 0,0, rec, GraphicsUnit.Pixel); 

       // send to the picture box &refresh; 
       myForm1.Invoke(new Action(() => 
       { 
        myForm1.getPicturebox().Image = bmp; 
        myForm1.getPicturebox().Refresh(); 
        myForm1.Update(); 
       })); 

       // Clean up 
       g.Dispose(); 
       bmp.Dispose(); 

      } 

      //kill 
      currentBitmap.Dispose(); 

      //do 10 times per second 
      System.Threading.Thread.Sleep(100); 

     } 
    } 
} 
+0

私はあなたがしていることを理解することは本当に難しいと認めます。新しいビットマップrec.X、rec.Yのサイズはなぜですか?全部のスケッチを表示できますか? – TaW

答えて

0

私が正しく理解していれば、rec変数はWidth=333Height=324を持つ矩形を識別し、正しいXYを持つ矩形が含まれています。 if文の中ので

、希望のサイズに設定して開始:、その後

rec.Width = 333; 
rec.Height = 324; 

Bitmapコンストラクタは、幅と高さを期待していることに注意してください、そう

Bitmap bmp = new Bitmap(rec.X, rec.Y); 

を変更

Bitmap bmp = new Bitmap(rec.Width, rec.Height); 

それだけです。残りのコードはそれは今のままにしておいてください。

関連する問題