2012-03-26 3 views
0

나는 윈도우 폼에 이미지를 표시하는 프로그램을 가지고 있으며, 이미지를 페인트 할 때 이미지 안에 메시지를 넣는다. (잘 작동한다.) 나는 메시지를 다시 읽는 메소드를 가지고있다. 그러나 이렇게하면 winforms 화면이 멈 춥니 다! 나는 무한 루프에 빠지게 될 것임에 틀림 없다. 이 방법은 내가 메시지를 다시 얻는 것처럼 작동합니다 ... 누군가가 내 프로그램의 동결을 도울 수 있습니까? 아래winforms 동결 유지 - 끝없는 루프? 어떻게 동결되지 않나요?

코드 :

public partial class MyImages : Form 
    { 
     //I have variables related to encoding and decoding here(deleted) 
     private const String MESSAGE = "2008-01-07"; 

     Bitmap firstLoaded; 
     Bitmap theImage; 
     Bitmap imageEmbedded; 
     Boolean isGetMessage = false; 
     Boolean isEmbedImage = false; 
     Boolean isLoaded = false; 
     Graphics graphicsWindow; // reference to the graphic surface of this window 
     Graphics graphicsImage;  // reference to in-memory surface 
     BitArray bitsOfMessage = new BitArray(8); 
     String bytesOfTheMessage = null; 

     public MyImages() 
     { 
      InitializeComponent(); 
      this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.DoubleBuffer, true); 
     } 

     private void MyImages_Paint(object sender, PaintEventArgs e) 
     { 
      HandlePainting(); 
     } 

     public void HandlePainting() 
     { 
      if (isLoaded == true) 
      { 
       theImage = new Bitmap(Width, Height);  // bitmap for window surface copy 
       graphicsWindow = CreateGraphics(); // get our current window's surface 
       graphicsImage = Graphics.FromImage(theImage);  // create surfaces from the bitmaps 
       graphicsImage.DrawImage(firstLoaded, 0, 0, Width, Height); 

       if (isEmbedImage == true) 
       { 
        theImage = embedMessageInImage(theImage); 
       } 
       else if (isGetMessage == true) 
       { 
        getEmbeddedMessage(imageEmbedded); 
       } 

       if (isGetMessage == false) 
       { 
        graphicsWindow.DrawImage(theImage, 0, 0); 
       } 
       else if (isGetMessage == true) 
       { 
        graphicsWindow.DrawImage(imageEmbedded, 0, 0); 
       } 
      } 
     } 

     private void toolStripMenuItemLoadImage_Click(object sender, EventArgs e) 
     { 
      using (OpenFileDialog ofd = new OpenFileDialog()) 
      { 
       ofd.Title = "Load Image"; 

       if (ofd.ShowDialog() == DialogResult.OK) 
       { 
        firstLoaded = new Bitmap(ofd.FileName); 
        this.Invalidate(); 
       } 
      } 
      isLoaded = true; 
     } 

     private void toolStripMenuEmbedMessage_Click(object sender, EventArgs e) 
     { 
      isEmbedImage = true; 
      isGetMessage = false; 
      this.Invalidate(); 
     } 

     private void toolStripMenuItemGetMessage_Click(object sender, EventArgs e) 
     { 
      isEmbedImage = false; 
      isGetMessage = true; 
      this.Invalidate(); 
     } 

     public void convertToChar(int byteChar) 
     { 
      char val = Convert.ToChar(byteChar); 
      String nextChar = val.ToString(); 
      bytesOfTheMessage += nextChar; 

     } 

     private Bitmap embedMessageInImage(Bitmap bmp) 
     { 
      //Embed message in this method (deleted) 

       //unlock the bitmaps 
       newBitmap.UnlockBits(newData); 
       bmp.Save("tina.bmp"); 
       bmp.UnlockBits(originalData); 
       newBitmap.Save("tina7.bmp"); 
       imageEmbedded = newBitmap; 
       return newBitmap; 
      } 
     } 

     private void getEmbeddedMessage(Bitmap bmp) 
     { 
      unsafe 
      { 
       //create an empty bitmap the same size as original 
       Bitmap newBitmap = new Bitmap(bmp.Width, bmp.Height); 

       //lock the original bitmap in memory 
       System.Drawing.Imaging.BitmapData originalData = bmp.LockBits(
        new Rectangle(0, 0, bmp.Width, bmp.Height), 
        System.Drawing.Imaging.ImageLockMode.ReadOnly, 
        System.Drawing.Imaging.PixelFormat.Format24bppRgb); 

       //lock the new bitmap in memory 
       System.Drawing.Imaging.BitmapData newData = newBitmap.LockBits(
        new Rectangle(0, 0, bmp.Width, bmp.Height), 
        System.Drawing.Imaging.ImageLockMode.WriteOnly, 
        System.Drawing.Imaging.PixelFormat.Format24bppRgb); 

       //set the number of bytes per pixel 
       int pixelSize = 3; 

       for (int y = 0; y < bmp.Height; y++) 
       { 
        //get the data from the original image 
        byte* originalImageRow = (byte*)originalData.Scan0 + (y * originalData.Stride); 

        //get the data from the new image 
        byte* newImageRow = (byte*)newData.Scan0 + (y * newData.Stride); 

        for (int x = 0; x < bmp.Width; x++) 
        { 

         byte b = (byte)(originalImageRow[x * pixelSize + 0]); // B 
         getEachBitOfMessage(b, BLUE); 

         byte g = (byte)(originalImageRow[x * pixelSize + 1]); // G 
         getEachBitOfMessage(g, GREEN); 

         byte r = ((byte)(originalImageRow[x * pixelSize + 2])); //R 
         getEachBitOfMessage(r, RED); 

        } 
       } 

       //unlock the bitmaps 
       newBitmap.UnlockBits(newData); 
       bmp.UnlockBits(originalData); 
      } 
     } 

     public byte changeEachBit(byte byteToManipulate, int colour, byte theMessage) 
     { 
      byte value = 0; 
      byte returnByte = 0; 

      if (colour == BLUE) 
      { 
       value= (byte)(theMessage & BValueMask); 
       value = (byte)(value>>5); 
       returnByte = (byte)(byteToManipulate & BlueMask); 
       returnByte = (byte)(returnByte | value); 

      } 
      else if (colour == GREEN) 
      { 
       value = (byte)(theMessage & GValueMask); 
       value = (byte)(value >> 3); 
       returnByte = (byte)(byteToManipulate & GreenMask); 
       returnByte = (byte)(returnByte | value); 

      } 
      else if (colour == RED) 
      { 
       value = (byte)(theMessage & RValueMask); 
       returnByte = (byte)(byteToManipulate & RedMask); 
       returnByte = (byte)(returnByte | value); 
      } 

      return returnByte; 
     } 

     public void getEachBitOfMessage(byte byteToManipulate, int colour) 
     { 
      //I Input bits into image here (deleted) 

     } 
    } 
} 
+2

무엇을 시도 했습니까? 무한 루프라는 것을 어떻게 알 수 있습니까? 누군가가 여러분의 모든 코드를 복사하고 디버그 한 다음 문제가 무엇인지 알려주 길 기대하십니까? –

+0

일부 log4net 로깅을 구현하십시오. –

답변

2

은 동결 상단 도구 모음에서 일시 정지 버튼을 클릭하자. 이렇게하면 디버거가 실행이 중단 될 때마다 중단되므로 어디서 멈추었는지 쉽게 식별 할 수 있고 그 이유를 알아낼 수도 있습니다 (감시 윈도우를 사용하여 값을 보거나 마우스를 올려 놓는 것을 잊지 마세요).

+0

고마워, 네가 할 수 있다는 것을 몰랐어! – BigBug