(目前想到2种方法,读者如果有更好的方法,欢迎评论区交流)
思路1:先保存计算结果,再用另一个控件进行读取
using System;
using OpenCvSharp;
using OpenCvSharp.Extensions;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Media.Imaging;
using OpenCvSharp.Flann;
using System.Windows.Media;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//MessageBox.Show("请选择输入图片");
OpenFileDialog open = new OpenFileDialog();
open.Filter = "Open DataFile | *.bmp;*.jpg;";
open.Title = "Open DataFile";
open.AddExtension = false;
open.CheckFileExists = true;
open.DereferenceLinks = true;
open.Multiselect = false;
if (open.ShowDialog() == DialogResult.OK)
{
String path = open.FileName;
Mat image = Cv2.ImRead(path);
Mat[] BGR;
Cv2.Split(image, out BGR);
//创建三个bitmap对象,分别对应BGR三通道的图像数据
Bitmap B = BGR[0].ToBitmap();
Bitmap G = BGR[1].ToBitmap();
Bitmap R = BGR[2].ToBitmap();
//保存图片到磁盘
B.Save("./tu/B通道图像.bmp");
G.Save("./tu/G通道图像.bmp");
R.Save("./tu/R通道图像.bmp");
//调整图片大小以适应窗口
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
//显示图片
pictureBox1.Image = Image.FromFile(open.FileName);
comboBox1.Enabled = true;
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
//comboBox1.SelectedIndex 获取或设置指定当前选定项的索引。
//MessageBox.Show("当前选择框的选中值:" + (comboBox1.SelectedIndex + 1).ToString());
//显示图片
if (comboBox1.SelectedIndex == 0)
{
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox1.Image = Image.FromFile("./tu/B通道图像.bmp");
}
if (comboBox1.SelectedIndex == 1)
{
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox1.Image = Image.FromFile("./tu/G通道图像.bmp");
}
if (comboBox1.SelectedIndex == 2)
{
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox1.Image = Image.FromFile("./tu/R通道图像.bmp");
}
}
}
}
方法2:声明一个变量
using System;
using OpenCvSharp;
using OpenCvSharp.Extensions;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Media.Imaging;
using OpenCvSharp.Flann;
using System.Windows.Media;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
Bitmap B;
Bitmap G;
Bitmap R;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//MessageBox.Show("请选择输入图片");
OpenFileDialog open = new OpenFileDialog();
open.Filter = "Open DataFile | *.bmp;*.jpg;";
open.Title = "Open DataFile";
open.AddExtension = false;
open.CheckFileExists = true;
open.DereferenceLinks = true;
open.Multiselect = false;
if (open.ShowDialog() == DialogResult.OK)
{
String path = open.FileName;
Mat image = Cv2.ImRead(path);
Mat[] BGR;
Cv2.Split(image, out BGR);
//创建三个bitmap对象,分别对应BGR三通道的图像数据
B = BGR[0].ToBitmap();
G = BGR[1].ToBitmap();
R = BGR[2].ToBitmap();
//调整图片大小以适应窗口
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
//显示图片
pictureBox1.Image = Image.FromFile(open.FileName);
comboBox1.Enabled = true;
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
//comboBox1.SelectedIndex 获取或设置指定当前选定项的索引。
//MessageBox.Show("当前选择框的选中值:" + (comboBox1.SelectedIndex + 1).ToString());
//显示图片
if (comboBox1.SelectedIndex == 0)
{
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox1.Image = B;
}
if (comboBox1.SelectedIndex == 1)
{
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox1.Image = G;
}
if (comboBox1.SelectedIndex == 2)
{
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox1.Image = R;
}
}
}
}