我已经在我当前的应用程序中编写了数百行图像管理代码,所有这些代码似乎都运行良好......除了上传的 GIF 的动画在某处松散。我已经将问题追溯到将图像保存到磁盘(以便缓存它们)的位置,因为它们直接从数据库(它们存储为 blob)显示它们看起来很好。
这是代码——我没有列出一堆对自定义对象和函数的调用,但我认为它们与问题无关——我认为它在这两个关键函数中的某个地方:
Private Sub CreateImageFileInCache()
Dim CMSImage As Cms.DataTransferObjects.Image = My.Application.ManagerFactory.ImageManagerInstance.ById(ImageId)
If CMSImage IsNot Nothing Then
SaveMetaInfo(CMSImage)
Using ms As New IO.MemoryStream(CMSImage.Data)
Dim expectedSize As New Drawing.Size(Width, Height)
Using img As System.Drawing.Image = resizeImage(System.Drawing.Bitmap.FromStream(ms), _
expectedSize, _
Drawing.Drawing2D.InterpolationMode.HighQualityBicubic, CMSImage.MimeType)
img.Save(ImageCachePath, GetEncoderInfo(CMSImage.MimeType), GenerateEncodingParameters)
img.Dispose()
End Using
End Using
End If
End Sub
Private Function resizeImage(ByVal imgToResize As System.Drawing.Image, ByVal size As System.Drawing.Size, ByVal Quality As System.Drawing.Drawing2D.InterpolationMode, ByVal format As String) As System.Drawing.Bitmap
Dim sourceWidth As Integer = imgToResize.Width
Dim sourceHeight As Integer = imgToResize.Height
Dim resizedImage As System.Drawing.Bitmap
Dim canvas As System.Drawing.Graphics
Dim calculatedSize As Drawing.Size = Global.Concrete.Base.Web.Controls.ProductImage.calculateNewImageSize(sourceWidth, sourceHeight, size)
If calculatedSize.Width > imgToResize.Width AndAlso calculatedSize.Height > imgToResize.Height Then
calculatedSize.Width = imgToResize.Width
calculatedSize.Height = imgToResize.Height
End If
resizedImage = New System.Drawing.Bitmap(calculatedSize.Width, calculatedSize.Height)
canvas = System.Drawing.Graphics.FromImage(resizedImage)
canvas.InterpolationMode = Quality
If Quality = Drawing.Drawing2D.InterpolationMode.HighQualityBicubic Then
canvas.CompositingQuality = Drawing.Drawing2D.CompositingQuality.AssumeLinear
canvas.SmoothingMode = Drawing.Drawing2D.SmoothingMode.AntiAlias
End If
canvas.DrawImage(imgToResize, 0, 0, calculatedSize.Width, calculatedSize.Height)
canvas.Dispose()
imgToResize.Dispose()
If format.Contains("gif") Then
Dim quantizer As Concrete.Cms.ImageManipulation.OctreeQuantizer
quantizer = New Concrete.Cms.ImageManipulation.OctreeQuantizer(255, 8)
resizedImage = quantizer.Quantize(resizedImage)
End If
Return resizedImage
End Function
任何建议都非常感激。
编辑:将 img.Save(ImageCachePath, GetFormat(MimeType)) 替换为对 .Save 的三参数调用仍然会产生静态 gif。
编辑 2:实际上对 GIF 几乎做任何事情似乎都会阻止它的动画!尝试使用 Canvas 调整它的大小,并尝试使用 Quantizing 来保持质量似乎都搞砸了动画。
干杯,马特