【问题标题】:Image after resize has white border调整大小后的图像有白色边框
【发布时间】:2013-12-17 05:56:54
【问题描述】:

我正在尝试制作调整图像大小的功能。

public static Bitmap FixedSize(Bitmap imgPhoto, int Width, int Height, InterpolationMode im)
{
if ((Width == 0) && (Height == 0))
    return imgPhoto;

if ((Width < 0) || (Height < 0))
    return imgPhoto;

int destWidth = Width;
int destHeight = Height;

int srcWidth = imgPhoto.Size.Width;
int srcHeight = imgPhoto.Size.Height;

if (Width == 0)
    destWidth = (int)(((float)Height / (float)srcHeight) * (float)srcWidth);
if (Height == 0)
    destHeight = (int)(((float)Width / (float)srcWidth) * (float)srcHeight);

Bitmap bmPhoto = new Bitmap(destWidth, destHeight,
    PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution,
    imgPhoto.VerticalResolution);

Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.Clear(Color.White);
grPhoto.InterpolationMode = im;
grPhoto.DrawImage(imgPhoto,     
    new Rectangle(new Point(0, 0), new Size(destWidth, destHeight)),
    new Rectangle(0, 0, srcWidth, srcHeight),
    GraphicsUnit.Pixel);            
    grPhoto.Dispose();
return new Bitmap(bmPhoto);
}

当我调试代码时,所有数字似乎都正常,但是当我保存图像时,它的左侧和顶部边框上有一条白线。知道应该有什么问题吗?我尝试搜索并使用了完全相同的代码,该代码应该可以工作,但该行仍然存在。
谢谢。

【问题讨论】:

    标签: c# image resize border line


    【解决方案1】:
    public static Bitmap ResizeImage(Bitmap image, int percent)
        {
            try
            {
                int maxWidth = (int)(image.Width * (percent * .01));
                int maxHeight = (int)(image.Height * (percent * .01));
                Size size =   GetSize(image, maxWidth, maxHeight);
                Bitmap newImage = new Bitmap(size.Width, size.Height, PixelFormat.Format24bppRgb);
                SetGraphics(image, size, newImage);
                return newImage;
            }
            finally { }
        }
    
       public static void SetGraphics(Bitmap image, Size size, Bitmap newImage)
        {
            using (Graphics graphics = Graphics.FromImage(newImage))
            {
                graphics.CompositingQuality = CompositingQuality.HighQuality;
                graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                graphics.SmoothingMode = SmoothingMode.HighQuality;
                graphics.DrawImage(image, 0, 0, size.Width, size.Height);
            }
        }
    

    标记一下,如果对你有帮助。

    【讨论】:

    • 虽然我不明白为什么,但它有效!谢谢:-)
    • GetSize 方法是什么? ^^
    猜你喜欢
    • 2011-10-12
    • 2020-07-16
    • 2010-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-01
    • 2011-07-24
    • 1970-01-01
    相关资源
    最近更新 更多