My friend zgke provided me with the source code for screenshots and background removal. Do you have any better comments or suggestions for communication?

Source: Internet
Author: User

I want

And remove the red background.

The Code provided by a friend is as follows:

 

Only in one program. CS, note that the file path corresponds to the name (if you want to try Program Correctness)

Using system; using system. collections. generic; using system. LINQ; using system. text; using system. drawing; using system. drawing. imaging; using system. runtime. interopservices; namespace zgkeprogram {class program {static void main (string [] ARGs) {bitmap _ newbitmap = (Bitmap) image. fromfile (@ "C: \ Documents and Settings \ Administrator \ Desktop \ eegtest.bmp"); // create the original image from the specified location // create the image data bitmapdata _ DATA = _ newbitmap. lo Ckbits (New rectangle (0, 0, _ newbitmap. width, _ newbitmap. height), imagelockmode. readonly, _ newbitmap. pixelformat); // create an RGB data stream with the size of _ data. stride row byte [] _ colorbytes = new byte [_ data. stride]; // the starting position of the row 1st is _ data. scan0 graph 1st rows are black. You cannot use 2nd rows _ startptr as the starting position of 2nd rows intptr _ startptr = new intptr (_ data. scan0.toint32 () + _ data. stride); // copy the data to the RGB stream marshal. copy (_ startptr, _ colorbytes, 0, _ data. stride); // release lock _ n Ewbitmap. unlockbits (_ data); int _ startwidth = 0; // start position bool _ startbool = true; // used to determine whether int _ endwidth = 0 is found at the start position; // The Red ending position bool _ endbool = true; // determines whether int _ endindex = _ colorbytes is found at the ending position. length-1; // The index switch (_ newbitmap. pixelformat) {Case pixelformat. format32bppargb: // color depth 32-bit = 4 byte [] for (INT I = 0; I! = _ Newbitmap. width; I ++) {If (_ startbool) {// data stream is B g r ........ B g r A if the color r> = 200 and g B is smaller than 50, it is considered as red if (_ colorbytes [I * 4 + 2]> = 200 & _ colorbytes [I * 4 + 1] <= 50 & _ colorbytes [I * 4] <= 50) {_ startwidth = I; // record start position _ startbool = false; // The next loop is not in the position} If (_ endbool) {// the difference between this and above is that the index at the end position of the forward loop at the end position of B g r a is r-2 is G-3 is B. same as if (_ colorbytes [_ endindex -( I * 4)-1]> = 200 & _ colorbytes [_ endindex-(I * 4) -2] <= 50 & _ colorbytes [_ endindex-(I * 4)-3] <= 50) {_ endwidth = _ newbitmap. width-I; // record end position _ endbool = false ;}} if (! _ Endbool &&! _ Startbool) break;} // create a new image with a red starting position. 0: The Red ending position.-The red starting position. The image has a high bitmap _ savebitmap = _ newbitmap. clone (New rectangle (_ startwidth, 0, _ endwidth-_ startwidth, _ newbitmap. height), _ newbitmap. pixelformat); _ savebitmap. save (@ "C: \ Documents ents and Settings \ Administrator \ Desktop \ 111.bmp"); _ savebitmap. maketransparent (color. red); // remove the red background color. _ Savebitmap. Save (@ "C: \ Documents and Settings \ Administrator \ Desktop \ 123.bmp ");}}}

 

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.