Image segmentation program

Source: Internet
Author: User

Small image splitting system

You can divide a large image into several small images based on the size specified by you. For example, an image of 100*100 can be divided into 10 images of 100*10, can Save the current popular format. With the error log function, the background thread processes image Cutting
The younger brother attaches the source code for your reference. It is very crude. Can you share your ideas?

 

Source code: imagecutorsplit

 

The core code is as follows:

 

/// <Summary>
/// Image Segmentation
/// </Summary>
/// <Param name = "originalpath"> original image path </param>
/// <Param name = "portionwidth"> split image width </param>
/// <Param name = "portionheight"> height of the split image </param>
/// <Param name = "savepath"> Save path </param>
/// <Param name = "FILENAME"> file name </param>
/// <Param name = "format"> image format </param>
/// <Param name = "issave"> whether to save </param>
/// <Returns> return value </returns>
Public static image [] splitimage (string originalpath, int portionwidth, int portionheight, string savepath, string filename, imageformat format, bool issave)
{
Image [] images = NULL;
If (! File. exists (originalpath ))
Return images;
Using (Stream imgstream = file. openread (originalpath ))
{
Using (bitmap BMP = new Bitmap (imgstream ))
{
Images = Split (BMP, portionwidth, portionheight, savepath, filename, format, issave );
}
}
Return (image []) images. Clone ();
}
/// <Summary>
/// Split the image
/// </Summary>
/// <Param name = "IMG"> original image </param>
/// <Param name = "portionwidth"> split image width </param>
/// <Param name = "portionheight"> height of the split image </param>
/// <Param name = "savepath"> Save path </param>
/// <Param name = "FILENAME"> file name </param>
/// <Param name = "format"> image format </param>
/// <Param name = "issave"> whether to save </param>
/// <Returns> return value </returns>
Private Static Image [] Split (image IMG, int portionwidth, int portionheight, string savepath, string filename, imageformat format, bool issave)
{
Int imgindex = 0;
// How many new types of images can be divided into original images
Int maxheight = IMG. height;
Int maxwidth = IMG. width;
Int heightnumber = (maxheight % portionheight)> 0? (Maxheight/portionheight) + 1: (maxheight/portionheight );
Int widthnumber = (maxwidth % portionwidth)> 0? (Maxwidth/portionwidth) + 1: (maxwidth/portionwidth );
Int allnumber = heightnumber * widthnumber;

Image [] images = new image [allnumber];
Using (Bitmap imgsplitted = new Bitmap (portionwidth, portionheight ))
{
Using (Graphics G = graphics. fromimage (imgsplitted ))
{
For (INT y = 0; y <maxheight; y + = portionheight)
{
For (INT x = 0; x <maxwidth; x + = portionwidth)
{
Try
{
G. Clear (color. Transparent );
G. drawimage (IMG, new rectangle (0, 0, portionwidth, portionheight), X, Y, portionwidth, portionheight, graphicsunit. pixel );
G. Save ();
If (issave)
{
If (! Directory. exists (savepath ))
{
Directory. createdirectory (savepath );
Thread. Sleep (1000 );
}
Imgsplitted. Save (path. Combine (savepath, filename + imgindex. tostring () + "." + format. tostring (), format );
}
Images [imgindex] = imgsplitted. Clone () as image;
Imgindex ++;
}
Catch (exception ex)
{
Throw new logexception ("split", Ex. Message );
}
}
}
}
}
Return images;
}

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.