diff --git a/TIASshot/Form1.Designer.cs b/TIASshot/Form1.Designer.cs index f2c0245..bfeeb14 100644 --- a/TIASshot/Form1.Designer.cs +++ b/TIASshot/Form1.Designer.cs @@ -346,6 +346,7 @@ this.Text = "Form1"; this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing); this.Load += new System.EventHandler(this.Form1_Load); + this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint); ((System.ComponentModel.ISupportInitialize)(this.picPreview)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.picDisplay)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.icImagingControl1)).EndInit(); diff --git a/TIASshot/Form1.cs b/TIASshot/Form1.cs index 67012b2..90b801c 100644 --- a/TIASshot/Form1.cs +++ b/TIASshot/Form1.cs @@ -22,7 +22,8 @@ private LightSource _light = new LightSource(); private bool _isLightOn = true; private PreviewMonitor _previewMonitor; - private Bitmap _lastBmpShow = null; + private Bitmap _dispBuf = null; + private bool _isUpdateBuf = false; /// @@ -89,7 +90,7 @@ } _previewMonitor = new PreviewMonitor(); - _previewMonitor.Show(); + if (_previewMonitor.IsShown) _previewMonitor.Show(); } /// @@ -133,25 +134,31 @@ /// /// 画像表示 + /// ワーカースレッドから呼び出される /// /// public void ShowImage(Bitmap bmp) { - // なぜかわからないがここはInvokeなしでworkerスレッドから呼び出しできる - //if (InvokeRequired) { - // Invoke((MethodInvoker)delegate { ShowImage(bmp); }); - // return; - //} + var bmp2 = new Bitmap(bmp); + _dispBuf = bmp; + _isUpdateBuf = true; + Invalidate(); - if (_previewMonitor.IsShown) { - var bmpShow = (Bitmap)bmp.Clone(); - _previewMonitor.ShowFrame(bmpShow); - if (_lastBmpShow != null) { - _lastBmpShow.Dispose(); - } - _lastBmpShow = bmpShow; + if (_previewMonitor!=null && _previewMonitor.IsShown) { + _previewMonitor.UpdateImage(bmp2); } + } - picDisplay.Image = bmp; + /// + /// プレビューウィンドウの再描画 + /// + /// + /// + private void Form1_Paint(object sender, PaintEventArgs e) { + if (!_isUpdateBuf || _dispBuf == null) return; + + picDisplay.Image?.Dispose(); + picDisplay.Image = _dispBuf; + _isUpdateBuf = false; } /// diff --git a/TIASshot/IScam.cs b/TIASshot/IScam.cs index d395190..08823d7 100644 --- a/TIASshot/IScam.cs +++ b/TIASshot/IScam.cs @@ -105,7 +105,12 @@ using (Mat img = Mat.FromPixelData(frameType.Height, frameType.Width, MatType.CV_8UC3, buffer.GetIntPtr())) { using (Mat imgt_full = img.T()) { using (var imgt = new Mat(imgt_full, _roi)) { - _bmps[_bmpIndex] = imgt.ToBitmap(); + //_bmps[_bmpIndex] = imgt.ToBitmap(); + //using (Bitmap bmp = imgt.ToBitmap()) { + // _form.ShowImage(bmp); + //} + _form.ShowImage(imgt.ToBitmap()); + if (_calibrating > 0) { var whitePatch = Cv2.Mean(imgt, _chartMasks[12]); @@ -141,12 +146,12 @@ } } } - _form.ShowImage(_bmps[_bmpIndex]); - _bmpIndex = (_bmpIndex + 1) % 2; - if (_bmps[_bmpIndex] != null) { - _bmps[_bmpIndex].Dispose(); - _bmps[_bmpIndex] = null; - } + //_form.ShowImage(_bmps[_bmpIndex]); + //_bmpIndex = (_bmpIndex + 1) % 2; + //if (_bmps[_bmpIndex] != null) { + // _bmps[_bmpIndex].Dispose(); + // _bmps[_bmpIndex] = null; + //} return FrameQueuedResult.ReQueue; } diff --git a/TIASshot/PreviewMonitor.Designer.cs b/TIASshot/PreviewMonitor.Designer.cs index 0ebba79..1191db6 100644 --- a/TIASshot/PreviewMonitor.Designer.cs +++ b/TIASshot/PreviewMonitor.Designer.cs @@ -95,7 +95,6 @@ this.label4.Size = new System.Drawing.Size(345, 19); this.label4.TabIndex = 4; this.label4.Text = "Tongue Image Analyzing System"; - this.label4.Click += new System.EventHandler(this.label4_Click); // // label5 // @@ -127,6 +126,7 @@ this.Text = "PreviewMonitor"; this.TopMost = true; this.Load += new System.EventHandler(this.PreviewMonitor_Load); + this.Paint += new System.Windows.Forms.PaintEventHandler(this.PreviewMonitor_Paint); ((System.ComponentModel.ISupportInitialize)(this.picPreview)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit(); this.ResumeLayout(false); diff --git a/TIASshot/PreviewMonitor.cs b/TIASshot/PreviewMonitor.cs index 6e10da2..2abdcff 100644 --- a/TIASshot/PreviewMonitor.cs +++ b/TIASshot/PreviewMonitor.cs @@ -11,34 +11,60 @@ namespace TIASshot { public partial class PreviewMonitor : Form { - public bool IsShown { get; private set; } = false; + private Point _location; + private Size _size = new Size(0, 0); + private Bitmap _dispBuf; + private bool _isUpdateBuf = false; + public bool IsShown => (_size.Width > 0); + + /// + /// プレビューウィンドウのコンストラクタ + /// public PreviewMonitor() { InitializeComponent(); - Visible = false; - } - - private void PreviewMonitor_Load(object sender, EventArgs e) { - // プレビューウィンドウの位置とサイズを設定 + // プレビューウィンドウの位置とサイズを取得 foreach (var scr in Screen.AllScreens) { if (scr.Primary) continue; // プライマリスクリーンは除外 if (scr.Bounds.Width <= Config.GetInt("Shot/PreviewMonitorWidth")) { - Location = new Point(scr.Bounds.X, scr.Bounds.Y); - this.Size = scr.Bounds.Size; - IsShown = true; - Visible = true; + _location = scr.Bounds.Location; + _size = scr.Bounds.Size; } } } - public void ShowFrame(Bitmap bmp) { - if (!IsShown) return; - picPreview.Image = bmp; + /// + /// プレビューウィンドウのロードイベント + /// + /// + /// + private void PreviewMonitor_Load(object sender, EventArgs e) { + // プレビューウィンドウの位置とサイズを設定 + Location = _location; + Size = _size; } - private void label4_Click(object sender, EventArgs e) { + /// + /// プレビューウィンドウの表示状態を更新 + /// + /// + public void UpdateImage(Bitmap image) { + _dispBuf = image; + _isUpdateBuf = true; + Invalidate(); + } + /// + /// プレビューウィンドウの再描画イベント + /// + /// + private void PreviewMonitor_Paint(object sender, PaintEventArgs e) { + if (!IsShown || !_isUpdateBuf || _dispBuf == null) return; + + picPreview.Image?.Dispose(); + picPreview.Image = _dispBuf; + _isUpdateBuf = false; } } }