AForge 拍照 GetCurrentVideoFrame().GetHbitmap 内存不足

  使用AForge 控制高拍仪来拍照,遇到提示“内存不足”的错误,查了一些资料后,发现有个对象没有进行释放。

  以下红字部分即为 增加的销毁对象方法。


//

        int flag = 0;

        private void videoDevice_Acqution()
        {
            if (flag == 1) return;

            if (this.videoSourcePlayer.Visible == false)
            {
                this.kpImageViewer1.Visible = false;
                this.axWindowsMediaPlayer1.Visible = false;
                this.axFramerControl1.Visible = false;
                this.axWebBrowser1.Visible = false;
                this.videoSourcePlayer.Visible = true;
                this.videoSourcePlayer.BringToFront();
            }

                if (videoSourcePlayer.GetCurrentVideoFrame() == null) return;
                //校验是否为可扫项
                if (!this.checkIsScanable(this.treeView1.SelectedNode)) return;

                if (flag == 0)
                {
                    flag = 1;
                    Bitmap bmpNew = null;
                    string fileName = "";
                    string img = "";
                    Graphics g = null;
                    Bitmap bitmap = null;
                    try
                    {
                        IntPtr hbitmap = videoSourcePlayer.GetCurrentVideoFrame().GetHbitmap();
                        bitmap = System.Drawing.Image.FromHbitmap(hbitmap);
                        fileName = DateTime.Now.ToString("yyyyMMddhhmmssfff") + ".jpg";
                        img = this.docFolder + "/" + fileName;
                        bmpNew = new Bitmap(bitmap.Width, bitmap.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
                        g = Graphics.FromImage(bmpNew);
                        g.DrawImage(bitmap, 0, 0, bitmap.Width, bitmap.Height);
                        g.Dispose();
                        bitmap.Dispose();
                        //销毁对象 否则会内存溢出
                        DeleteObject(hbitmap);
                    }
                    catch {
                        flag = 0;
                        if (g != null)
                            g.Dispose();
                        if (bitmap != null)
                            bitmap.Dispose();
                        return;
                    }
                    //判断高拍仪设置的旋转角度
                    string rotate = "0";
                    if (videoSourcePlayer.Text.Equals("master")){
                        rotate = (String)master["rotate"];
                    }else if(videoSourcePlayer.Text.Equals("slave")){
                        rotate = (String)slave["rotate"];
                    }
                    if ("90".Equals(rotate.Trim())) {
                        bmpNew.RotateFlip(RotateFlipType.Rotate90FlipNone);
                    }else if("180".Equals(rotate.Trim())){
                        bmpNew.RotateFlip(RotateFlipType.Rotate180FlipNone);
                    }else if("270".Equals(rotate.Trim())){
                        bmpNew.RotateFlip(RotateFlipType.Rotate270FlipNone);
                    }
                    bmpNew.Save(img, ImageFormat.Jpeg);
                    
                    this.addNewTreeNode(fileName);

                    //更新状态栏信息start            
                    calJnxxScanPro(false);
                    //更新状态栏信息end
                    flag = 0;
                }
               
            }

        [System.Runtime.InteropServices.DllImport("gdi32.dll")]
        public static extern bool DeleteObject(IntPtr hObject);

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值