private void ShowImage()
        {
            using var Trace = new Trace();  //This c# 8.0 using feature will auto dispose when the function is done.
            try
            {
                string lastimage = "";

                if (this._imageRes == null && this.Cam.ImageResolutions.Count > 0)
                {
                    this._imageRes = this.Cam.ImageResolutions[0];
                }

                if (this._imageRes != null)
                {
                    lastimage = this._imageRes.LastFileName;
                }

                //first check for saved image in cameras folder. If doesn't exist load the last camera image.
                if (this.pbMaskImage.Tag == null || !string.Equals(this.pbMaskImage.Tag.ToString(), lastimage, StringComparison.OrdinalIgnoreCase))
                {
                    this.pbMaskImage.Tag = lastimage;

                    if ((!string.IsNullOrWhiteSpace(lastimage)) && (File.Exists(lastimage)))
                    {
                        this._cameraLayer = new Bitmap(lastimage);

                        //merge layer if masks exist
                        if (File.Exists(this._maskfilename))
                        {
                            using (Bitmap maskLayer = new Bitmap(this._maskfilename))
                            {
                                this.pbMaskImage.Image = this.MergeBitmaps(this._cameraLayer, maskLayer);
                                this._transparentLayer = new Bitmap(this.AdjustImageOpacity(maskLayer, 2f)); // create new bitmap here to prevent file locks and increase to 100% opacity
                            }
                        }
                        else //if there are no masks
                        {
                            this.pbMaskImage.Image = new Bitmap(this._cameraLayer);
                            this._transparentLayer = new Bitmap(this.pbMaskImage.Image.Width, this.pbMaskImage.Image.Height, PixelFormat.Format32bppPArgb);
                        }
                    }
                    else
                    {
                        //lbl_imagefile.ForeColor = Color.Gray;
                        //this.Text += " (Missing)";
                    }
                }

                this.pbMaskImage.Refresh();
            }
            catch (Exception ex)
            {
                Log("Error: " + ex.Msg());
            }
        }
Beispiel #2
0
        public bool UpdateImageResolutions(ClsImageQueueItem CurImg)
        {
            bool ret = false;

            if (!CurImg.IsValid())
            {
                return(ret);
            }

            lock (CamLock)
            {
                ImageResItem newri = new ImageResItem(CurImg);

                int  idx     = this.ImageResolutions.IndexOf(newri);
                bool updated = false;

                if (idx > -1)
                {
                    if (CurImg.TimeCreated > this.ImageResolutions[idx].LastSeenDate)
                    {
                        updated = true;
                        this.ImageResolutions[idx].LastFileName = CurImg.image_path;
                        this.ImageResolutions[idx].LastSeenDate = CurImg.TimeCreated;
                        this.ImageResolutions[idx].LastFileSize = CurImg.FileSize;
                        this.ImageResolutions[idx].LastFileDPI  = CurImg.DPI;
                        this.ImageResolutions[idx].Count++;
                    }
                }
                else
                {
                    ret = true;
                    this.ImageResolutions.Add(newri);
                }

                //sort so most recent is at top of list

                if (ret || updated)
                {
                    this.ImageResolutions = this.ImageResolutions.OrderByDescending(x => x.LastSeenDate).ToList();
                }
            }

            return(ret);
        }
        private void UpdateRes()
        {
            using var Trace = new Trace();  //This c# 8.0 using feature will auto dispose when the function is done.

            cbResolution.Items.Clear();
            foreach (ImageResItem res in this.Cam.ImageResolutions)
            {
                cbResolution.Items.Add(res);
            }

            if (cbResolution.Items.Count > 0)
            {
                cbResolution.SelectedIndex = 0;
                if (cbResolution.SelectedValue != null)
                {
                    this._imageRes = (ImageResItem)cbResolution.SelectedValue;
                }
            }
        }
Beispiel #4
0
        public string GetMaskFile(bool MustExist, ClsImageQueueItem CurImg = null, ImageResItem ir = null)
        {
            string ret = "";

            lock (CamLock)
            {
                try
                {
                    String resstr = "";

                    if (CurImg != null)
                    {
                        resstr = $"_{CurImg.Width}x{CurImg.Height}";
                    }
                    else if (ir == null && this.ImageResolutions.Count > 0)
                    {
                        ir     = this.ImageResolutions[0]; //the first one should be the most recent image processed because of the sort.
                        resstr = $"_{ir.Width}x{ir.Height}";
                    }
                    else if (ir != null)
                    {
                        resstr = $"_{ir.Width}x{ir.Height}";
                    }

                    string CamMaskFile = "";
                    if (!string.IsNullOrEmpty(this.MaskFileName))
                    {
                        if (this.MaskFileName.Contains("\\") && this.MaskFileName.Contains("."))
                        {
                            CamMaskFile = this.MaskFileName;
                        }
                        else if (this.MaskFileName.Contains("."))
                        {
                            CamMaskFile = Path.Combine(Path.GetDirectoryName(AppSettings.Settings.SettingsFileName), $"{this.MaskFileName}");
                        }
                        else
                        {
                            CamMaskFile = Path.Combine(Path.GetDirectoryName(AppSettings.Settings.SettingsFileName), $"{this.MaskFileName}.bmp");
                        }

                        //Add WidthxHeight to filename
                        string ResFile = Path.Combine(Path.GetDirectoryName(CamMaskFile), $"{Path.GetFileNameWithoutExtension(CamMaskFile)}{resstr}.bmp");

                        bool resempty     = string.IsNullOrEmpty(resstr);
                        bool cammaskexist = File.Exists(CamMaskFile);
                        bool resexist     = File.Exists(ResFile);
                        if (!resempty && cammaskexist && !Path.GetFileName(CamMaskFile).Contains("_") && !resexist)
                        {
                            //lets rename it to appropriate ResFile name
                            string tmpresstr = "";
                            using (FileStream fileStream = new FileStream(CamMaskFile, FileMode.Open, FileAccess.Read))
                            {
                                using Image img = Image.FromStream(fileStream, false, false);
                                tmpresstr       = $"_{img.Width}x{img.Height}";
                            }
                            if (tmpresstr == resstr)
                            {
                                AITOOL.Log($"Debug: Renaming mask file from '{CamMaskFile}' to '{ResFile}'...");
                                File.Move(CamMaskFile, ResFile);
                            }
                            else
                            {
                                AITOOL.Log($"Debug: Cannot rename mask file because it does not match the current image resolution of '{resstr}' (!={tmpresstr}):  MaskFile='{CamMaskFile}'...");
                            }
                        }
                        else
                        {
                            //AITOOL.Log($"Debug: ResEmpty={resempty}, CamMaskExist={cammaskexist}, ResMaskFileExist={resexist}, CurRes={resstr}, MaskRes={resstr}, CamMaskFile='{CamMaskFile}', ResMaskFile={ResFile}...");
                        }

                        if (MustExist)
                        {
                            if (File.Exists(ResFile))
                            {
                                return(ResFile);
                            }
                            else
                            {
                                return("");
                            }
                        }
                        else
                        {
                            return(ResFile);
                        }
                    }
                }
                catch (Exception ex)
                {
                    AITOOL.Log("Error: " + ex.Msg());
                }
            }

            return(ret);
        }