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