【问题标题】:Resize Image Keep Aspect Ratio White Line Border调整图像大小保持纵横比白线边框
【发布时间】:2016-07-29 04:27:44
【问题描述】:

我使用以下代码(改编自 Example #2 here)来调整图像大小,保持纵横比。但我在调整大小的图像周围不断出现白色边框。我做错了什么。

 Bitmap ResizekeepAspectRatio(Bitmap imgPhoto, int Width, int Height)
            {
                int sourceWidth = imgPhoto.Width;
                int sourceHeight = imgPhoto.Height;
                int sourceX = 0;
                int sourceY = 0;
                int destX = 0;
                int destY = 0;

                float nPercent = 0;
                float nPercentW = 0;
                float nPercentH = 0;

                nPercentW = ((float)Width / (float)sourceWidth);
                nPercentH = ((float)Height / (float)sourceHeight);
                if (nPercentH < nPercentW)
                {
                    nPercent = nPercentH;
                    destX = System.Convert.ToInt16((Width -
                                  (sourceWidth * nPercent)) / 2);
                }
                else
                {
                    nPercent = nPercentW;
                    destY = System.Convert.ToInt16((Height -
                                  (sourceHeight * nPercent)) / 2);
                }

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

                Bitmap bmPhoto = new Bitmap(Width, Height,
                                  PixelFormat.Format24bppRgb);
                bmPhoto.SetResolution(imgPhoto.HorizontalResolution,
                                 imgPhoto.VerticalResolution);

                Graphics grPhoto = Graphics.FromImage(bmPhoto);
                grPhoto.Clear(Color.White);
                grPhoto.InterpolationMode =
                        InterpolationMode.HighQualityBicubic;

                grPhoto.DrawImage(imgPhoto,
                    new Rectangle(destX, destY, destWidth, destHeight),
                    new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
                    GraphicsUnit.Pixel);

                grPhoto.Dispose();
                return bmPhoto;
            }

更新:

调整大小的图像示例

缩放在角落显示白色边框

【问题讨论】:

  • float 可能导致计算缩短 1 个像素。将float 更改为double
  • @BarmakShemirani 谢谢...实际上下面的答案解决了这个问题..

标签: c# .net bitmap gdi+ system.drawing


【解决方案1】:

尝试换行

Bitmap bmPhoto = new Bitmap(Width, Height,
                              PixelFormat.Format24bppRgb);
To 
 Bitmap bmPhoto = new Bitmap(destWidth, destHeight,
                          PixelFormat.Format24bppRgb);

由于您大部分时间都希望保持纵横比,因此您最终会在图像周围留出额外的空间,因此如果您不需要额外的空间,则使新的图片尺寸适合新的目标尺寸

编辑:

Try to comment out the line grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic

【讨论】:

  • 谢谢......但我不是在谈论额外的空间......我已经使用另一个裁剪功能来裁剪它。我在谈论图像中的白线边框......你有吗看到更新..
  • 尝试注释掉行grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
  • 谢谢...它工作...有什么问题?注释掉这一行会降低输出图像质量吗?
  • 我认为这不会降低图像质量,但是您可以尝试使用 InterpolationMode 和不使用它来比较图像..
  • 您介意更新您的答案,以便我接受。
猜你喜欢
  • 2013-06-26
  • 2012-04-15
  • 2012-05-01
  • 2012-11-15
  • 1970-01-01
相关资源
最近更新 更多