【问题标题】:ASP.NET code that saves .jpg files from stream does not save .gif files从流中保存 .jpg 文件的 ASP.NET 代码不保存 .gif 文件
【发布时间】:2016-05-08 15:46:18
【问题描述】:

我收到错误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

【问题讨论】:

    标签: asp.net vb.net stream jpeg gif


    【解决方案1】:

    给你的函数添加一个参数:

    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
    

    您可能还需要检查是否通过了有效的扩展,但这取决于您在做什么以及您是否认为有必要。

    【讨论】:

    • 我试过了,现在得到:A Graphics object cannot be created from an image that has an indexed pixel format.
    【解决方案2】:

    原来我必须更改PixelFormat 参数,

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

    【讨论】:

      猜你喜欢
      • 2019-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-12
      • 2018-11-12
      相关资源
      最近更新 更多