ASP.NET 从流中保存 .jpg 文件的代码不保存 .gif 文件

ASP.NET code that saves .jpg files from stream does not save .gif files

我遇到错误 A Graphics object cannot be created from an image that has an indexed pixel format.

下面的代码在

时将图像保存在我的服务器上
imageURL="http://www.wijny.nl/imagestore/product/375/villamariaestateprivatebinsauvignonblancmarlboroughnewzealand10126029.jpg"

代码在

时不保存任何图像
imageURL="http://www.wijny.nl/imagestore/product/377/villamariasinglevineyardcha.gif"

我看到的唯一区别是图像源的格式,.jpg 文件存储正确,.gif 文件不正确。我如何确保代码保存任何类型的图像,无论是 .png、.jpg 还是 .gif?

Dim imgRequest As WebRequest
Dim imgResponse As WebResponse
Dim imgStream As Stream
imgRequest = WebRequest.Create(imageURL)

Dim localImageFilename as String = 'test.jpg'

Try
    imgResponse = imgRequest.GetResponse()
    imgStream = imgResponse.GetResponseStream()
Catch ex As Exception
    LogError("imgRequest.GetResponse", ex.Message)
End Try

imgRemoteImage = System.Drawing.Image.FromStream(imgStream)
localImagePath = Server.MapPath(ConfigurationManager.AppSettings("photospath").ToString) + localImageFilename

ResizeAndSaveImage(1200, 1200, localImagePath, imgRemoteImage)

Private Function ResizeAndSaveImage(ByVal maxWidth As Integer, ByVal maxHeight As Integer, ByVal path As String, ByVal img As System.Drawing.Image) As Boolean
    Dim newWidth, newHeight As Integer
    Dim scaleFactor As Double
    Dim bResult As Boolean
    newWidth = img.Width
    newHeight = img.Height

    If img.Width > maxWidth Or img.Height > maxHeight Then
        If img.Width > maxWidth Then
            scaleFactor = maxWidth / img.Width
            newWidth = Math.Round(img.Width * scaleFactor, 0)
            newHeight = Math.Round(img.Height * scaleFactor, 0)
        End If
        If newHeight > maxHeight Then
            scaleFactor = maxHeight / newHeight
            newWidth = Math.Round(newWidth * scaleFactor, 0)
            newHeight = Math.Round(newHeight * scaleFactor, 0)
        End If
    End If

        Try
            Dim bMap As New Bitmap(newWidth, newHeight, img.PixelFormat)

            Dim gr As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(bMap)
            gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality
            gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality
            gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High

            Dim rectDestination As New System.Drawing.Rectangle(0, 0, newWidth, newHeight)
            gr.DrawImage(img, rectDestination, 0, 0, img.Width, img.Height, GraphicsUnit.Pixel)

            bMap.Save(path, System.Drawing.Imaging.ImageFormat.Jpeg) 
            bMap.Dispose()

            bResult = True

        Catch ex As Exception

        End Try

    Return bResult
End Function

为您的函数添加一个参数:

Private Function ResizeAndSaveImage(ByVal maxWidth As Integer, ByVal maxHeight As Integer, ByVal path As String, ByVal img As System.Drawing.Image, byval Extension as String) As Boolean ' where extension comes from your filename...it'll either be GIF, JPG, or PNG.

然后在保存位图的地方做这样的事情:

if Extension = "JPG" then
    bMap.Save(path, System.Drawing.Imaging.ImageFormat.Jpeg) 
elseif Extension = "GIF" then
    bMap.Save(path, System.Drawing.Imaging.ImageFormat.Gif)
else
    bMap.Save(path, System.Drawing.Imaging.ImageFormat.Png) 
end if

您可能还想包括一个检查以查看是否传递了有效的扩展名,但这取决于您在做什么以及您认为是否有必要。

原来我不得不更改 PixelFormat 参数,

这适用于 .gif 个文件:bMap = New Bitmap(newWidth, newHeight, PixelFormat.Format24bppRgb)