C # picture taken from a portion of the image, and returns the image taken

        ///  <Summary> 
        /// taken from a portion of the picture image
         ///  </ Summary> 
        ///  <param name = "fromImagePath"> Source picture address </ param>         
        ///  <param name = "nX" > offset from the start X coordinate position taken </ param> 
        ///  <param name = "nY"> offset from the Y coordinate of the start position taken </ param> 
        ///  <param name = "toImagePath"> save the image address </ param> 
        ///  <param name = "width"> save the image width </ param> 
        ///  <param name = "height"> highly conserved image </ param> 
        ///  <Returns> </ returns>
        public void CaptureImage(string fromImagePath, intnX, int nY, String toImagePath, int width, int height) 
        { 
            // original image file 
            Image fromImage = Image.FromFile (fromImagePath);
             // Create a new Bitmap 
            Bitmap Bitmap = new new Bitmap (width, height);
             // Create plot area 
            Graphics Graphic = Graphics.FromImage (Bitmap);
             // taken picture region corresponding to the write mapping region 
            graphic.DrawImage (fromImage, 0 , 0 , new new the Rectangle (nX, nY, width, height), the GraphicsUnit. Pixel);
            // generated from a plot area of the new map 
            Image saveimage = Image.FromHbitmap (bitmap.GetHbitmap ());
             // save the image 
            saveImage.Save (toImagePath, ImageFormat.Png);
             // release resources    
            saveImage.Dispose (); 
            Graphic. the Dispose (); 
            bitmap.Dispose (); 
        }
        public  static Image CaptureImage (Image fromImage, int offsetX, int offsetY, int width, int height) 
        { 
            // create a new Bitmap 
            Bitmap Bitmap = new new Bitmap (width, height);
             // create a mapping region 
            Graphics Graphic = Graphics. FromImage (Bitmap);
             // taken picture region corresponding to the write mapping region 
            graphic.DrawImage (fromImage, 0 , 0 , new new the Rectangle (offsetX, offsetY, width, height), GraphicsUnit.Pixel);
             // from the mapping area generate a new map
            Saveimage = Image Image.FromHbitmap (bitmap.GetHbitmap ());          
             // release resources   
             // saveImage.Dispose (); 
            graphic.Dispose (); 
            bitmap.Dispose (); 
            return saveimage; 
        } 

        ///  <Summary> 
        // / interception of the picture area, the picture taken returned
         ///  </ Summary> 
        ///  <param name = "srcImage"> </ param> 
        ///  <param name = "POS"> </ param> 
        ///  <param name = "cutWidth"> </ param> 
        ///  <param name = "cutHeight"> </ param> 
        ///  <Returns> </ Returns>
        public  staticCutImage Image (Image srcImage, int nX, int nY, int cutWidth, int cutHeight) 
        { 
            Image cutedImage = null ;
             // initialize a bitmap object, after storing the image taken 
            Bitmap bmpDest = new new Bitmap (cutWidth, cutHeight, PixelFormat .Format32bppRgb); 
            Graphics G = Graphics.FromImage (bmpDest);
             // rectangle defined, vertex position to the left on the intercepted image to be taken and the taken image area size of 
            the rectangle rectSource = new new the rectangle (nX, nY, cutWidth , cutHeight);
             //Rectangle defined, should be taken to position the image area drawing bitmap size and initialized
             // rectDest described, will be taken area, from left vertex start drawing the bitmap, the size of the original drawing area taken 
            the Rectangle rectDest = new new the Rectangle ( 0 , 0 , cutWidth, cutHeight);
             // the first parameter is the object you want to load the image taken, and the second and third parameters are as defined earlier taken image rendering process and associated attributes, the fourth attribute property value measurement unit that can be used 
            g.drawImage (srcImage, rectDest, rectSource, GraphicsUnit.Pixel); 
            PixelProcess (bmpDest); 
            // displaying the intercepted image on the GUI 
            cutedImage = (image) bmpDest; 
            g.Dispose (); 
            return cutedImage; 
        } 
        // image pixel through 
        public  static  void PixelProcess(Bitmap bmp)
        {
            Color colorTransparent = bmp.GetPixel(0, 0);
            bmp.MakeTransparent(colorTransparent);
        }

 

Guess you like

Origin www.cnblogs.com/yc1224/p/11670938.html