我已经写了几百行图像管理代码在我当前的应用程序中,似乎很好地工作......除了上传的GIF在动画的某处放松动画之外。我将问题追溯到将图像保存到磁盘(以缓存它们)的方式,直接从数据库中显示它们(将它们存储为blob),这看起来很好。在dotnet中保存和调整GIF的大小 - 如何预先设置动画?
下面是代码 - 有一些调用自定义对象和函数,但我不相信它们与问题有关 - 我认为它在这两个关键函数的某处:
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调整大小,并尝试使用量化来保证质量,似乎都会使动画搞砸。
干杯, 马特