Recently need to upload a picture and cut to the specified location, and then found a clip on the internet, the plugin, but only the front desk has no backend, and then I all kinds of Baidu, and finally completed, hereby write a little souvenir blog.
I do not say the front desk, with the Cropper Plug-ins, interested in their own Baidu to find it. The cloud-dwelling community has this plugin.
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} after trimming parameters
JavaScriptSerializer JSS = new JavaScriptSerializer ();
Imgsize imagesize = JSS. Deserialize (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 (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);
The code above uses myself to create a imgsize class with the following code:
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 preceding code:
File conversion:
<summary>
///converts from form file 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 pictures and save///</summary>///<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 = graphics.fromimage (b)) {//clear the canvas and fill the g.clear (color.transparent) with a transparent background color; Draw 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; The catch (System.Exception e) {throw e;} finally {originalimage.dispose (); B.dispose ();}}
The final result is to save it under the E-packing directory
The above is a small set of C # to introduce how to achieve the image of the cut and save, I hope to be helpful to everyone, if you have any questions welcome to my message, small series will promptly reply to everyone, here also thank you for the cloud Habitat Community website support!