位图.clone收到错误“对象当前正在使用”

时间:2018-04-16 19:54:29

标签: c# .net image-processing bitmap

当我尝试克隆位图以将其格式从32bpp更改为24bpp时,我在使用相机帧来自USB相机的位图时遇到此错误,以便匹配后续比较方法的输入要求。

在Windows 10上使用Visual Studio 2017 Pro C#。

初始化Bitmap对象......

namespace ACCORD_WindowsFormsApp9
{
    public partial class Control_Panel : Form
                    static Bitmap camera_snapshot_Bitmap = null;
                    static Bitmap camera_frame_Bitmap = null;
                    static Bitmap latest_frame_buffer_Bitmap = null;
                    static Bitmap latest_frame_clone_Bitmap = null;
                    static Bitmap reference_snapshot_Bitmap = null, reference_cropped_Bitmap = null;
                    static Bitmap target_snapshot_Bitmap = null, target_cropped_Bitmap = null;
            static int PictureBox_live_camera_changes = 0;


. . .

        // CONSTRUCTOR:
        public Control_Panel()
        {
            InitializeComponent();
        // Automatically start inputing each frame from camera:
            var videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
            // Select physical USB camera:
                videoSource = new VideoCaptureDevice( videoDevices[0].MonikerString, pixelFormat_for_ExhaustiveTemplateMatching);


        // Select event handlers:
            videoSource.NewFrame += new NewFrameEventHandler(Event_camera_frame);
            PictureBox_live_camera.Paint +=     Event_draws_crop_rectangle;

        // Start slurpin up them camera frames:
            videoSource.Start();

       }

使用USB摄像头的最新帧激活事件..............

private  void Event_camera_frame(object sender, NewFrameEventArgs camera_frame_event)
{

    // Select latest frame from camera:   
        if( camera_snapshot_Bitmap != null )
        {
            camera_snapshot_Bitmap.Dispose();
        }
        Bitmap  camera_frame_Bitmap =  camera_frame_event.Frame;                       // copy pointer

        camera_snapshot_Bitmap = new Bitmap( rotation_filter.Apply(   camera_frame_Bitmap ));           

        if(  latest_frame_buffer_Bitmap != null )
        {   
             latest_frame_buffer_Bitmap.Dispose();
        }
        latest_frame_buffer_Bitmap = new Bitmap(    camera_snapshot_Bitmap );           // copy frame bitmap data to buffer for later image snapshotting
        latest_frame_clone_Bitmap = latest_frame_buffer_Bitmap.Clone(new Rectangle(0, 0, latest_frame_buffer_Bitmap.Width, latest_frame_buffer_Bitmap.Height), System.Drawing.Imaging.PixelFormat.Format24bppRgb );

        set_display_rectangle();        //   PictureBox_latest_frame  ==>  display_rectangle X, Y, Width, Height
    update_target_view();
}




void update_target_view()
{
        if( PictureBox_live_camera_changes == 0 )       return;

        PictureBox_live_camera.Image = latest_frame_clone_Bitmap;
        PictureBox_live_camera.BackgroundImage = latest_frame_clone_Bitmap;
            PictureBox_live_camera_changes = 1;

 }

    void Event_draws_crop_rectangle(object sender, PaintEventArgs e)
    {
        e.Graphics.DrawRectangle( crop_pen, display_rectangle );                     /////////////   DRAW CROP RECTANGLE   /////////////
    }
    private void set_display_rectangle( )
    {
        int width, height;
            width = PictureBox_live_camera.Width;
            height = PictureBox_live_camera.Height;

        // Set crop factor as a function of max_wheel_clicks:
            double crop_factor_X =  Convert.ToDouble( mouse_wheel_clicksX ) /   max_wheel_clicksX;     // min 0 .. 1.0 max
            double crop_factor_Y =  Convert.ToDouble( mouse_wheel_clicksY ) /   max_wheel_clicksY;     // min 0 .. 1.0 max

        // Adjust crop rectangle to match mouse wheel:
            // Adjust height & width of crop rectangle by the crop factor:
                display_width_pixels  = Convert.ToInt32( width  * crop_factor_X  - crop_pen_size_pixels * 2);
                if( display_width_pixels < 5 )      display_width_pixels = 5;
                display_height_pixels = Convert.ToInt32( height * crop_factor_Y  - crop_pen_size_pixels * 2 ) ;
                if( display_height_pixels < 5 )     display_height_pixels = 5;

            // Adjust crop rectangle upper left x & y as a function of crop rectangle that will be centered within target image:
                display_x_pixels =  Convert.ToInt32( width  / 2.0 -  display_width_pixels / 2.0  + crop_shift_pixelX );
                display_y_pixels =  Convert.ToInt32( height / 2.0 -  display_height_pixels / 2.0 + crop_shift_pixelY );

            display_rectangle.Width =    display_width_pixels;
            display_rectangle.Height =   display_height_pixels;
            display_rectangle.X  =   display_x_pixels;
            display_rectangle.Y =    display_y_pixels;
    }

0 个答案:

没有答案