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;
}