C#的winform的中委托显示图片
生活随笔
收集整理的這篇文章主要介紹了
C#的winform的中委托显示图片
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一、背景
主窗體的程序執(zhí)行完成后,會(huì)保留一些過程圖片,子窗體中的按鈕會(huì)調(diào)用顯示這些圖片,并將這些圖片顯示在主窗體的picturebox中;
二、代碼實(shí)現(xiàn)
1、子窗體的核心代碼
//委托傳圖片public delegate void DelegetShowImage(Bitmap _bitmap);public event DelegetShowImage delegetShowImage;//利用委托,在主窗體中顯示圖片private void button1_Click(object sender, EventArgs e){if (public_img.srcImg_0 == null|| public_img.srcImg_0.IsDisposed){MessageBox.Show("請(qǐng)先讀取一張圖片", "提示");return;}Mat srcImg = public_img.srcImg_0.Clone();Bitmap bitmap = BitmapConverter.ToBitmap(srcImg);delegetShowImage(bitmap);//pictureBox1.Image = bitmap;}2、主窗體的核心代碼
//主窗體的代碼 private void Form1_Load(object sender, EventArgs e){Bitmap bitmap = new Bitmap(pictureBox1.Image);srcImg = BitmapConverter.ToMat(bitmap);na.ZGTotalOffLineWithDetail(srcImg);public_img.srcImg_0 = srcImg;splitContainer4.Panel2.Controls.Clear();junior_test panel_test_form = new junior_test();//委托事件panel_test_form.delegetShowImage += delegateShowPictrueBox;//panel_test_form.delegetShowText += delegateShowTextbox;panel_test_form.savePic += savePic;panel_test_form.TopLevel = false;panel_test_form.MaximizeBox = true;panel_test_form.Dock = DockStyle.Fill;panel_test_form.FormBorderStyle = FormBorderStyle.None;splitContainer4.Panel2.Controls.Add(panel_test_form);panel_test_form.Show();splitContainer4.Panel2.AutoSizeMode = AutoSizeMode.GrowOnly;splitContainer4.Panel2.AutoScroll = true;//顯示當(dāng)前件號(hào)label6.Text = public_img.project_name;} //委托方法private void delegateShowPictrueBox(Bitmap _bitmap){//設(shè)置填充屬性pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;//顯示圖片pictureBox1.Image = _bitmap;}private void delegateShowTextbox(string text){//設(shè)置填充屬性//pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;//顯示圖片textBox1.AppendText(text);}private void savePic(string text){if (text != "save_pic") return;SaveFileDialog savedialog = new SaveFileDialog();savedialog.Title = "附件另存";savedialog.Filter = "jpg圖片|*.JPG|png圖片|*.PNG|Bmp 圖片|*.bmp|jpeg圖片|*.JPEG";savedialog.FilterIndex = 3;//設(shè)置默認(rèn)文件類型顯示順序 savedialog.RestoreDirectory = true; //點(diǎn)了保存按鈕進(jìn)入if (pictureBox1.Image != null){if (savedialog.ShowDialog() == DialogResult.OK){string pictureName = savedialog.FileName;//照片另存using (MemoryStream mem = new MemoryStream()){Bitmap bmp = new Bitmap(pictureBox1.Image);//保存到磁盤文件bmp.Save(@pictureName, pictureBox1.Image.RawFormat);bmp.Dispose();MessageBox.Show("附件另存成功!", "注意", MessageBoxButtons.OK, MessageBoxIcon.Information);}}}else{MessageBox.Show("沒有附件信息!", "注意", MessageBoxButtons.OK, MessageBoxIcon.Information);}}總結(jié)
以上是生活随笔為你收集整理的C#的winform的中委托显示图片的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 机器视觉---九点标定
- 下一篇: C++CLR类库封装Native类库并用