【问题标题】:winforms keeps freezing -endless loop? how do i un-freeze?winforms一直冻结 - 无休止的循环?我如何解冻?
【发布时间】:2012-03-26 02:44:30
【问题描述】:

我有一个程序可以将图像显示到 Windows 窗体,并在绘制图像时在图像中放置一条消息(这很好用),然后我有一个方法可以读回消息。但是,这样做会导致 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)

        }
    }
}

【问题讨论】:

  • 你试过什么?你怎么知道这是一个无限循环?您真的希望有人复制您的所有代码,对其进行调试,然后告诉您问题所在吗?
  • 实现一些 log4net 日志记录。

标签: c# .net winforms infinite-loop visual-studio-debugging


【解决方案1】:

让它冻结并单击顶部工具栏上的暂停按钮。 这将导致调试器在执行的任何地方中断,然后您可以轻松地确定它卡在哪里,并尝试找出原因(不要忘记使用监视窗口或悬停它们来监视值)。

【讨论】:

  • 谢谢,我不知道你能做到!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-06
相关资源
最近更新 更多