2017-02-10 129 views
1

我想從c#的中心上傳時間中裁剪圖像。我已經試過這個代碼但不返回新的圖像。如何在c#中上傳圖像後自動從中心裁剪圖像?

[AllowAnonymous] 
    [HttpPost] 
    public string Upload(string dataImage, string Pusername, double dataX = 0, double? dataY = 0, double? dataHeight = 0, double? dataWidth = 0, double? dataRotate = 0) 
    { 
     if (!String.IsNullOrEmpty(dataImage)) 
     { 
      try 
      { 
       string lenght = dataImage.Substring(0, dataImage.LastIndexOf(",")); 
       string test = dataImage.Remove(0, lenght.Length + 1); 
       byte[] imageBytes = Convert.FromBase64String(test); 
       MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length); 
       ms.Write(imageBytes, 0, imageBytes.Length); 

       Bitmap image1 = new Bitmap(ms); 
       Rectangle rect = new Rectangle(Convert.ToInt32(dataX), Convert.ToInt32(dataY), Convert.ToInt32(dataWidth), Convert.ToInt32(dataHeight)); 

       Bitmap cropped = null; 

       if (dataX != 0 || dataY != 0 || dataHeight != 0 || dataWidth != 0) 
       { 
        cropped = RotateImageByAngle(image1, rect, (float)dataRotate); 
       } 
       else 
       {       
        cropped = (Bitmap)ResizeImage(image1, new Size(image1.Width, image1.Height)); 
       } 

       string fileName = string.Empty; 
       string Destintionpath = string.Empty; 

       string FileExtensionType = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders().FirstOrDefault(x => x.FormatID == image1.RawFormat.Guid).FilenameExtension; 

       string FinalFileExtension; 

       if (FileExtensionType.ToLower().Contains("jpg") || FileExtensionType.ToLower().Contains("jpeg") || FileExtensionType.ToLower().Contains("jfif")) 
        FinalFileExtension = "jpeg"; 
       else if (FileExtensionType.ToLower().Contains("png")) 
        FinalFileExtension = "png"; 
       else if (FileExtensionType.ToLower().Contains("bmp")) 
        FinalFileExtension = "bmp"; 
       else 
        FinalFileExtension = "gif"; 

       fileName = System.IO.Path.GetFileName(Pusername+ "_" + DateTime.UtcNow.ToString("mmddyyyy-Hmmss", CultureInfo.InvariantCulture) + "." + FinalFileExtension); 
       Destintionpath = System.IO.Path.Combine(Server.MapPath("~/" + ConfigurationManager.AppSettings["ImagesFolder"].ToString() + "/"), fileName); 
       string databasepath = Request.Url.GetLeftPart(UriPartial.Authority) + "/" + ConfigurationManager.AppSettings["ImagesFolder"].ToString() + "/" + fileName; 

       string FileExtension = fileName.Substring(fileName.LastIndexOf('.') + 1, fileName.Length - (fileName.LastIndexOf('.') + 1)); 


       if (FileExtension.ToLower() == "jpg" || FileExtension.ToLower() == "jpeg") 
        ImageCompressionJpeg.Compress(cropped, Destintionpath); 
       else if (FileExtension.ToLower() == "png") 
        ImageCompressionJpeg.RemoveTransparency(cropped, Destintionpath); 
       else 
        cropped.Save(Destintionpath); 

       RegisterModel objregister = new RegisterModel(); 
       objregister.UserName = Profile.UserName; 
       objregister.CloudImagePath = databasepath; 
       string result = u.UserThumbnail(objregister).Result; 

       return databasepath; 
      } 
      catch 
      { 
       return "error"; 
      } 
     } 
     else 
     { 
      return "error"; 
     } 
    } 

下面是我的ResizeImage方法下面這個方法用於圖像裁剪,但我不返回新的裁剪圖像。

public static Image ResizeImage(Image imgToResize, Size destinationSize) 
    { 
     var originalWidth = imgToResize.Width; 
     var originalHeight = imgToResize.Height; 

     var hRatio = (float)originalHeight/destinationSize.Height; 
     var wRatio = (float)originalWidth/destinationSize.Width; 

     var ratio = Math.Min(hRatio, wRatio); 

     var hScale = Convert.ToInt32(destinationSize.Height * ratio); 
     var wScale = Convert.ToInt32(destinationSize.Width * ratio); 

     var startX = (originalWidth - wScale)/2; 
     var startY = (originalHeight - hScale)/2; 

     var sourceRectangle = new Rectangle(startX, startY, wScale, hScale); 

     var bitmap = new Bitmap(destinationSize.Width, destinationSize.Height); 

     var destinationRectangle = new Rectangle(0, 0, bitmap.Width, bitmap.Height); 

     using (var g = Graphics.FromImage(bitmap)) 
     { 
      g.InterpolationMode = InterpolationMode.HighQualityBicubic; 
      g.DrawImage(imgToResize, destinationRectangle, sourceRectangle, GraphicsUnit.Pixel); 
     } 

     return bitmap; 

    } 

回答

0

可以使用,

using (var g = Graphics.FromImage(bitmap)) 
     { 
      g.TranslateTransform((float)bitmap.Width/2, (float)bitmap.Height/2); 
      g.InterpolationMode = InterpolationMode.HighQualityBicubic; 
      g.DrawImage(imgToResize, destinationRectangle, sourceRectangle, GraphicsUnit.Pixel); 
     } 

希望這有助於!