C# PictureBox.SizeMode = Zoom does not redraw when assigning new images

亡梦爱人 提交于 2019-12-11 18:03:42

问题


I am working on a CCTV project which employs ONVIF. I use a Winform sample, which is provided by "ONVIF Device Manager" project, to obtain video frames from a camera. (You can find it here) It was working fine if I connect to one camera. But, if I connect to six cameras some picture box does not redraw when I assign new images in DrawFrame(). Two back rectangles contain a red ellipse in the attached picture are supposed to display an image. This issues only occur when picture box size mode is Zoom. As what I have tried, those picture boxes can only redraw if I call Application.DoEvent() or call PictureBox.Update()/Refresh() every time when I set a new image.

Two red ellipses in the attached picture are supposed to display an image

private void DrawFrame(VideoBuffer videoBuffer, PlaybackStatistics statistics)
{
    Bitmap bmp = img as Bitmap;
    BitmapData bd = null;
    try
    {
        bd = bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);//bgra32

        using (var md = videoBuffer.Lock())
        {

            CopyMemory(bd.Scan0, md.value.scan0Ptr, videoBuff.stride * videoBuff.height);

            //bitmap.WritePixels(
            //    new Int32Rect(0, 0, videoBuffer.width, videoBuffer.height),
            //    md.value.scan0Ptr, videoBuffer.size, videoBuffer.stride,
            //    0, 0
            //);
        }

    }
    catch (Exception err)
    {
        //errBox.Text = err.Message;
        Debug.Print("DrawFrame:: " + err.Message);
    }
    finally
    {
        bmp.UnlockBits(bd);
    }
    imageBox.Image = bmp;
    /*Application.DoEvent() // not recommended since this method causes the current thread to be suspended
    or call imageBox.Update() // causes hanging on UI thread
    or imageBox.Refresh() // causes hanging on UI thread
    or PictureBox.Invalidate(), do nothing.*/
}

I create picture boxes and add to a panel following this code.

PictureBox ptBox = new PictureBox();
ptBox.Size = new Size(elementWidth, elementHeight);
ptBox.Name = "PictureBox_" + (j) + (i);
ptBox.Location = new Point(j * elementWidth, i * elementHeight); //relative location
ptBox.BorderStyle = BorderStyle.FixedSingle;
ptBox.SizeMode = PictureBoxSizeMode.Zoom;
mPanel.Controls.Add(ptBox);

After a week, I found that PictureBoxs occurs the error does not trigger OnPaint(PaintEventArgs e) event. It leads to the error that new images are not redrawn.

来源:https://stackoverflow.com/questions/55034455/c-sharp-picturebox-sizemode-zoom-does-not-redraw-when-assigning-new-images

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!