极品分享

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);



其中,销毁方法

//销毁对象 否则会内存溢出

DeleteObject(hbitmap);


 [System.Runtime.InteropServices.DllImport("gdi32.dll")]

 public static extern bool DeleteObject(IntPtr hObject);


2017-03-15 0 /
NET学习
/
标签: 

评论回复

回到顶部