With the mobile phone camera more and more pixels, resulting in a picture like the capacity of more and more large, if uploading more than one picture is not compressed, quality processing is prone to oom memory leak problem.
Recently made a project to upload multiple photos to webservices, but the project deployment will appear flashback phenomenon, and later debugging found that the picture is not compressed, a picture size of 2M, however WebServices can not pick up multiple large pictures, so need to change the configuration file, Here I change to 40M.
<system.web> " 40960 " usefullyqualifiedredirecturl="true "/> </system.web>
Change here to find the upload picture or there is a problem, and then after a step-by-step debugging found that the local image converted to bitmap did not empty, and then kept in memory, resulting in memory leaks. Just empty the converted bitmap.
/// <summary> ///convert a picture into a string stream/// </summary> /// <param name= "File_path" >file name (without file://)</param> /// <returns></returns> Public Static stringImagetostring (stringFile_path) { //image path to upload//string uploadfile = File_path; //Convert to file//System.IO.FileInfo imgfile = new System.IO.FileInfo (uploadfile); ////File converted to bytes //byte[] Imgbyte = new Byte[imgfile.length]; //////Read the file //System.IO.FileStream Imgstream = Imgfile.openread (); //////writes a file to a byte array //imgstream.read (imgbyte, 0, Convert.ToInt32 (imgfile.length)); //////byte array converted to String type //string by = Convert.tobase64string (imgbyte); ////file name after uploading to the server////fileup.updatefile (Imgbyte, Guid.NewGuid () + ". png"); //return imgbyte; Bitmap Bitmap= Bitmapfactory.decodefile (File_path);//convert picture files to bitmap format:/storage/emulated/0/dcim/camera/img_20180425_105725.jpg stringBitstring =bitmaptostring (bitmap); Bitmap=NULL;//Be sure to clear it, or it will cause an oom problemGC. Collect (); returnbitstring; } /// <summary> ///Image Zoom Processing/// </summary> /// <param name= "Bgimage" >Bitmap File</param> /// <param name= "Newwidth" >New picture Width</param> /// <param name= "Newheight" >New picture Height</param> /// <returns></returns> Public StaticBitmap zoomimage (Bitmap bgimage,DoubleNewwidth,Doublenewheight) { //get the width and height of this picture floatwidth =Bgimage. Width; floatHeight =Bgimage. Height; //Create a Matrix object for manipulating picturesMatrix Matrix =NewMatrix (); //calculate the wide and high zoom rate floatScaleWidth = ((float) newwidth)/width; floatScaleHeight = ((float) newheight)/height; //Zoom Picture ActionMatrix. Postscale (ScaleWidth, ScaleHeight); Bitmap Bitmap= Bitmap.createbitmap (Bgimage,0,0, (int) width, (int) Height, matrix,true); returnbitmap; } Static stringbitmaptostring (Bitmap Bitmap) {Bitmap bit= Zoomimage (Bitmap, -, +);//Small Map//Mass Compression//MemoryStream stream = new MemoryStream (); //bit.compress (Bitmap.CompressFormat.Jpeg, (), stream); //byte[] BitmapData = stream. ToArray (); //Bitmap map = Bitmapfactory.decodebytearray (bitmapData, 0, bitmapdata.length); //Btn_imagetwo. Setimagebitmap (map); //Bitmap im = Zoomimage (Bitmap, n, +);//Big PictureMemoryStream Big_stream =NewMemoryStream (); Bit.compress (Bitmap.CompressFormat.Jpeg, the, Big_stream); byte[] Big_bitmapdata =Big_stream. ToArray (); returnconvert.tobase64string (Big_bitmapdata); }
WebServices Accept to save the picture:
PrivateString ImagePath ="/handlerimages/"; /// <summary> ///Upload Image/// </summary> /// <param name= "Content" >Picture character Stream</param> /// <param name= "Pathandname" >Picture name</param> /// <returns></returns>[WebMethod] Public BOOLUpdatefile (stringContentstringpathandname) { //Save Picture Path stringFilePath =Server.MapPath (ImagePath); //determine if a path exists if(!directory.exists (FilePath)) { //Create Pathdirectory.createdirectory (FilePath); } stringSavefilepath =Path.Combine (FilePath, pathandname); byte[] filebytes; Try{filebytes=convert.frombase64string (content); MemoryStream MemoryStream=NewMemoryStream (filebytes);//1. Define and instantiate a memory stream to hold the byte array submitted. FileStream fileUpload =NewFileStream (Savefilepath, FileMode.Create);///2. Define the actual file object and save the uploaded file. Memorystream.writeto (FileUpload);///3. Write the data in the memory stream to a physical fileMemorystream.close (); Fileupload.close (); FileUpload=NULL; MemoryStream=NULL; return true; } Catch { return false; } }
Call WebServices upload Image:
New
Service. Updatefile (Imagetobyte ("/storage/emulated/0/dcim/camera/img_20180425_105725.jpg" " . jpg ");
Xamarin.android compress the pictures and upload them to webservices