How to Cut and Save Pictures in C

  • 2021-11-10 10:30:23
  • OfStack

Recently, I need to upload a picture and cut it according to the specified position. Later, I found a plug-in for cutting pictures on the Internet, but only the front desk has no back end. Then I made various Baidu and finally finished it. I hereby write a blog to commemorate it.

I won't say at the front desk, use cropper plug-in, and go to Baidu to find it if you are interested. This site has this plug-in.

Here's the code:


HttpPostedFile file = context.Request.Files["avatar_file"];
string datasize = context.Request.Params["avatar_data"];
//{"x":30.003846153846148,"y":16.715384615384625,"height":300.8,"width":300.8,"rotate":0}  Parameters after clipping 
JavaScriptSerializer jss = new JavaScriptSerializer();
ImgSize imagesize = jss.Deserialize<ImgSize>(datasize);
byte[] FileByte = SetFileToByteArray(file);// Picture array 
string strtExtension = System.IO.Path.GetExtension(file.FileName);// Picture format 
MemoryStream ms1 = new MemoryStream(FileByte);
Bitmap sBitmap = (Bitmap)Image.FromStream(ms1);
Rectangle section = new Rectangle(new Point(imagesize.ToInt(imagesize.x), imagesize.ToInt(imagesize.y)), new Size(imagesize.ToInt(imagesize.width), imagesize.ToInt(imagesize.height)));
Bitmap CroppedImage = MakeThumbnailImage(sBitmap, section.Width, section.Height, section.Width, section.Height, section.X, section.Y);

In the above code, I created an ImgSize class myself, and 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>
///  Will From Form file File is converted to byte Array 
/// </summary>
/// <param name="File">from Submit file stream </param>
/// <returns></returns>
private byte[] SetFileToByteArray(HttpPostedFile File)
{
Stream stream = File.InputStream;
byte[] AyyayByte = new byte[File.ContentLength];
stream.Read(AyyayByte, 0, File.ContentLength);
stream.Close();
return AyyayByte;
}

Core clipping method:


/// <summary>
///  Crop the picture and save it 
/// </summary>
/// <param name="Image"> Picture information </param>
/// <param name="maxWidth"> Thumbnail width </param>
/// <param name="maxHeight"> Thumbnail height </param>
/// <param name="cropWidth"> Cropping width </param>
/// <param name="cropHeight"> Cutting height </param>
/// <param name="X">X Shaft </param>
/// <param name="Y">Y Shaft </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 = Graphics.FromImage(b))
{
// Empty the canvas and fill it with a transparent background color 
g.Clear(Color.Transparent);
// Draws the specified part of the original picture at the specified position and at the specified size 
g.DrawImage(originalImage, new Rectangle(0, 0, cropWidth, cropHeight), X, Y, cropWidth, cropHeight, GraphicsUnit.Pixel);
Image displayImage = new Bitmap(b, maxWidth, maxHeight);
displayImage.Save("E:\\cutimg.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
Bitmap bit = new Bitmap(b, maxWidth, maxHeight);
return bit;
}
}
catch (System.Exception e)
{
throw e;
}
finally
{
originalImage.Dispose();
b.Dispose();
}
}

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


Related articles: