C # simple image processing

Source: Internet
Author: User
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 each frame in JPEG format
For (INT I = 0; I <count; I ++)
{
GIF. selectactiveframe (FD, I );
GIF. Save (psavedpath + "// Frame _" + I + ". jpg", imageformat. JPEG );
}
}

/** // <Summary>
/// Obtain the image thumbnail
/// </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, which can be 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) of the Start plot of the Target Image </param>
/// <Param name = "ppartstartpointy"> coordinate Y value (usually) of the Start plot of the Target Image </param>
/// <Param name = "ppartwidth"> width of the Target Image </param>
/// <Param name = "ppartheight"> height of the Target Image </param>
/// <Param name = "porigstartpointx"> coordinate X value at the start of the original image capture </param>
/// <Param name = "porigstartpointy"> coordinate Y value at the start of the original image capture </param>
/// <Param name = "pformat"> Save format, which can be 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 )); // Original Image Position (the size of the image captured from the original 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.