C # implements automatic cutting of pictures

Source: Internet
Author: User

As a TD game requires some picture material, but the existing download from the Internet < to protect the radish > image resources are multiple pictures together, and there is no rules, although there is an XML file seems to describe the image of the picture in the location of the image of the information, But because I don't want to spend too much time studying the contents of this XML file, I change my mind to write a tool that automatically splits images based on transparent boundaries.

realized, basically meet the needs.

Main interface:

Qie () is the beginning of the transduction first function, traversing the picture each pixel, to find not transparent on the start of the cut graph.

    void Qie ()        {for                      (int x = 0; x < BMP. Width; x + +)            {for                (int y = 0; y < bmp. Height; y++)                {                    if (IsT (x, y))                    {                    }                    else                    {                        int maxy = 0, MaxX = 0, MinX = 9999, miny = 9999;                        Startqie (x,y,ref minx,ref maxx,ref miny,ref maxy);                        if (Maxy! = 0)                        {                            jianqie (MinX, MaxX, Miny, Maxy)                        ;            }        }}}

Startqie is a recursive function that looks for pixels that are connected to the current pixel and is not transparent, to determine if her position is larger than the current range, and to update the current maximum range.

The size of the last cut image is the maximum range.

    void Startqie (int x2,int Y2,Refint MinX,Refint MaxX,Refint Miny,RefIntMaxy) {list<point> PS =New list<point>(); Find (x2, Y2,RefPS);foreach (var aInchPS) {if (a.x < MinX) MinX =a.x;if (A.y < miny) Miny =A.Y;if (a.x > MaxX) MaxX =a.x;if (A.y > Maxy) Maxy = A.y;} //return; for (int x = MinX; x <= MaxX; x++for (int y = miny; y <=maxY; y++if (Conten (PS, x, y) continue; if (IsT (x, y) = = falseref MinX, ref MaxX, ref miny, ref Maxy);}} } } 
This function is used to cut the image, and to cut out the sub-graph from the master graph and save the local according to the given range.

void Jianqie (int MinX,int MaxX,int Miny,IntMaxy) {i++;var w = MaxX-MinX;var h = maxy-Miny;var img =NewBitmap (W,H); Graphics g =Graphics.fromimage (IMG); G.pageunit =GraphicsUnit.Pixel; G.drawimage (BMP,new RectangleF (0, 0, W, h), new RectangleF (MinX, Miny, W, h), GraphicsUnit.Pixel ); Img. Save (Bp+ "/" + i + ".png" for (int x = MinX; x <= MaxX; x++for (int y = miny; y <= maxy; y++) {BMP. SetPixel (x,y,color.transparent); } } }

This is the last more important function, defined in 8 directions, the Find method is used to find the connected and opaque pixels in 8 directions, and is added to the list.

  int[,] Csz =Newint[8,2] {{0,1}, {0,-1}, {-1,0}, {1,0}, {1,1}, {1,-1}, {-1,-1}, {-1,1} };void Find (int x,int y,Ref list<point>PS) {for (int i =0; I <8; i++) {int zy = Csz[i,0];int sx = csz[i, 1var nx=x+1*zy; var ny=y+1*SX; if (Conten (ps,nx,ny) ==1 & & NX < BMP. Width && ny >-1 && NY < BMP. Height && IsT (NX, NY) ==false) {PS. ADD (new point (Nx,ny)); Find (NX, NY, ref PS)}}}     

Small software, sometimes useful. The current insufficiency is: cannot automatically recognize the small element, this actually is very good solves, but at that time the goal already basic realization then did not spend the time to solve this problem.

Cut out the diagram:

C # implements automatic cutting of 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.