Asp.net 调整大小后,白色图像得到灰色边框

Asp.net 调整大小后,白色图像得到灰色边框,asp.net,vb.net,image,resize,Asp.net,Vb.net,Image,Resize,我在谷歌搜索某种解决方案,我找到了一个,我试图在我的代码中实现它,但它不起作用。问题是,在调整白色图像的大小后,它们会得到灰色边框 这是我找到的soloution的链接: Private Shared Function ResizeImageFile(ByVal imageFile As Byte(), ByVal targetSize As Integer) As Byte() Using oldImage As System.Drawing.Image = System.Drawi

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

这是我找到的soloution的链接:

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
上面写着: 发生此问题的原因是将图像数据插值到 新的大小,但沿边缘没有要插值的像素和.NET 默认情况下,这些边使用黑色像素。要解决此问题,您需要使用 DrawImage调用中的ImageAttributes类

代码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:在白色图像上显示灰色边框的代码

这是调整大小后的图像:

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

新图像的宽度大小=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
更新日期:2011年7月30日。

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
代码1解决了白色图像上的灰色边框问题,但出现了新问题。问题出在这一行代码中:

canvas.DrawImage(旧图像,新矩形(新点(0,0),newSize),0,0,newImage.Width,newImage.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
如果我想上传、调整和保存原始图像,例如640x480px,目标尺寸为400px。作为输出,我得到了一个宽度为400px、高度为300px、没有灰色边框的图像,但oldImage没有调整大小/缩放到400px。相反,旧图像是以原始分辨率绘制的如何缩放要正确绘制的旧图像?有人能告诉我正确的解决方案或修改代码吗?

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

由于以下代码行,代码1无法正常工作:

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
canvas.DrawImage(旧图像,新矩形(新点(0,0),newSize),0,0,newImage.Width,newImage.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, 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
画布.DrawImage(旧图像,新矩形(新点(0,0),新大小),0,0,oldImage.WidtholdImage.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):

public void ResizeImage(HttpPostedFile fil,字符串sPhysicalPath,
字符串文件名,字符串sThumbNailFileName,
格式的图像格式,int(rez)
{
尝试
{
System.Drawing.Image oImg=System.Drawing.Image.FromStream(fil.InputStream);
十进制pixtosubstract=0;
小数百分比;
//违约
Size ThumbNailSizeToUse=新尺寸();
if(ThumbNailSize.WidthoImg.Size.Height)
{
百分比=((十进制)oImg.Size.Width-(十进制)ThumbNailSize.Width)/(十进制)oImg.Size.Width);
pixtosubstract=百分比*oImg.Size.Height;
ThumbNailSizeToUse.Width=ThumbNailSize.Width;
ThumbNailSizeToUse.Height=oImg.Size.Height-(int)pixtosubstract;
}
其他的
{
百分比=(((十进制)oImg.Size.Height-(十进制)ThumbNailSize.Height)/(十进制)oImg.Size.Height);
pixtosubstract=百分比*(十进制)oImg.Size.Width;
ThumbNailSizeToUse.Height=ThumbNailSize.Height;
ThumbNailSizeToUse.Width=oImg.Size.Width-(int)pixtosubstract;
}
}
其他的
{
ThumbNailSizeToUse.Width=oImg.Size.Width;
ThumbNailSizeToUse.Height=oImg.Size.Height;
}
位图bmp=新位图(ThumbNailSizeToUse.Width、ThumbNailSizeToUse.Height);
bmp.SetResolution(rez,rez);
System.Drawing.Image oThumbNail=bmp;
bmp=null;
Graphics oGraphic=Graphics.FromImage(oThumbNail);
oGraphic.CompositingQuality=CompositingQuality.HighQuality;
.SmoothingMode=SmoothingMode.HighQuality;
oGraphic.InterpolationMode=InterpolationMode.HighQualityBicubic;
矩形或矩形=新矩形(0,0,ThumbNailSizeToUse.Width,ThumbNailSizeToUse.Height);
制图图像(oImg、oRectangle);
保存(sPhysicalPath+sThumbNailFileName,of格式);
oImg.Dispose();
}
捕获(例外情况除外)
{
响应。写入(例如消息);
}
}

不确定问题是什么。您遇到了一个问题并找到了解决方案。@Antonio,能否提供一个不起作用的示例图像,以及
newSize
的维度,以便我们可以看到您看到的内容。我尝试了你提供的代码,没有发现任何问题。(虽然从技术上讲,我不会将此称为调整图像大小,但实际上您只是在更大的画布上绘制旧图像。)如果您遇到更多问题,请查看。还有,你看到了吗?如果你是为了一个ASP.NET项目做这件事,这是很重要的。如果你有工作代码,我相信你真的应该把它作为一个答案发布,并把它标记为正确的,这样这个问题就不会再出现在“未回答”下,而且如果人们从谷歌或其他网站上找到这个页面,它更容易作为参考。