相同的代码在不同的服务器上产生不一致的图像质量

2024-01-12

拍摄以下两张图像:

开发版本 - IIS7 Windows 7 Pro 64 位机器

实时版本 - IIS7 Windows Server 2008 64 位机器

请注意,实时版本是如何“像素化”并且看起来质量较低,而开发版本是平滑的、抗锯齿的并且看起来不错。它们都是由相同的代码生成的:

' Settings
Dim MaxHeight As Integer = 140
Dim MaxWidth As Integer = 140
Dim WorkingFolderPath As String = "\\server\share\bla\"
Dim AllowedFileExtensions As New ArrayList
AllowedFileExtensions.Add(".jpg")
AllowedFileExtensions.Add(".jpeg")

' Select an image to use from the WorkingFolder
Dim ImageFileName As String = ""
Dim WorkingFolder As New IO.DirectoryInfo(WorkingFolderPath)
Dim SourceImages As IO.FileInfo() = WorkingFolder.GetFiles()

For Each SourceImage As IO.FileInfo In SourceImages
    If AllowedFileExtensions.Contains(SourceImage.Extension.ToLower) = True Then
        ImageFileName = SourceImage.Name
    End If
Next

' Determine path to selected image (if no image was found use a placeholder)
Dim PhysicalPath As String = ""
If ImageFileName = "" Then
    ' No Image was found, use the filler image
    PhysicalPath = Server.MapPath("ProductOfTheMonthMissing.jpg")
Else
    ' An Image was found, Redirect to it / build path for Thumnailing
    If Request.QueryString("FullSize") = "true" Then
        Response.Redirect("../share/bla/" & ImageFileName)
    Else
        PhysicalPath = WorkingFolderPath & ImageFileName
    End If
End If

' Load image and output in binary (resizing if necessary)
Using ProductImage As System.Drawing.Image = System.Drawing.Image.FromFile(PhysicalPath)
    Dim newWidth As Integer = ProductImage.Width
    Dim newHeight As Integer = ProductImage.Height

    ' Check if selected size is too big, if so, determine new size
    If ProductImage.Width > MaxWidth Or ProductImage.Height > MaxHeight Then
        Dim ratioX As Double = CDbl(MaxWidth) / ProductImage.Width
        Dim ratioY As Double = CDbl(MaxHeight) / ProductImage.Height
        Dim ratio As Double = Math.Min(ratioX, ratioY)

        newWidth = CInt(ProductImage.Width * ratio)
        newHeight = CInt(ProductImage.Height * ratio)
    End If

    ' Create a new bitmap from the image with new size
    Dim Codecs As ImageCodecInfo() = ImageCodecInfo.GetImageEncoders()
    Dim CodecInfo As ImageCodecInfo = Nothing
    Dim ProductOfTheMonth As New Bitmap(ProductImage, newWidth, newHeight)
    Dim ReSizer As Graphics = Graphics.FromImage(ProductOfTheMonth)

    ReSizer.InterpolationMode = InterpolationMode.HighQualityBicubic
    ReSizer.SmoothingMode = SmoothingMode.HighQuality
    ReSizer.PixelOffsetMode = PixelOffsetMode.HighQuality
    ReSizer.CompositingQuality = CompositingQuality.HighQuality

    ' Ensure the encoder uses the best quality settings
    Dim EncoderParams As New EncoderParameters(3)
    EncoderParams.Param(0) = New EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L)
    EncoderParams.Param(1) = New EncoderParameter(System.Drawing.Imaging.Encoder.ScanMethod, CInt(EncoderValue.ScanMethodInterlaced))
    EncoderParams.Param(2) = New EncoderParameter(System.Drawing.Imaging.Encoder.RenderMethod, CInt(EncoderValue.RenderProgressive))

    ' Set jpeg as the output codec
    For Each Codec As ImageCodecInfo In Codecs
        If Codec.MimeType = "image/jpeg" Then
            CodecInfo = Codec
        End If
    Next

    ' Ready a memory stream and byte array
    Dim MemStream As New MemoryStream()
    Dim bmpBytes As Byte()

    ' Save the image the the memory stream & prep ContentType for HTTP reasponse
    Response.ContentType = "image/jpeg"
    ProductOfTheMonth.Save(MemStream, CodecInfo, EncoderParams)

    ' Flush memory stream into byte array & flush to browser
    bmpBytes = MemStream.GetBuffer()
    Response.BinaryWrite(bmpBytes)

    ' Cleanup
    ProductOfTheMonth.Dispose()
    MemStream.Close()
    ProductImage.Dispose()
End Using

这背后的原因是什么以及我该如何解决这个问题?大概是实时网络服务器上的 GD 问题 - 但我不知道是什么 - 我试图尽可能彻底地设置图形和编解码器设置,但它仍然不同?

Edit:两个示例中的源图像也相同(位于中央 unc 共享上)- 源图像的副本here https://i.stack.imgur.com/tFsar.jpg


我曾经在这里回答过类似的问题:.Net 中的图形错误图像插值 https://stackoverflow.com/questions/7649288/graphics-wrong-image-interpolation-in-net/7760559,但简而言之,似乎不同的平台使用不同的内部算法(或者可能是 GDI 中的内部舍入问题)。

无论如何,问题出在设置上。所以请尝试以下操作:

Using s As Bitmap = DirectCast(Bitmap.FromFile(PhysicalPath), Bitmap)
    Dim scale As Double = Math.Min(140.0 / s.Width, 140.0 / s.Height)
    Using d As New Bitmap(CInt(Math.Floor(scale * s.Width)), CInt(Math.Floor(scale * s.Height)), System.Drawing.Imaging.PixelFormat.Format24bppRgb)
        Using dg As Graphics = Graphics.FromImage(d)
            dg.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic
            dg.SmoothingMode = SmoothingMode.HighQuality
            dg.PixelOffsetMode = PixelOffsetMode.HighQuality
            dg.CompositingQuality = CompositingQuality.HighQuality
            dg.Clear(Color.White)
            dg.DrawImage(s, New Rectangle(0, 0, d.Width, d.Height), New Rectangle(0, 0, s.Width, s.Height), GraphicsUnit.Pixel)
        End Using

        Dim jpegArgs As New EncoderParameters(3)
        jpegArgs.Param(0) = New EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L)
        jpegArgs.Param(1) = New EncoderParameter(System.Drawing.Imaging.Encoder.ScanMethod, CInt(EncoderValue.ScanMethodInterlaced))
        jpegArgs.Param(2) = New EncoderParameter(System.Drawing.Imaging.Encoder.RenderMethod, CInt(EncoderValue.RenderProgressive))

        Dim Codecs As ImageCodecInfo() = ImageCodecInfo.GetImageEncoders()
        Dim jpegParams As ImageCodecInfo = Nothing

        '#### Set jpeg as the output codec
        For Each Codec As ImageCodecInfo In Codecs
            If Codec.MimeType = "image/jpeg" Then
                jpegParams = Codec
            End If
        Next

        Response.Clear()
        Response.ContentType = "image/jpeg"

        d.Save(Response.OutputStream, jpegParams, jpegArgs)
    End Using
End Using

祝你好运!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

相同的代码在不同的服务器上产生不一致的图像质量 的相关文章

  • 颜色变换器功能上的堆栈溢出错误

    我有两种颜色 红色 和 鲑鱼色 我需要动态创建面板以及面板背景颜色 这些颜色必须介于两种颜色之间 红色 public Color x y protected void Page Load object sender EventArgs e
  • 数字或货币的字符串格式?

    我需要为每个千给出逗号 所以我用了DataFormatString 0 它运行良好 但当值为0 它正在显示 00 我只想只显示 0 我们怎样才能做到这一点 DataFormatString 0 C0 这将格式化为小数点后 0 位的货币 Da
  • 异步提交或回滚事务范围

    正如许多人所知 TransactionScope当async await Net 中引入了模式 如果我们尝试使用一些它们就会损坏await在事务范围内调用 现在这个问题已经解决了 感谢范围构造函数选项 a 17527759 1178314
  • Swashbuckle 在 ASP.NET Core 中失败并出现 NotSupportedException 异常

    我跟着这个关于如何在我的 asp net core 2 2 项目中添加 swashbuckle 当我运行该项目时 我收到以下错误 处理请求时发生未处理的异常 NotSupportedException HTTP 方法 GET 和路径 id
  • 为什么在此单元测试中,BackgroundWorker 没有在正确的线程上调用 RunWorkerCompleted?

    backgroundWorker 的全部目的是在执行耗时的任务后更新 UI 组件正如广告所宣传的那样在我的 WPF 应用程序中 但是在我的测试中 回调不会在调用线程上调用 Test public void TestCallbackIsInv
  • .NET 是否有相当于 Python 中的 **kwargs 的功能?

    我一直无法通过典型渠道找到这个问题的答案 在Python中我可以有以下函数定义 def do the needful kwargs Kwargs is now a dictionary i e do the needful spam 42
  • SolrNet:尝试添加和提交时 SolrConnectionException (400) 错误请求

    我已经到了 SolrNet 执行 Add 方法的地步 但是当我尝试 Commit 时 我收到了错误 以下是我的 schema xml 模型 调用它的代码以及我得到的错误 更奇怪的是 尽管出现错误 但在我重新启动 Tomcat 后 该模型仍会
  • 如何在C#中使用默认浏览器打开带有锚点(#)的html文件

    我正在尝试在 C 中打开上下文帮助文件 当我没有指定锚点时 它工作得很好 Process Start C Help Help htm 但是当我指定锚点时 它不会打开 Process Start C Help Help htm Toc3420
  • 寻找自定义 SynchronizationContext 的示例(单元测试所需)

    我需要定制同步上下文 http msdn microsoft com en us library system threading synchronizationcontext aspx that 拥有一个运行 Posts 和 Sends
  • NUnit 与 xUnit

    两者有什么区别NUnit http www nunit org and xUnit net https xunit net 开发其中两个而不是仅一个有什么意义 我读到 xUnit 是由 NUnit 的发明者开发的 xUnit net 是 N
  • 连接到 Facebook 并使用 api

    有没有好的教程如何制作简单的控制台 Facebook 应用程序 连接到 Facebook 并获取朋友列表 用户照片 状态或其他内容 我查看了 facebook SDK 的 facebook 示例 但如果我想在 facebook 上授权 我必
  • 该变量未声明或从未分配警告

    这是基类 public class BaseClass UserControl protected ListView list protected TreeView tree public BaseClass 儿童班 public part
  • 如何使用 PetaPoco 库自动从数据库创建模型?

    我的数据库中有一个表 我想为其创建一个带有 getter 和 setter 的模型类 对于我项目中的大部分任务 我使用 PetaPoco 我手动创建了模型 但很少有表有很多列 有没有办法使用 PetaPoco 从数据库创建模型 我强烈建议您
  • 生成Excel文件错误

    我在经典 ASP 中使用以下代码生成 Excel 文件 代码很简单并且有效 我在 Windows Vista x86 上的 IIS 7 0 下运行代码 两个问题 有一个奇怪的警告框 这是屏幕快照 http i27 tinypic com 2
  • 无法在.net core中使用WCF WSHttpBinding

    我正在尝试将我的项目从 net 移动到 net core 我最初在 net 中使用 WCF WSHttpBinding 服务 但无法在 net core 中使用相同的服务 我尝试使用 BasicHttpBinding 在客户端与 WsHtt
  • 如何将pdf页面设置设置为打印属性对话框?

    大家好 我想知道如何设置 pdf 页面设置到打印属性对话框 例如 如果我的 PDF 页面设置为横向 则布局会自动显示横向而不是纵向 如果我的 PDF 页面设置为纵向 则布局会自动显示纵向 我在这个主题上做了很多研发 但没有找到任何满意的链接
  • 为什么WCF中不允许方法重载?

    假设这是一个ServiceContract ServiceContract public interface MyService OperationContract int Sum int x int y OperationContract
  • 在 .NET 中记录 StackOverflowException

    最近 我的 NET 应用程序 asp net 网站 中出现了堆栈溢出异常 我之所以知道该异常是因为它出现在我的 EventLog 中 我知道 StackOverflow 异常无法被捕获或处理 但是有没有办法在它杀死您的应用程序之前记录它 我
  • 如何将 CSV 文件读入 .NET 数据表

    如何将 CSV 文件加载到System Data DataTable 根据CSV文件创建数据表 常规 ADO net 功能是否允许这样做 我一直在使用OleDb提供者 但是 如果您正在读取具有数值的行 但希望将它们视为文本 则会出现问题 但
  • 创建带有部分的选项卡式侧边栏 WPF

    我正在尝试创建一个带有部分的选项卡式侧边栏 如 WPF 中的以下内容 我考虑过几种方法 但是有没有更简单 更优雅的方法呢 方法一 列表框 Using a ListBox并将 SelectedItem 绑定到右侧内容控件所绑定的值 为了区分标

随机推荐