调整大小后白色图像获得灰色边框 [英] After resizing white image gets gray border

查看:148
本文介绍了调整大小后白色图像获得灰色边框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在搜索谷歌的某种解决方案,我发现了一个,我试图在我的代码中实现它,但它不起作用。问题是,在调整白色图像大小后,它们会变成灰色边框。

i was searching google for some kind solution and i found one, i tried to implement it in my code but it doesn't work. The problem is that after resizing white images they gets gray border.

这是我发现的soloution的链接:

它说:
出现此问题是因为您正在将图像数据插值为
新尺寸,但沿边缘没有要插值的像素和默认情况下.NET
对这些边使用黑色像素。要解决此问题,您需要在DrawImage调用中使用
ImageAttributes类....

It says: This problem is occuring because you are interpolating your image data to a new size, but along the edges there are no pixels to interpolate and .NET uses black pixels for these edges by default. To fix this you need to use an ImageAttributes class in your DrawImage call....

https:/ /groups.google.com/group/microsoft.public.dotnet.framework.drawing/browse_thread/thread/d834851b49274fd9/81a4fd43694457ac?hl=en&lnk=st&q=DrawImage+resized+border#81a4fd43694457ac

代码1:这是我的代码实现ImageAttributes:

Private Shared Function ResizeImageFile(ByVal imageFile As Byte(), ByVal targetSize As Integer) As Byte()

    Using oldImage As System.Drawing.Image = System.Drawing.Image.FromStream(New MemoryStream(imageFile))

        Dim newSize As Size = CalculateDimensions(oldImage.Size, targetSize)

        Using newImage As New Bitmap(newSize.Width, newSize.Height, PixelFormat.Format32bppRgb)

            Using canvas As Graphics = Graphics.FromImage(newImage)

                Using ia As New ImageAttributes

                    ia.SetWrapMode(Drawing2D.WrapMode.TileFlipXY)
                    canvas.SmoothingMode = SmoothingMode.AntiAlias
                    canvas.InterpolationMode = InterpolationMode.HighQualityBicubic
                    canvas.PixelOffsetMode = PixelOffsetMode.HighQuality
                    canvas.DrawImage(oldImage, New Rectangle(New Point(0, 0), newSize), 0, 0, newImage.Width, newImage.Height, GraphicsUnit.Pixel, ia)

                    Dim m As New MemoryStream()

                    newImage.Save(m, ImageFormat.Png)
                    Return m.GetBuffer()

                End Using

            End Using

        End Using

    End Using

End Function

Private Shared Function CalculateDimensions(ByVal oldSize As Size, ByVal targetSize As Integer) As Size

    Dim newSize As New Size()

    If oldSize.Height > oldSize.Width Then

        newSize.Width = CInt((oldSize.Width * (CSng(targetSize) / CSng(oldSize.Height))))
        newSize.Height = targetSize

    Else

        newSize.Width = targetSize
        newSize.Height = CInt((oldSize.Height * (CSng(targetSize) / CSng(oldSize.Width))))

    End If

    Return newSize

End Function

代码2:在白色图像上保留灰色边框的代码

这是后面的图像调整大小:

新图像尺寸宽度= 400px

new image size in width = 400px

Private Shared Function ResizeImageFile(ByVal imageFile As Byte(), ByVal targetSize As Integer) As Byte()

    Using oldImage As System.Drawing.Image = System.Drawing.Image.FromStream(New MemoryStream(imageFile))

        Dim newSize As Size = CalculateDimensions(oldImage.Size, targetSize)

        Using newImage As New Bitmap(newSize.Width, newSize.Height, PixelFormat.Format32bppRgb)

            Using canvas As Graphics = Graphics.FromImage(newImage)

                    canvas.SmoothingMode = SmoothingMode.AntiAlias
                    canvas.InterpolationMode = InterpolationMode.HighQualityBicubic
                    canvas.PixelOffsetMode = PixelOffsetMode.HighQuality
                    canvas.DrawImage(oldImage, New Rectangle(New Point(0, 0), newSize))

                    Dim m As New MemoryStream()

                    newImage.Save(m, ImageFormat.Png)
                    Return m.GetBuffer()

            End Using

        End Using

    End Using

End Function

Private Shared Function CalculateDimensions(ByVal oldSize As Size, ByVal targetSize As Integer) As Size

    Dim newSize As New Size()

    If oldSize.Height > oldSize.Width Then

        newSize.Width = CInt((oldSize.Width * (CSng(targetSize) / CSng(oldSize.Height))))
        newSize.Height = targetSize

    Else

        newSize.Width = targetSize
        newSize.Height = CInt((oldSize.Height * (CSng(targetSize) / CSng(oldSize.Width))))

    End If

    Return newSize

End Function

更新30.07.2011。:

CODE 1解决了白色图像上灰色边框的问题,但是有新问题。问题在于这行代码:

CODE 1 solved the problem with the gray borders on white images, but there is new problem. The problem is in this line of code:

canvas.DrawImage(oldImage,New Rectangle(New Point(0,0),newSize),0,0,newImage。 Width,newImage.Height,GraphicsUnit.Pixel,ia)

canvas.DrawImage(oldImage, New Rectangle(New Point(0, 0), newSize), 0, 0, newImage.Width, newImage.Height, GraphicsUnit.Pixel, ia)

使用此代码,我得到的输出图像具有所需的宽度和高度,没有灰色边框但是oldImage没有缩放。

With this code I get output image with desired width and height and without gray borders but the oldImage isn't scaled.

例如:

如果我想上传,调整大小并保存有形的图像,例如640x480px,targetSize是400px。作为输出,我得到一个宽度为400px,高度:300px的图像,没有灰色边框,但oldImage没有调整大小/缩放到400px。在这种情况下,oldImage以原始分辨率绘制。 如何正确缩放oldImage?有人能指出我正确的解决方案或修改代码吗?

If I want to upload, resize and save the image that is orginaly for instance 640x480px and that the targetSize is 400px. As output I get an image that is width: 400px, height: 300px, without gray borders, but oldImage isn't resized/scaled to 400px. Insted of this, oldImage is drawn with the original resolution. How to scale oldImage to be drawn correctly? Can someone point me to the right solution or modify the code?

Thanx给大家,但我找到了解决所有问题的方法。

由于以下代码行,CODE 1 无法正常运行

The CODE 1 didn't work correctly because of the following line of code:

canvas.DrawImage(oldImage,New Rectangle(New Point(0,0),newSize),0,0,newImage.Width,newImage.Height,GraphicsUnit.Pixel,ia )

canvas.DrawImage(oldImage, New Rectangle(New Point(0, 0), newSize), 0, 0, newImage.Width, newImage.Height, GraphicsUnit.Pixel, ia)

解决方案:

canvas.DrawImage(oldImage,New Rectangle(New Point(0,0),newSize),0,0, oldImage.Width oldImage.Height ,GraphicsUnit.Pixel,ia)

canvas.DrawImage(oldImage, New Rectangle(New Point(0, 0), newSize), 0, 0, oldImage.Width, oldImage.Height, GraphicsUnit.Pixel, ia)

这里是完整的工作代码(没有灰色/黑色边框的调整大小的图片):

Private Shared Function ResizeImageFile(ByVal imageFile As Byte(), ByVal targetSize As Integer) As Byte()

    Using oldImage As System.Drawing.Image = System.Drawing.Image.FromStream(New MemoryStream(imageFile))

        Dim newSize As Size = CalculateDimensions(oldImage.Size, targetSize)

        Using newImage As New Bitmap(newSize.Width, newSize.Height, PixelFormat.Format32bppRgb)

            Using canvas As Graphics = Graphics.FromImage(newImage)

                Using ia As New ImageAttributes

                    ia.SetWrapMode(Drawing2D.WrapMode.TileFlipXY)
                    canvas.SmoothingMode = SmoothingMode.AntiAlias
                    canvas.InterpolationMode = InterpolationMode.HighQualityBicubic
                    canvas.PixelOffsetMode = PixelOffsetMode.HighQuality
                    canvas.DrawImage(oldImage, New Rectangle(New Point(0, 0), newSize), 0, 0, oldImage.Width, oldImage.Height, GraphicsUnit.Pixel, ia)

                    Dim m As New MemoryStream()

                    newImage.Save(m, ImageFormat.Png)
                    Return m.GetBuffer()

                End Using

            End Using

        End Using

    End Using

End Function

Private Shared Function CalculateDimensions(ByVal oldSize As Size, ByVal targetSize As Integer) As Size

    Dim newSize As New Size()

    If oldSize.Height > oldSize.Width Then

        newSize.Width = CInt((oldSize.Width * (CSng(targetSize) / CSng(oldSize.Height))))
        newSize.Height = targetSize

    Else

        newSize.Width = targetSize
        newSize.Height = CInt((oldSize.Height * (CSng(targetSize) / CSng(oldSize.Width))))

    End If

    Return newSize

End Function


推荐答案

这是我所拥有的类中的一个函数,你必须替换一些类属性(ThumbNailSize.Width,ThumbNailSize.Height): / p>

Here is a function out of a class I have, you will have to replace some of the class Properties(ThumbNailSize.Width, ThumbNailSize.Height):

public void ResizeImage(HttpPostedFile fil, string sPhysicalPath, 
                              string sOrgFileName,string sThumbNailFileName,
                              ImageFormat oFormat, int rez)
{

    try
    {

        System.Drawing.Image oImg = System.Drawing.Image.FromStream(fil.InputStream);

        decimal pixtosubstract = 0;
        decimal percentage;

        //default
        Size ThumbNailSizeToUse = new Size();
        if (ThumbNailSize.Width < oImg.Size.Width || ThumbNailSize.Height < oImg.Size.Height)
        {
            if (oImg.Size.Width > oImg.Size.Height)
            {
                percentage = (((decimal)oImg.Size.Width - (decimal)ThumbNailSize.Width) / (decimal)oImg.Size.Width);
                pixtosubstract = percentage * oImg.Size.Height;
                ThumbNailSizeToUse.Width = ThumbNailSize.Width;
                ThumbNailSizeToUse.Height = oImg.Size.Height - (int)pixtosubstract;
            }
            else
            {
                percentage = (((decimal)oImg.Size.Height - (decimal)ThumbNailSize.Height) / (decimal)oImg.Size.Height);
                pixtosubstract = percentage * (decimal)oImg.Size.Width;
                ThumbNailSizeToUse.Height = ThumbNailSize.Height;
                ThumbNailSizeToUse.Width = oImg.Size.Width - (int)pixtosubstract;
            }

        }
        else
        {
            ThumbNailSizeToUse.Width = oImg.Size.Width;
            ThumbNailSizeToUse.Height = oImg.Size.Height;
        }

        Bitmap bmp = new Bitmap(ThumbNailSizeToUse.Width, ThumbNailSizeToUse.Height);
        bmp.SetResolution(rez, rez);
        System.Drawing.Image oThumbNail = bmp;

        bmp = null;

        Graphics oGraphic = Graphics.FromImage(oThumbNail);

        oGraphic.CompositingQuality = CompositingQuality.HighQuality;

        oGraphic.SmoothingMode = SmoothingMode.HighQuality;

        oGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic;

        Rectangle oRectangle = new Rectangle(0, 0, ThumbNailSizeToUse.Width, ThumbNailSizeToUse.Height);

        oGraphic.DrawImage(oImg, oRectangle);

        oThumbNail.Save(sPhysicalPath  + sThumbNailFileName, oFormat);

        oImg.Dispose();

    }
    catch (Exception ex)
    {
        Response.Write(ex.Message);
    }

}

这篇关于调整大小后白色图像获得灰色边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆