C # + WinForm + EMGUCV Learn II: Create and manipulate pictures

Source: Internet
Author: User

The procedure is as follows;

Using system;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 Emgu.CV;using Emgu.cv.cvenum;using emgu.cv.structure;using system.xml;using system.xml.serialization;using System.IO;namespace windowsformsapplication1{public partial class Form1:form {public Form1 () {Initializeco        Mponent (); }///<summary>///show blue///</summary>//<param name= "Sender" ></param        >//<param name= "E" ></param> private void Button1_Click (object sender, EventArgs e)            {IMAGE&LT;BGR, byte> img1 = new IMAGE&LT;BGR, byte> (new Bgr (255, 0, 0)); pictureBox1.Image = Img1.                    Tobitmap (); }///<summary>///Open a picture from the dialog box;///</summary>//<param name= "SenDer "></param>//<param name=" E "></param> private void button2_click (object sender, Ev Entargs e) {string strFileName = string.            Empty;            OpenFileDialog ofd = new OpenFileDialog (); if (OFD. ShowDialog () ==dialogresult.ok) {image<bgr, byte> img1 = new IMAGE&LT;BGR, byte> (OFD.                FileName); pictureBox1.Image = Img1.            Tobitmap (); }}///<summary>//for pixel operation;///</summary>//<param name= "Sender" & gt;</param>//<param name= "E" ></param> private void Button3_Click (object sender, Eventar            GS e) {image<bgr, byte> img1 = new IMAGE&LT;BGR, byte> (New Bgr (250, 0, 0));            Byte B1 = 255;                 BGR yellow = new Bgr (0, 255, 255);//The RGB value can be interpreted as a vector overlay;//first for (int i=20;i<60;i++) { for (int j=20;j<60;j++) {img1.                    Data[i, j, 0] = 0; Img1.                    Data[i, j, 1] = B1; Img1.                Data[i, J, 2] = B1; }}//second for (int i=120;i<160;i++) {for (int j=20;j<60                ; j + +) {img1[i, j] = yellow; }}//third byte[,,] data = img1. Data;                    delivery address only; for (int i=20;i<60;i++) {for (int j=120;j<160;j++) {                    Data[i, j, 0] = 0;                    Data[i, j, 1] = B1;                Data[i, J, 2] = B1; }} picturebox1.image = Img1.        Tobitmap (); }///<summary>///image overlay;///</summary>//<param name= "Sender" ></param >//<param name= "E" ></param> private void Button4_Click (object sender, EventarGS e) {image<bgr, byte> imgblue = new IMAGE&LT;BGR, byte> (new Bgr (255, 0, 0));            IMAGE&LT;BGR, byte> imggreen = new IMAGE&LT;BGR, byte> (new Bgr (0, 255, 0));            IMAGE&LT;BGR, byte> imgred = new IMAGE&LT;BGR, byte> (new Bgr (0, 0, 255));            IMAGE&LT;BGR, byte> img1 = imgblue + Imggreen + imgred; pictureBox1.Image = Img1.        Tobitmap (); }///<summary>//change each pixel point;///</summary>//<param name= "Sender" ><        /param>//<param name= "E" ></param> private void Button5_click (object sender, EventArgs e) {//you need choose a gray picture Image<gray, byte> imggray = new Image<gray, BYTE&G            t; ("E:\\testpictures\\rice.png"); Single is one-precision floating-point number Image<gray, single> img1 = imggray.convert<single> (Delegate (Byte  b) {                  Return (single) Math.sin (b * b/255.0);            }            ); pictureBox1.Image = Img1.            Tobitmap (); Img1. Dispose ();//If you do not add this, the system will automatically log off the picture, but try to manually log out;}//<summary>//image to XML//</summary >//<param name= "sender" ></param>//<param name= "E" ></param> private V OID Button6_click (object sender, EventArgs e) {string strFileName = string.            Empty;            OpenFileDialog ofd = new OpenFileDialog (); if (OFD. ShowDialog () ==dialogresult.ok) {image<bgr, byte> img1 = new IMAGE&LT;BGR, byte> (OFD.                FileName); pictureBox1.Image = Img1.                                Tobitmap ();                Note References Systom.xml and IO, etc. StringBuilder SB1 = new StringBuilder (); (New XmlSerializer (typeof (IMAGE&LT;BGR, byte>)).                Serialize (New StringWriter (SB1), IMG1); XmlDocument XMLDoc = new XmlDocument (); Xmldoc.loadxml (SB1.                ToString ());                                                  Xmldoc.save ("Image.xml");        }}////<summary>//XML to image, need to load XML file, I use the last button generated XML file;///</summary> <param name= "Sender" ></param>///<param name= "E" ></param> private void button 7_click (object sender, EventArgs e) {string strFileName = string.            Empty;            OpenFileDialog ofd = new OpenFileDialog (); if (OFD.                ShowDialog () = = DialogResult.OK) {XmlDocument doc = new XmlDocument (); Doc. Load (OFD.                FileName); IMAGE&LT;BGR, byte> img1 = (IMAGE&LT;BGR, byte>) (New XmlSerializer (typeof (IMAGE&LT;BGR, byte> ))).                           Deserialize (new XmlNodeReader (DOC)); pictureBox1.Image = Img1.            Tobitmap (); }        }    }}

Operation Result:


The second program finishes.

C # + WinForm + EMGUCV Learn II: Create and manipulate pictures

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.