C # image Cutting

Source: Internet
Author: User

Image cutting is to cut a large image into multiple small images according to user requirements. In the DOTNET environment, the system provides the GDI + class library, which provides a convenient interface for image operation and processing.

The following is an image cutting Applet:

Public class imagemanager
{
/// <Summary>
/// Image Cutting
/// </Summary>
/// <Param name = "url"> image file name </param>
/// <Param name = "width"> width of the image after cutting </param>
/// <Param name = "height"> height of the image after cutting </param>
/// <Param name = "savepath"> Save path of the image file after cutting </param>
/// <Param name = "fileext"> image file extension after cutting </param>
Public static void cut (string URL, int width, int height, string savepath, string fileext, string logofile)
{
Bitmap bitmap = new Bitmap (URL );
Decimal maxrow = math. Ceiling (decimal) bitmap. Height/height );
Decimal maxcolumn = math. Ceiling (decimal) bitmap. width/width );
For (decimal I = 0; I <maxrow; I ++)
{
For (decimal J = 0; j <maxcolumn; j ++)
{
String filename = I. tostring () + "," + J. tostring () + "." + fileext;
Bitmap BMP = new Bitmap (width, height );

For (INT offsetx = 0; offsetx <width; offsetx ++)
{
For (INT offsety = 0; offsety {
If (J * width + offsetx) <bitmap. width) & (I * height + offsety) <bitmap. Height ))
{
BMP. setpixel (offsetx, offsety, bitmap. getpixel (INT) (J * width + offsetx), (INT) (I * height + offsety )));
}
}
}
Graphics G = graphics. fromimage (BMP );
G. drawstring ("zhehui technology", new font ("", 20), new solidbrush (color. fromargb (70, color. whitesmoke), 60, height/2); // Add a watermark
Imageformat format = imageformat. PNG;
Switch (fileext. tolower ())
{
Case "PNG ":
Format = imageformat. PNG;
Break;
Case "BMP ":
Format = imageformat. BMP;
Break;
Case "GIF ":
Format = imageformat. gif;
Break;

}
BMP. Save (savepath + "//" + filename, format );
}
}

}


}

 

Programmers only need to call the cut function to apply it.

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.