.Net 图像裁剪导致文件比原始文件大得多
.Net image crop resulting in files much larger than original
我正在使用如下代码裁剪图像:
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% 或更高,我得到的图像质量低于原始图像,但文件大小仍然相同或更大。
我想要的是与原始图像质量相同的图像。导致大小大幅增加的代码是什么,即使只是原始部分的裁剪部分?
发现错误...它与图像处理无关。在我选择编码器的最终案例陈述中是一个错误。我捕获扩展名的代码有“。”字符存在,但我的案例名称没有,因此,它自动命中 else 案例,默认图像类型为 png,这当然是一种无损格式,并且通常具有更大的文件大小
我正在使用如下代码裁剪图像:
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% 或更高,我得到的图像质量低于原始图像,但文件大小仍然相同或更大。
我想要的是与原始图像质量相同的图像。导致大小大幅增加的代码是什么,即使只是原始部分的裁剪部分?
发现错误...它与图像处理无关。在我选择编码器的最终案例陈述中是一个错误。我捕获扩展名的代码有“。”字符存在,但我的案例名称没有,因此,它自动命中 else 案例,默认图像类型为 png,这当然是一种无损格式,并且通常具有更大的文件大小