【问题标题】:Resize JPEG image to fixed width, while keeping aspect ratio as it is将 JPEG 图像大小调整为固定宽度,同时保持纵横比不变
【发布时间】:2012-01-03 02:44:34
【问题描述】:

如何在保持纵横比的同时将 JPEG 图像调整为固定宽度?以简单的方式,同时保持质量。

【问题讨论】:

    标签: c# image-resizing aspect-ratio


    【解决方案1】:

    如果要将宽度减少 25% 到固定值,则必须将高度减少 25%。

    如果要将宽度增加 25% 到固定值,则必须将高度增加 25%。

    这真的很简单。

    【讨论】:

      【解决方案2】:

      如果您搜索它们,我认为有很多这样的示例。这是我常用的一种...

          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));
      

      【讨论】:

        【解决方案3】:

        快速搜索代码项目找到了以下文章。它允许调整接受布尔值以限制新图像以保持原始纵横比的图像大小。我不确定质量如何,因为没有提供屏幕截图。见文章here

        【讨论】:

          【解决方案4】:

          假设有一个 (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);
          

          最后你会得到一个宽x高的新图像,然后,你需要刷新图形并保存 imgnew。

          【讨论】:

            【解决方案5】:

            这将仅在垂直轴上缩放:

                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;
                }
            

            【讨论】:

              猜你喜欢
              • 2019-01-08
              • 1970-01-01
              • 2013-06-26
              • 2012-04-15
              • 2013-04-03
              • 2017-10-28
              相关资源
              最近更新 更多