2012-03-26 67 views
0

我有一個程序,它向Windows窗體顯示一個圖像,並在圖像中放置一條消息(這工作正常),然後我有一個方法可以讀回消息。但是,這樣做會導致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

你有什麼嘗試?你怎麼知道這是一個無限循環?你真的希望有人複製你的所有代碼,調試它,然後告訴你問題是什麼? – 2012-03-26 02:46:04

+0

執行一些log4net日誌記錄。 – 2012-03-26 02:48:00

回答

2

讓它結冰,然後單擊頂部工具欄上的暫停按鈕。 這會導致調試器在任何可能執行的地方斷開,然後您可以輕鬆識別它被卡住的位置,並嘗試找出原因(不要忘記使用監視窗口觀察值或將其懸停)。

+0

謝謝,我不知道你能做到這一點! – BigBug 2012-03-26 03:16:17