C # some simple operations on images [ZT]

Source: Internet
Author: User

I have not updated for a while, and I am not enthusiastic about learning. I am posting a few tips on Image Processing today, hoping to help you:
(1)how to get each frame in .gif image?
(2) how to obtain the thumbnail of an image?
(3) how to "intercept" the specified area of the image?

Using System. drawing;
Using System. Drawing. drawing2d;
Using System. Drawing. imaging;

Public   Class Imagehelper
{
/**/ ///   <Summary>
/// Obtain the frames in the image.
///   </Summary>
///   <Param name = "ppath"> Image path </Param>
///   <Param name = "psavepath"> Save path </Param>
Public   Void Getframes ( String Ppath, String Psavedpath)
{
Image GIF = Image. fromfile (ppath );
Framedimension FD =   New Framedimension (GIF. framedimensionslist [ 0 ]);

// Get the number of frames (GIF images may contain multiple frames, and images of other formats are generally only one frame)
Int Count = GIF. getframecount (FD );

// Save frames in JPEG format
For ( Int I =   0 ; I < Count; I ++ )
{< br> GIF. selectactiveframe (FD, I);
GIF. save (psavedpath + " \ Frame _ " + I + " .. jpg " , imageformat. JPEG);
}
}

/**/ ///   <Summary>
/// Retrieve image thumbnails
///   </Summary>
///   <Param name = "ppath"> Image path </Param>
///   <Param name = "psavepath"> Save path </Param>
///   <Param name = "pwidth"> Thumbnail width </Param>
///   <Param name = "pheight"> Thumbnail height </Param>
///   <Param name = "pformat"> Save format, usually JPEG </Param>
Public   Void Getsmaller ( String Ppath, String Psavedpath, Int Pwidth, Int Pheight)
{
Try
{
Image smallerimg;
Image originalimg = Image. fromfile (ppath );
Image. getthumbnailimageabort callback =   New Image. getthumbnailimageabort (thumbnailcallback );
Smallerimg = Originalimg. getthumbnailimage (pwidth, pheight, callback, intptr. Zero );
Smallerimg. Save (psavedpath +   " \ Smaller.jpg " , Imageformat. JPEG );
}
Catch (Exception X)
{
//
}
}

/**/ ///   <Summary>
/// Obtain the specified part of the image
///   </Summary>
///   <Param name = "ppath"> Image path </Param>
///   <Param name = "psavepath"> Save path </Param>
///   <Param name = "ppartstartpointx"> Coordinate x value (usually) at the start of drawing the Target Image) </Param>
///   <Param name = "ppartstartpointy"> Coordinate Y value (usually) at the start of drawing the Target Image) </Param>
///   <Param name = "ppartwidth"> Target Image Width </Param>
///   <Param name = "ppartheight"> Target Image Height </Param>
///   <Param name = "porigstartpointx"> Coordinate X at the beginning of the original image capture </Param>
///   <Param name = "porigstartpointy"> Coordinate Y value at the beginning of the original image capture </Param>
///   <Param name = "pformat"> Save format, usually JPEG </Param>
Public   Void Getpart ( String Ppath, String Psavedpath, Int Ppartstartpointx, Int Ppartstartpointy, Int Ppartwidth, Int Ppartheight, Int Porigstartpointx, Int Porigstartpointy)
{
Image originalimg = Image. fromfile (ppath );

Bitmap partimg =   New Bitmap (ppartwidth, ppartheight );
Graphics graphics = Graphics. fromimage (partimg );
Rectangle destrect =   New Rectangle ( New Point (ppartstartpointx, ppartstartpointy ), New Size (ppartwidth, ppartheight )); // Target Location
Rectangle origrect =   New Rectangle ( New Point (porigstartpointx, porigstartpointy ), New Size (ppartwidth, ppartheight )); // Source image position (the size of the image captured from the source image is equal to the size of the target image by default)

Graphics. drawimage (originalimg, destrect, origrect, graphicsunit. pixel );
Partimg. Save (psavedpath +   " \ Part.jpg " , Imageformat. JPEG );
}

Public   Bool Thumbnailcallback ()
{
Return False;
}
}

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.