我收到错误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
答案 0 :(得分:0)
为您的函数添加参数:
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
您可能还希望包含检查以查看是否正在传递有效的附加信息,但这取决于您正在做什么以及您认为是否有必要。
答案 1 :(得分:0)
原来我必须更改PixelFormat
参数,
适用于.gif
个文件:bMap = New Bitmap(newWidth, newHeight, PixelFormat.Format24bppRgb)