2011-11-21 111 views

回答

1

如果將寬度減少25%爲固定值,則必須將高度減少25%。

如果將寬度增加25%爲固定值,則必須將高度增加25%。

這真的很簡單。

0

我認爲如果你搜索它們,這裏有很多這樣的例子。這裏是一個我經常使用...調用代碼的

public static Stream ResizeGdi(Stream stream, System.Drawing.Size size) 
    { 
     Image image = Image.FromStream(stream); 

     int width = image.Width; 
     int height = image.Height; 

     int sourceX = 0, sourceY = 0, destX = 0, destY = 0; 

     float percent = 0, percentWidth = 0, percentHeight = 0; 
     percentWidth = ((float)size.Width/(float)width); 
     percentHeight = ((float)size.Height/(float)height); 

     int destW = 0; 
     int destH = 0; 

     if (percentHeight < percentWidth) 
     { 
      percent = percentHeight; 
     } 
     else 
     { 
      percent = percentWidth; 
     } 

     destW = (int)(width * percent); 
     destH = (int)(height * percent); 

     MemoryStream mStream = new MemoryStream(); 

     if (destW == 0 
      && destH == 0) 
     { 
      image.Save(mStream, System.Drawing.Imaging.ImageFormat.Jpeg); 
      return mStream; 
     } 

     using (Bitmap bitmap = new Bitmap(destW, destH, System.Drawing.Imaging.PixelFormat.Format48bppRgb)) 
     { 
      using (System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bitmap)) 
      { 
       //graphics.Clear(Color.Red); 
       graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; 
       graphics.DrawImage(image, 
        new Rectangle(destX, destY, destW, destH), 
        new Rectangle(sourceX, sourceY, width, height), 
        GraphicsUnit.Pixel); 
      } 

      bitmap.Save(mStream, System.Drawing.Imaging.ImageFormat.Jpeg); 
     } 

     mStream.Position = 0; 
     return mStream as Stream; 
    } 

例...

Stream stream = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.None); 

resizedStream = ImageUtility.ResizeGdi(stream, new System.Drawing.Size(resizeWidth, resizeHeight)); 
+0

他希望保持寬度不變,這是不行的 – rboarman

0

代碼項目上快速搜索發現下面的文章。它允許調整接受布爾值的圖像以限制新圖像以保持原始縱橫比。我不確定質量如何,因爲沒有提供截圖。見文章here

1

假設有一個(double width)變量:

Image imgOriginal = Bitmap.FromFile(path); 
double height = (imgOriginal.Height * width)/imgOriginal.Width; 
Image imgnew = new Bitmap((int)width, (int)height, PixelFormat.Format32bppArgb); 
Graphics g = Graphics.FromImage(imgnew); 
g.DrawImage(imgOriginal, new Point[]{new Point(0,0), new Point(width, 0), new Point(0, height)}, new Rectangle(0,0,imgOriginal.Width, imgOriginal.Height), GraphicsUnit.Pixel); 

在端you'll具有寬度x高度新的圖像,那麼,您需要同時刷新圖形e保存imgnew。

17

這將在規模僅垂直軸:

public static Image ResizeImageFixedWidth(Image imgToResize, int width) 
    { 
     int sourceWidth = imgToResize.Width; 
     int sourceHeight = imgToResize.Height; 

     float nPercent = ((float)width/(float)sourceWidth); 

     int destWidth = (int)(sourceWidth * nPercent); 
     int destHeight = (int)(sourceHeight * nPercent); 

     Bitmap b = new Bitmap(destWidth, destHeight); 
     Graphics g = Graphics.FromImage((Image)b); 
     g.InterpolationMode = InterpolationMode.HighQualityBicubic; 

     g.DrawImage(imgToResize, 0, 0, destWidth, destHeight); 
     g.Dispose(); 

     return (Image)b; 
    } 
相關問題