C# 判斷圖形A是否在圖形B裡存在 並返回所在位置的座標

來源:互聯網
上載者:User

做了下測試

  Bitmap Image1 = (Bitmap)Image.FromFile(@"c:/1.BMP");

            Bitmap Image2 = Image1.Clone(new Rectangle(20, 20, 50, 50), Image1.PixelFormat);

            Point _Point= Zgke.MyImage.Panit.MyPanit.GetImageContains(Image1, Image2,0);

            MessageBox.Show(_Point.ToString());

返回的座標是20,20

下面是全部代碼

  1. /// <summary>
      
  2.      /// 判斷圖形裡是否存在另外一個圖形 並返回所在位置
      
  3.      /// </summary>
      
  4.      /// <param name="p_SourceBitmap">原始圖形</param>
      
  5.      /// <param name="p_PartBitmap">小圖形</param>
      
  6.      /// <param name="p_Float">溶差</param>
      
  7.      /// <returns>座標</returns>
      
  8.      public
     
    static
     Point GetImageContains(Bitmap p_SourceBitmap, Bitmap p_PartBitmap,
    int
     p_Float)  
  9.      {  
  10.          int
     _SourceWidth = p_SourceBitmap.Width;  
  11.          int
     _SourceHeight = p_SourceBitmap.Height;  
  12.   
  13.          int
     _PartWidth = p_PartBitmap.Width;  
  14.          int
     _PartHeight = p_PartBitmap.Height;  
  15.   
  16.          Bitmap _SourceBitmap = new
     Bitmap(_SourceWidth, _SourceHeight);  
  17.          Graphics _Graphics = Graphics.FromImage(_SourceBitmap);  
  18.          _Graphics.DrawImage(p_SourceBitmap, new
     Rectangle(0, 0, _SourceWidth, _SourceHeight));  
  19.          _Graphics.Dispose();  
  20.          BitmapData _SourceData = _SourceBitmap.LockBits(new
     Rectangle(0, 0, _SourceWidth, _SourceHeight), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);  
  21.          byte
    [] _SourceByte = 
    new
     
    byte
    [_SourceData.Stride * _SourceHeight];  
  22.          Marshal.Copy(_SourceData.Scan0, _SourceByte, 0, _SourceByte.Length);  //複製出p_SourceBitmap的相素資訊 
      
  23.   
  24.          Bitmap _PartBitmap = new
     Bitmap(_PartWidth, _PartHeight);  
  25.          _Graphics = Graphics.FromImage(_PartBitmap);  
  26.          _Graphics.DrawImage(p_PartBitmap, new
     Rectangle(0, 0, _PartWidth, _PartHeight));  
  27.          _Graphics.Dispose();  
  28.          BitmapData _PartData = _PartBitmap.LockBits(new
     Rectangle(0, 0, _PartWidth, _PartHeight), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);  
  29.          byte
    [] _PartByte = 
    new
     
    byte
    [_PartData.Stride * _PartHeight];  
  30.          Marshal.Copy(_PartData.Scan0, _PartByte, 0, _PartByte.Length);   //複製出p_PartBitmap的相素資訊 
      
  31.   
  32.        
  33.          for
     (
    int
     i = 0; i != _SourceHeight; i++)  
  34.          {  
  35.              if
     (_SourceHeight - i < _PartHeight) 
    return
     
    new
     Point(-1, -1);  
    //如果 剩餘的高 比需要比較的高 還要小 就直接返回             
      
  36.              int
     _PointX = -1;    
    //臨時存放座標 需要包正找到的是在一個X點上
      
  37.              bool
     _SacnOver = 
    true
    ;   
    //是否都比配的上
      
  38.              for
     (
    int
     z = 0; z != _PartHeight - 1; z++)       
    //迴圈目標進行比較
      
  39.              {  
  40.                  int
     _TrueX = GetImageContains(_SourceByte, _PartByte, i * _SourceData.Stride, _SourceWidth, _PartWidth, p_Float);  
  41.   
  42.                  if
     (_TrueX == -1)   
    //如果沒找到 
      
  43.                  {  
  44.                      _PointX = -1;    //設定座標為沒找到
      
  45.                      _SacnOver = false
    ;   
    //設定不進行返回
      
  46.                      break
    ;  
  47.                  }  
  48.                  else
      
  49.                  {  
  50.                      if
     (z == 0) _PointX = _TrueX;  
  51.                      if
     (_PointX != _TrueX)   
    //如果找到了 也的保證座標和上一行的座標一樣 否則也返回
      
  52.                      {  
  53.                          _PointX = -1;//設定座標為沒找到
      
  54.                          _SacnOver = false
    ;  
    //設定不進行返回
      
  55.                          break
    ;  
  56.                      }  
  57.                  }                                           
  58.              }  
  59.              if
     (_SacnOver) 
    return
     
    new
     Point(_PointX , i);                
  60.          }  
  61.          return
     
    new
     Point(-1, -1);  
  62.      }  
  63.      /// <summary>
      
  64.      /// 判斷圖形裡是否存在另外一個圖形 所在行的索引
      
  65.      /// </summary>
      
  66.      /// <param name="p_Source">原始圖形資料</param>
      
  67.      /// <param name="p_Part">小圖形資料</param>
      
  68.      /// <param name="p_SourceIndex">開始位置</param>
      
  69.      /// <param name="p_SourceWidth">原始圖形寬</param>
      
  70.      /// <param name="p_PartWidth">小圖寬</param>
      
  71.      /// <param name="p_Float">溶差</param>
      
  72.      /// <returns>所在行的索引 如果找不到返回-1</returns>
      
  73.      private
     
    static
     
    int
     GetImageContains(
    byte
    [] p_Source, 
    byte
    [] p_Part, 
    int
     p_SourceIndex, 
    int
     p_SourceWidth,
    int
     p_PartWidth, 
    int
     p_Float)  
  74.      {  
  75.          int
     _PartIndex = 0;  
  76.          int
     _SourceIndex=p_SourceIndex;           
  77.          for
     (
    int
     i = 0; i < p_SourceWidth; i++)  
  78.          {  
  79.              if
     (p_SourceWidth - i < p_PartWidth) 
    return
     -1;  
  80.              Color _CurrentlyColor = Color.FromArgb((int
    )p_Source[_SourceIndex+3], (
    int
    )p_Source[_SourceIndex + 2], (
    int
    )p_Source[_SourceIndex + 1], (
    int
    )p_Source[_SourceIndex]);  
  81.              Color _CompareColoe = Color.FromArgb((int
    )p_Part[3], (
    int
    )p_Part[2], (
    int
    )p_Part[1], (
    int
    )p_Part[0]);  
  82.              _SourceIndex += 4;  
  83.       
  84.              bool
     _ScanColor = ScanColor(_CurrentlyColor, _CompareColoe, p_Float);  
  85.   
  86.              if
     (_ScanColor)  
  87.              {  
  88.                  _PartIndex += 4;  
  89.                  int
     _SourceRVA = _SourceIndex;  
  90.                  bool
     _Equals=
    true
    ;  
  91.                  for
     (
    int
     z = 0; z != p_PartWidth-1; z++)  
  92.                  {  
  93.                      _CurrentlyColor = Color.FromArgb((int
    )p_Source[_SourceRVA + 3], (
    int
    )p_Source[_SourceRVA + 2], (
    int
    )p_Source[_SourceRVA + 1], (
    int
    )p_Source[_SourceRVA]);  
  94.                      _CompareColoe = Color.FromArgb((int
    )p_Part[_PartIndex + 3], (
    int
    )p_Part[_PartIndex + 2], (
    int
    )p_Part[_PartIndex + 1], (
    int
    )p_Part[_PartIndex]);  
  95.   
  96.                      if
     (!ScanColor(_CurrentlyColor, _CompareColoe, p_Float))  
  97.                      {  
  98.                          _PartIndex = 0;  
  99.                          _Equals = false
    ;  
  100.                          break
    ;                             
  101.                      }  
  102.                      _PartIndex += 4;  
  103.                      _SourceRVA += 4;  
  104.                  }  
  105.                  if
    (_Equals)
    return
     i;  
  106.              }  
  107.              else
      
  108.              {  
  109.                  _PartIndex = 0;  
  110.              }  
  111.          }  
  112.          return
     -1;  
  113.      }  
  114.   
  115.      /// <summary>
      
  116.      /// 檢查色彩(可以根據這個更改比較方式
      
  117.      /// </summary>
      
  118.      /// <param name="p_CurrentlyColor">當前色彩</param>
      
  119.      /// <param name="p_CompareColor">比較色彩</param>
      
  120.      /// <param name="p_Float">溶差</param>
      
  121.      /// <returns></returns>
      
  122.      private
     
    static
     
    bool
     ScanColor(Color p_CurrentlyColor, Color p_CompareColor, 
    int
     p_Float)  
  123.      {  
  124.          int
     _R = p_CurrentlyColor.R;  
  125.          int
     _G = p_CurrentlyColor.G;  
  126.          int
     _B = p_CurrentlyColor.B;  
  127.   
  128.          return
     (_R <= p_CompareColor.R + p_Float && _R >= p_CompareColor.R - p_Float) && (_G <= p_CompareColor.G + p_Float && _G >= p_CompareColor.G - p_Float) && (_B <= p_CompareColor.B + p_Float && _B >= p_CompareColor.B - p_Float);  
  129.   
  130.      } 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.