SoFunction
Updated on 2025-03-07

How to cut and save images in C#

Recently, I needed to upload a picture and cut it in the specified location. Later, I found a plug-in for cropping pictures online, but there is only the front desk and no backend. Then I used various Baidu and finally completed it. I wrote a blog to commemorate it.

I won’t talk about the front desk. I will use the cropper plug-in. If you are interested, go to Baidu to find it. I have this plugin.

Here is the code:

HttpPostedFile file = ["avatar_file"];
string datasize = ["avatar_data"];
//{"x":30.003846153846148,"y":16.715384615384625,"height":300.8,"width":300.8,"rotate":0} Parameters after clippingJavaScriptSerializer jss = new JavaScriptSerializer();
ImgSize imagesize = <ImgSize>(datasize);
byte[] FileByte = SetFileToByteArray(file);//Picture arraystring strtExtension = ();//Picture formatMemoryStream ms1 = new MemoryStream(FileByte);
Bitmap sBitmap = (Bitmap)(ms1);
Rectangle section = new Rectangle(new Point((), ()), new Size((), ()));
Bitmap CroppedImage = MakeThumbnailImage(sBitmap, , , , , , );

In the above code, I created an ImgSize class myself, the code is as follows:

class ImgSize
{
//{"x":30.003846153846148,"y":16.715384615384625,"height":300.8,"width":300.8,"rotate":0}
public double x { get; set; }
public double y { get; set; }
public double width { get; set; }
public double height { get; set; }
public int rotate { get; set; }
public int ToInt(double doubleValue)
{
return Convert.ToInt32(doubleValue);
}
}

Several methods used in the above code:

File conversion:

/// <summary>
/// Convert Form form file to byte array/// </summary>
/// <param name="File">from Submit File Stream</param>/// &lt;returns&gt;&lt;/returns&gt;
private byte[] SetFileToByteArray(HttpPostedFile File)
{
Stream stream = ;
byte[] AyyayByte = new byte[];
(AyyayByte, 0, );
();
return AyyayByte;
}

Core tailoring method:

/// &lt;summary&gt;
/// Crop the picture and save it/// &lt;/summary&gt;
/// <param name="Image">Picture Information</param>/// <param name="maxWidth">Thumbnail width</param>/// <param name="maxHeight">Thumbnail height</param>/// <param name="cropWidth">Crop Width</param>/// <param name="cropHeight">Crop Height</param>/// <param name="X">X axis</param>/// <param name="Y">Y axis</param>public static Bitmap MakeThumbnailImage(Image originalImage, int maxWidth, int maxHeight, int cropWidth, int cropHeight, int X, int Y)
{
Bitmap b = new Bitmap(cropWidth, cropHeight);
try
{
using (Graphics g = (b))
{
//Clear the canvas and fill it with transparent background color();
//Draw the specified part of the original picture at the specified position and according to the specified size(originalImage, new Rectangle(0, 0, cropWidth, cropHeight), X, Y, cropWidth, cropHeight, );
Image displayImage = new Bitmap(b, maxWidth, maxHeight);
("E:\\", );
Bitmap bit = new Bitmap(b, maxWidth, maxHeight);
return bit;
}
}
catch ( e)
{
throw e;
}
finally
{
();
();
}
}

The final result is to save it to the root directory of E disk

The above is the editor’s introduction to how C# can be tailored and saved. I hope it will be helpful to everyone. If you have any questions, please leave me a message. The editor will reply you in time. Thank you very much for your support for my website!