C # Image trim, scale, rotate, convert to mouse cursor

Source: Internet
Author: User

        =======================================================//Image clipping, zooming, converting to mouse cursor//======================        =================================///<summary>////////capture a new image from image pic///</summary> Public Bitmap getrect (image pic, Rectangle Rect) {//create image Rectangle DrawRect = new Rec  Tangle (0, 0, rect.width, rect.height);           Draw the whole block area Bitmap tmp = new Bitmap (drawrect.width, drawrect.height);                   Creates a bitmap by the specified size//draws the Graphics g = graphics.fromimage (TMP);                    Create a Graphics object from a bitmap g.clear (Color.FromArgb (0, 0, 0, 0));   Empty G.drawimage (pic, DrawRect, Rect, GraphicsUnit.Pixel);     Draw the return TMP from a given area of pic;        Returns the new image that was built}////<summary>///////From image pic Capture area rect build to drawrect size image///</summary>       Public Bitmap Getrectto (Image pic, Rectangle Rect, Rectangle drawrect) {     Create image Bitmap tmp = new Bitmap (drawrect.width, drawrect.height);                   Creates a bitmap by the specified size//draws the Graphics g = graphics.fromimage (TMP);                    Create a Graphics object from a bitmap g.clear (Color.FromArgb (0, 0, 0, 0));   Empty G.drawimage (pic, DrawRect, Rect, GraphicsUnit.Pixel);     Draw the return TMP from a given area of pic; Return the new image that was built}

        <summary>///Zoom in image pic, scale resize///</summary> public Bitmap Shrinkto (Ima GE pic, float reSize) {size S = new Size ((int) (pic. Width * reSize), (int) (pic.            Height * reSize));            Rectangle Rect = new Rectangle (new point (0, 0), S);        Return Shrinkto (pic, Rect); }///<summary>///Zoom to image pic For a new image of rect size///</summary> public Bitmap Shrink                   to (image pic, Rectangle Rect) {//create image Bitmap TMP = new Bitmap (rect.width, rect.height);  Creates a bitmap by the specified size Rectangle drawrect = new Rectangle (0, 0, rect.width, rect.height); Draw the entire block area Rectangle srcrect = new Rectangle (0, 0, pic. Width, pic.     Height);                   Entire area of PIC//Draw Graphics G = graphics.fromimage (TMP);                    Create a Graphics object from a bitmap g.clear (Color.FromArgb (0, 0, 0, 0)); Empty G.drawimage (pic, drawRect, Srcrect, graphicsunit.pixel);//Draw return TMP from a given area of pic; Return the new image that was built}

        <summary>//rotate any angle of the image/////</summary> public Bitmap Rotate (Bitmap bmp, FL        Oat angle) {return Rotate (BMP, Angle, color.transparent); }///<summary>//rotation at any angle, this function is not original///</summary> public Bitmap Rotate (Bitmap bmp, float angle, Color bkcolor) {int w = bmp.            Width + 2; int h = BMP.            Height + 2;            PixelFormat PF;            if (Bkcolor = = color.transparent) {pf = Pixelformat.format32bppargb; } else {pf = bmp.            PixelFormat;            } Bitmap tmp = new Bitmap (W, H, pf);            Graphics g = graphics.fromimage (TMP);            G.clear (Bkcolor);            G.drawimageunscaled (BMP, 1, 1);            G.dispose ();            GraphicsPath path = new GraphicsPath (); Path.            AddRectangle (New RectangleF (0f, 0f, W, h)); Matrix Mtrx = new Matrix ();            Mtrx.            Rotate (angle); RectangleF RCT = path.            GetBounds (MTRX); Bitmap DST = new Bitmap ((int) RCT. Width, (int) RCT.            Height, PF);            g = Graphics.fromimage (DST);            G.clear (Bkcolor); G.translatetransform (-RCT. X,-RCT.            Y);            G.rotatetransform (angle);            G.interpolationmode = Interpolationmode.highqualitybilinear;            g.drawimageunscaled (TMP, 0, 0);            G.dispose (); Tmp.            Dispose ();        return DST;            }//Get image of pic rotation angle angle After image public Bitmap Rotate2 (image pic, float angle) {//Create image int size = pic. Width > Pic. Height? Pic. Width * 3:pic.            Height * 3;                           Bitmap tmp = new Bitmap (size, size); Creates a bitmap by the specified size Rectangle Rect = new Rectangle (0, 0, pic. Width, pic.   Height);                   Entire area of PIC//Draw Graphics G = graphics.fromimage (TMP); Create a Graphics object from a bitmap g.clear (color.fromArgb (0, 0, 0, 0));  Empty G.translatetransform (RECT.WIDTH/2, RECT.HEIGHT/2);                               Set to rotate g.rotatetransform (angle) around the center; Controls the rotation angle point pos = new ((int) (size-pic. Width)/2), (int) (size-pic.  Height)/2));                                  Center alignment g.drawimage (pic, POS);     Draw the image G.translatetransform (-RECT.WIDTH/2,-RECT.HEIGHT/2);//Restore the anchor point to the upper left corner return TMP; Return the new image that was built}

        <summary>        ///image flips along the y-axis        ///</summary> public Bitmap flipy (Bitmap pic)        {            pic. RotateFlip (rotatefliptype.rotatenoneflipy);            return pic;        }        <summary>        ///image flips along the x        -axis///</summary> public Bitmap flipx (Bitmap pic)        {            pic. RotateFlip (ROTATEFLIPTYPE.ROTATENONEFLIPX);            return pic;        }

        <summary>        ////create mouse cursor from a given image        ////</summary> public cursor getcursor (Bitmap pic)        {            try {return new Cursor (pic. Gethicon ()); }         //Create mouse icon from bitmap            catch (Exception e) {return cursors.default;}        }        <summary>        ///Get image pic, create mouse cursor by specified size width        ///</summary> public cursor getcursor (image pic, int width)        {            Bitmap icon = new Bitmap (width, width);             Creates a bitmap by the specified size            Graphics g = graphics.fromimage (icon);              Create a Graphics object from a bitmap            g.clear (Color.FromArgb (0, 0, 0, 0));            G.drawimage (pic, 0, 0, icon. Width, icon. Height);    Draw image Bitmap            //bitmap icon = new Bitmap (tiles[toolspostion.y-1]);            try {return new Cursor (icon). Gethicon ()); }         //Create mouse icon from bitmap            catch (Exception e) {return cursors.default;}        }

C # Image trim, scale, rotate, convert to mouse cursor

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.