2012-11-22 60 views
1

需要幫助。我想調整某一圖片,但是當我運行這段代碼它給我的錯誤{「GDI +中發生一般性錯誤。」}調整圖片C#

private void btnPic_Click(object sender, EventArgs e) 
     { 
      OpenFileDialog open = new OpenFileDialog(); 
      open.Filter = "Images|*.bmp;*.jpg;*.gif|All files|*.*"; 

      if (open.ShowDialog(this) == DialogResult.OK) 
      { 

       var image = Image.FromFile(open.FileName); 
       var newImage = ScaleImage(image, 300, 400); 
       newImage.Save(@open.FileName, ImageFormat.Png); 

      } 
} 


public static Image ScaleImage(Image image, int maxWidth, int maxHeight) 
     { 
      var ratioX = (double)maxWidth/image.Width; 
      var ratioY = (double)maxHeight/image.Height; 
      var ratio = Math.Min(ratioX, ratioY); 

      var newWidth = (int)(image.Width * ratio); 
      var newHeight = (int)(image.Height * ratio); 

      var newImage = new Bitmap(newWidth, newHeight); 
      Graphics.FromImage(newImage).DrawImage(image, 0, 0, newWidth, newHeight); 
      return newImage; 
     } 
+0

在你得到錯誤代碼到底是哪行? –

+0

什麼是@ open.FileName? –

+0

如果將圖像保存爲新文件名,它會工作嗎? – CodeCaster

回答

1

你可以嘗試使用這個版本來代替。

private void btnPic_Click(object sender, EventArgs e) 
{ 
    OpenFileDialog open = new OpenFileDialog(); 
    open.Filter = "Images|*.bmp;*.jpg;*.gif|All files|*.*"; 

    if (open.ShowDialog(this) == DialogResult.OK) 
    { 

     var image = Image.FromFile(open.FileName); 
     var newImage = ResizeCenterCropped(image, 300, 400); 
     newImage.Save(@open.FileName, ImageFormat.Png); 

    } 
} 

public static Image ResizeCenterCropped(Image image, int width, int height) 
{ 
    var rect = CreateCroppedRectangle(image, width, height); 
    rect.X = (image.Width/2) - (rect.Width/2); 
    rect.Y = (image.Height/2) - (rect.Height/2); 
    return Resize(image, new Rectangle(0, 0, width, height), rect); 
} 

public static Image Resize(Image image, Rectangle destRectange, Rectangle sourceRectangle) 
{ 
    var rezisedImage = new Bitmap(destRectange.Width, destRectange.Height) 
    using (var g = Graphics.FromImage(rezisedImage)) 
    { 
     g.SmoothingMode = SmoothingMode.AntiAlias; 
     g.InterpolationMode = InterpolationMode.HighQualityBicubic; 
     g.PixelOffsetMode = PixelOffsetMode.HighQuality; 
     g.DrawImage(image, destRectange, sourceRectangle, GraphicsUnit.Pixel); 
     return rezisedImage; 
    } 
} 

public static Rectangle CreateCroppedRectangle(Image image, int width, int height) 
{ 
    var size = new Size(width, height); 
    var size2 = new Size(image.Width, image.Height); 

    //The maximum scale width we could use 
    float maxWidthScale = (float)size2.Width/(float)size.Width; 

    //The maximum scale height we could use 
    float maxHeightScale = (float)size2.Height/(float)size.Height; 

    //Use the smaller of the 2 scales for the scaling 
    float scale = Math.Min(maxHeightScale, maxWidthScale); 


    size.Width = (int)(size.Width * scale); 
    size.Height = (int)(size.Height * scale); 

    return new Rectangle(new Point(), size); 
} 
+0

嗨...謝謝 我有不同的scenerio來實現調整大小,但實現了這個代碼...非常感謝... 我可以用它來調整bmp大小嗎? –

1

當你保存,你流仍然是開放的open.FileName,你不能在其他的文件保存具有相同名稱。您應該執行image.Dispose()來釋放句柄,以便保存可以覆蓋,或者您應該更改文件名。

private void button1_Click(object sender, EventArgs e) 
     { 
      OpenFileDialog open = new OpenFileDialog(); 
      open.Filter = "Images|*.bmp;*.jpg;*.gif|All files|*.*"; 

      if (open.ShowDialog(this) == DialogResult.OK) 
      { 

       var image = Image.FromFile(open.FileName); 
       var newImage = ScaleImage(image, 300, 400); 
       image.Dispose();//Add this to your code 
       newImage.Save(open.FileName, ImageFormat.Png); 
      } 

或更改文件名

private void button1_Click(object sender, EventArgs e) 
     { 
      OpenFileDialog open = new OpenFileDialog(); 
      open.Filter = "Images|*.bmp;*.jpg;*.gif|All files|*.*"; 

      if (open.ShowDialog(this) == DialogResult.OK) 
      { 

       var image = Image.FromFile(open.FileName); 
       var newImage = ScaleImage(image, 300, 400); 
       string path = "1.png"; 
       newImage.Save(open.FileName, ImageFormat.Png); 
      }