Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 改进我的图像调整功能以获得更好的图像质量。。。循环减少?_Asp.net_Image Processing_Graphics_Bitmap_Image Resizing - Fatal编程技术网

Asp.net 改进我的图像调整功能以获得更好的图像质量。。。循环减少?

Asp.net 改进我的图像调整功能以获得更好的图像质量。。。循环减少?,asp.net,image-processing,graphics,bitmap,image-resizing,Asp.net,Image Processing,Graphics,Bitmap,Image Resizing,不久前,我试图为我的网站的后端创建一个图像上传工具。我成功地做到了这一点,但在较小的图像上,我的图像质量仍然很差 我需要创建4个图像: 最大放大图像1800 x 1800像素 最大显示图像180 x 275像素 搜索图像最大120 x 100px 最小缩略图最大50 x 50像素 在上传之前,我通常使用Photoshop或其他工具手动调整图像大小至1800 x 1800,然后使用下面的代码上传并调整大小(图像均为JPG) 变量包括: FileName=最初上传时没有问题 NewFileName=

不久前,我试图为我的网站的后端创建一个图像上传工具。我成功地做到了这一点,但在较小的图像上,我的图像质量仍然很差

我需要创建4个图像:

  • 最大放大图像1800 x 1800像素
  • 最大显示图像180 x 275像素
  • 搜索图像最大120 x 100px
  • 最小缩略图最大50 x 50像素
  • 在上传之前,我通常使用Photoshop或其他工具手动调整图像大小至1800 x 1800,然后使用下面的代码上传并调整大小(图像均为JPG)

    变量包括:

  • FileName=最初上传时没有问题
  • NewFileName=将已调整大小的图像另存为的文件名
  • maxWidth/maxHeight-不言自明
  • uploadDir=要保存到的目录
  • 分辨率=质量jpg分辨率0-100,我在这些示例中使用80

     Public Shared Sub ResizeImages(FileName, NewFileName, maxWidth, maxHeight, uploadDir, resolution)
     Try
        Dim originalImg As System.Drawing.Image = System.Drawing.Image.FromFile(uploadDir & FileName)
        Dim aspectRatio As Double
        Dim newHeight As Integer
        Dim newWidth As Integer
       ' Calculate Size '
            If originalImg.Width > maxWidth Or originalImg.Height > maxHeight Then
                If originalImg.Width >= originalImg.Height Then ' image is wider than tall
                    newWidth = maxWidth
                    aspectRatio = originalImg.Width / maxWidth
                    newHeight = originalImg.Height / aspectRatio
                Else ' image is taller than wide
                    newHeight = maxHeight
                    aspectRatio = originalImg.Height / maxHeight
                    newWidth = originalImg.Width / aspectRatio
                End If
            Else ' if image is not larger than max then keep original size
                newWidth = originalImg.Width
                newHeight = originalImg.Height
            End If
    
            Dim newImg As New Bitmap(originalImg, CInt(newWidth), CInt(newHeight)) '' blank canvas
            Dim canvas As Graphics = Graphics.FromImage(newImg) 'graphics element
    
            '*** compress ***'
            Dim myEncoderParameters As EncoderParameters
            myEncoderParameters = New EncoderParameters(1)
            ' set quality level based on "resolution" variable
            Dim myEncoderParameter = New EncoderParameter(System.Drawing.Imaging.Encoder.Quality, CType(resolution, Int32))
            myEncoderParameters.Param(0) = myEncoderParameter
    
            canvas.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality
            canvas.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic
            canvas.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality
    
            canvas.DrawImage(newImg, New Rectangle(0, 0, newWidth, newHeight))
            newImg.Save(uploadDir & (NewFileName), getCodec("image/jpeg"), myEncoderParameters)
    
            '*** Close ***'
            canvas.Dispose()
            originalImg.Dispose()
            newImg.Dispose()
            '*** Nothing ***'
            canvas = Nothing
            newImg = Nothing
            originalImg = Nothing
    
        Catch ex As Exception
            HttpContext.Current.Response.Write(ex.ToString & " " & uploadDir & " " & FileName & " _ " & NewFileName)
        End Try
    
    End Sub
    
  • 为了实现所有四个图像,我将所需的大小作为一个列表传递,然后按照预期文件大小的降序循环该列表,因此,首先是最大的一个,然后将最近上传的图像作为
    FileName
    参数传递到函数中,以便每次函数都接收到一个较小的图像,因此,我在阅读各种帖子时意识到,如果不尝试将2000x2000px的图像调整为50x50px,那么这种大幅减少将导致质量下降

    在使用这种方法运行循环之后,我的小缩略图的质量相当好,但是我的中间图像仍然很差

    此处按大小降序排列:

    http://www.hartnollguitars.co.uk/Products2/0/0/0/9/6/57/1347831580-dis.jpg http://www.hartnollguitars.co.uk/Products2/0/0/0/9/6/57/1347831580-tb.jpg

    正如你所看到的,“搜索”和“显示”图像在吉他边缘仍然是块状的

    我做错了什么

    如果我的缩减量太大,我将如何运行内存中的逐步缩减

    我的意思是,我觉得上面的函数每次都会将文件保存到光盘上,这一定会占用一些时间,因此如果我要循环一个缩小函数,在内存中以小增量(比如每次10%)缩小图像,然后在缩小到正确大小时将最终图像保存到光盘上。但我不知道该怎么做

    我使用的是ASP.NET2.0,而且我对它比较陌生,所以我并不完全了解所有可用的方法

    任何代码示例都会大有帮助


    谢谢

    您所做的错误是使用
    位图
    构造函数创建缩小的图像,然后将该图像绘制到自身上。
    位图
    构造器自然无法使用您稍后在
    图形
    对象中设置的质量设置来调整图像大小,因此质量将很差

    相反,您应该使用构造函数创建一个空白的
    位图
    对象,该构造函数只接受以下大小:

    Dim newImg As New Bitmap(newWidth, newHeight)
    
    然后,您应该在画布上绘制原始图像:

    canvas.DrawImage(originalImg, New Rectangle(0, 0, newWidth, newHeight))
    

    您考虑过ImageResize.net吗?