我正在使用以下代码裁剪图片:
Dim oImg As System.Drawing.Image = System.Drawing.Image.FromFile(sSrc)
'prep new image
Dim oCanvas As System.Drawing.Bitmap
Select Case oImg.PixelFormat
Case 198659, 197634, 196865, 65536 'indexed formats (as found in gifs) cant be used to make a graphic object
oCanvas = New System.Drawing.Bitmap(w, h, PixelFormat.Format32bppArgb)
Case Else 'use src img's pixel format
oCanvas = New System.Drawing.Bitmap(w, h, oImg.PixelFormat)
End Select
oCanvas.MakeTransparent()
Dim oGraphic As Graphics = Graphics.FromImage(oCanvas)
oGraphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic
oGraphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality
oGraphic.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality
oGraphic.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality
oGraphic.Clear(Color.Transparent)
oGraphic.DrawImage(oImg, New Rectangle(0, 0, w, h), X, Y, w, h, GraphicsUnit.Pixel)
Select Case sTgtFileExt
Case "jpg"
Dim jpgEncoder As ImageCodecInfo = GetEncoder(ImageFormat.Jpeg)
Dim myEncoder As System.Drawing.Imaging.Encoder = System.Drawing.Imaging.Encoder.Quality
Dim myEncoderParams As New EncoderParameters(1)
Dim myEncoderQuality As New EncoderParameter(myEncoder, CType(98L, Int32)) '98%
myEncoderParams.Param(0) = myEncoderQuality
oCanvas.Save(sTgt, jpgEncoder, myEncoderParams)
Case "png", "gif"
oCanvas.Save(sTgt, System.Drawing.Imaging.ImageFormat.Png)
Case "tiff", "tif"
oCanvas.Save(sTgt, System.Drawing.Imaging.ImageFormat.Tiff)
Case Else
oCanvas.Save(sTgt, System.Drawing.Imaging.ImageFormat.Png)
End Select
特别是对于裁剪.jpg源文件,我最终会得到裁剪后的图像,这些图像文件大小比未裁剪的原始文件大5-8倍。
例如,我可以输入一个大小为800kb的图像,然后将其中间的一半裁剪成一个新图像,当保存的图像超过5mb时。
如果我不能将jpeg质量保持在95%或更高,我会得到一张质量低于原始图像的图像,但文件大小仍然相同或更多。
我想要的是与原始图像质量相同的图像。什么是导致大小增加的代码,即使只是原始的裁剪部分?
答案 0 :(得分:0)
发现错误......它与图像处理无关。我最后一个案例陈述中的错误选择了编码器。我捕获扩展的代码有"。"字符存在,但我的案例名称没有,因此,它自动命中其他情况,默认图像类型为png,这当然是无损格式,并且通常具有更高的文件大小