2016-05-04 4 views
0

Das Bild wird beim Beschneiden in verschiedene Größen verzerrt.
Wie mache ich das ohne die Bildqualität zu beeinflussen? Mein aktuelles Ergebnis ist ein verzerrtes unscharfes Bild. Bitte helfen Sie.Bild zuschneiden, ohne die Pixel in c zu verzerren #

Hier ist mein Code:

var common = new Common(); 
string filesPath = HttpContext.Current.Server.MapPath(Const.directoryPath); 
string imageUrl1 = UploadImageToAzure(1123, "\\Configurator\\_trunk\\Content\\TempImages\\eddaec5aa33e4b1593b304674a842874.jpeg, "eddaec5aa33e4b1593b304674a842874_260x190.jpeg", cloudstorage, containerName); 
string cropimage260x190 = CropImagewithName(inputStream/*System.Io.Stream*/, 260, 190, cropedImageName); 


public string CropImagewithName(Stream stream, int width, int height, string name) 
     { 
      int bmpW = 0; 
      int bmpH = 0; 
      string filePath = string.Empty; 
      string imgName = string.Empty; 
      try 
      { 
       { 
        bmpW = width; 
        bmpH = height; 
        int newWidth = bmpW; 
        int newHeight = bmpH; 

        imgName = name; 
        imgName = imgName.Replace("-", ""); 
        filePath = Const.directoryPath + imgName; 
        this.upBmp = new Bitmap(stream); 

        this.newBmp = new Bitmap(newWidth, newHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb); 
        this.newBmp.SetResolution(300, 300); 

        int upWidth = this.upBmp.Width; 
        int upHeight = this.upBmp.Height; 
        int newX = 0; 
        int newY = 0; 
        decimal reDuce; 
        if (upWidth > upHeight) 
        { 
         reDuce = Convert.ToDecimal(newWidth)/Convert.ToDecimal(upWidth); 
         newHeight = Convert.ToInt32((Convert.ToDecimal(upHeight) * reDuce)); 
         newY = (bmpH - newHeight)/2; 

         newX = 0; 
        } 

        else if (upWidth < upHeight) 
        { 
         reDuce = Convert.ToDecimal(newHeight)/Convert.ToDecimal(upHeight); 
         newWidth = Convert.ToInt32((Convert.ToDecimal(upWidth) * reDuce)); 
         newX = (bmpW - newWidth)/2; 
         newY = 0; 
        } 
        else if (upWidth == upHeight) // 
        { 
         reDuce = Convert.ToDecimal(newHeight)/Convert.ToDecimal(upHeight); 
         newWidth = Convert.ToInt32((Convert.ToDecimal(upWidth) * reDuce)); 
         newX = (bmpW - newWidth)/2; 
         newY = (bmpH - newHeight)/2; 
        } 

        newGraphic = Graphics.FromImage(newBmp); 

        this.newGraphic.Clear(Color.White); 
        this.newGraphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; 
        this.newGraphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; 
        newGraphic.DrawImage(upBmp, newX, newY, newWidth, newHeight); 
        newBmp.Save(HttpContext.Current.Server.MapPath(filePath), System.Drawing.Imaging.ImageFormat.Jpeg); 
       } 
      } 
      catch (Exception) 
      { 
      } 
      finally 
      { 
       this.upBmp.Dispose(); 
       this.newBmp.Dispose(); 
       this.newGraphic.Dispose(); 
      } 
      return imgName; 
     } 
+0

Werfen Sie einen Blick auf das Beispiel auf dieser MSDN-Seite https://msdn.microsoft.com/en-us/library/ytz20d80(v=vs.110).aspx Es zeigt, wie man ein JPEG speichert und eine Qualitätsstufe bis zu 100 verwendet, die das Beste ist. – pmcilreavy

Antwort

1

Sie erleben JPEG Kompressionsartefakte, nicht geometrische Verzerrung. Sie müssen die JPEG-Komprimierungsqualität festlegen, bevor Sie Ihr Bild speichern. Hier ist, wie Sie Ihr Bild mit höchster Qualität speichern kann (suchen Sie nach 100L im Code unten):

ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders(); 
foreach (ImageCodecInfo codec in codecs) 
{ 
    if (codec.FormatID == ImageFormat.Jpeg.Guid) 
    { 
     var myEncoder = System.Drawing.Imaging.Encoder.Quality; 
     var myEncoderParameter = new EncoderParameter(myEncoder, 100L); 
     var myEncoderParameters = new EncoderParameters(1) { Param = { [0] = myEncoderParameter } }; 
     newBmp.Save(@"C:\qqq\111.jpeg", codec, myEncoderParameters); 

     break; 
    } 
} 

Hier ist der MSDN-Artikel für sie: https://msdn.microsoft.com/en-us/library/bb882583(v=vs.110).aspx

+0

Vielen Dank für Ihre Antwort, ich habe hier eine Lösung gefunden http://gunnarpeipman.com/2009/04/resizing-images-without-loss-of-quality/ –