一.控件效果说明
二.代码声明(已经循环读取10000次)
全局
OpenCvSharp.Point point = new OpenCvSharp.Point(0, 0);
OpenCvSharp.Size size2;
Mat src = new Mat();
初始化
size2 = new OpenCvSharp.Size(pictureBox1.Size.Width, pictureBox1.Size.Height);
按钮代码
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "图文件(*.*)|*.jpg;*.png;*.jpeg;*.bmp";
openFileDialog.RestoreDirectory = true;
openFileDialog.Multiselect = false;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
src = Cv2.ImRead(openFileDialog.FileName);
//适应图片
Mat showmat = new Mat();
Cv2.Resize(src, showmat, size2);
pictureBox1.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(showmat);
Cv2.WaitKey(100);
src.Dispose();
pictureBox1.Update();
pictureBox1.Image.Dispose();
showmat.Dispose();
}
总体代码实例:
using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace C1_6
{
public partial class Demo2_adaptwindow : Form
{
OpenCvSharp.Point point = new OpenCvSharp.Point(0, 0);
OpenCvSharp.Size size2;
Mat src = new Mat();
public Demo2_adaptwindow()
{
InitializeComponent();
size2 = new OpenCvSharp.Size(pictureBox1.Size.Width, pictureBox1.Size.Height);
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "图文件(*.*)|*.jpg;*.png;*.jpeg;*.bmp";
openFileDialog.RestoreDirectory = true;
openFileDialog.Multiselect = false;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
src = Cv2.ImRead(openFileDialog.FileName);
//适应图片
Mat showmat = new Mat();
Cv2.Resize(src, showmat, size2);
pictureBox1.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(showmat);
Cv2.WaitKey(100);
src.Dispose();
pictureBox1.Update();
pictureBox1.Image.Dispose();
showmat.Dispose();
}
}
}
}