Notifications
Clear all

[Closed] DotNet Image Quality/Compression?

Is there a way to somewhat easily set image quality for a jpeg for example? Right now, using the

	newBmp=dotNetObject "system.drawing.bitMap" newImgWidth newImgHeight ((dotNetClass "system.Drawing.Imaging.PixelFormat").Format32bppArgb)
							g=(dotNetClass "System.Drawing.graphics").FromImage newBmp
							g.CopyFromScreen newMPosX newMPosY 0 0 (dotNetObject "system.drawing.size"newImgWidth newImgHeight)(dotNetClass "System.Drawing.CopyPixelOperation").SourceCopy

newBmp.Save (newPath) (dotNetClass "system.Drawing.Imaging.ImageFormat").jpeg

Saves the jpeg at a pre-determinded compression it seems, which is pretty low. It would be awesome to be able to setup some sliders or something to expose that to the user to adjust the quality. If not that, then just being able to Up the quality in the code would be sweet.

thanks!

M

6 Replies

hi matthew,

you should be able to specify the interpolation mode to the graphics object.

 g.InterpolationMode = (dotnetclass "system.drawing.drawing2d.interpolationmode").bicubic

your options are –

.Bicubic : <System.Drawing.Drawing2D.InterpolationMode>, read-only, static
.Bilinear : <System.Drawing.Drawing2D.InterpolationMode>, read-only, static
.Default : <System.Drawing.Drawing2D.InterpolationMode>, read-only, static
.High : <System.Drawing.Drawing2D.InterpolationMode>, read-only, static
.HighQualityBicubic : <System.Drawing.Drawing2D.InterpolationMode>, read-only, static
.HighQualityBilinear : <System.Drawing.Drawing2D.InterpolationMode>, read-only, static
.Low : <System.Drawing.Drawing2D.InterpolationMode>, read-only, static
.NearestNeighbor : <System.Drawing.Drawing2D.InterpolationMode>, read-only, static

Sweet! Thanks dude. I will def. try it out and see what happens.

Edit:

So do I do it like this?? I tried this, and the image size/compression looked the same.

			newBmp=dotNetObject "system.drawing.bitMap" newImgWidth newImgHeight ((dotNetClass "system.Drawing.Imaging.PixelFormat").Format32bppArgb)
							g=(dotNetClass "System.Drawing.graphics").FromImage newBmp
							g.CopyFromScreen newMPosX newMPosY 0 0 (dotNetObject "system.drawing.size"newImgWidth newImgHeight)(dotNetClass "System.Drawing.CopyPixelOperation").SourceCopy
							g.InterpolationMode = (dotnetclass "system.drawing.drawing2d.interpolationmode").HighQualityBicubic

Any ideas?

Do I have to save the image and open it again to do this? Or can it be done before saving? I looked up some stuff here,
http://msdn.microsoft.com/en-us/library/system.drawing.drawing2d.interpolationmode.aspx?PHPSESSID=8mv3gjm6mn6qfq4mr33dgrrv80

But it talks about when scaling/rotating images.

thanks!

M

a simple google search turned up the answer…
http://www.vbforums.com/showthread.php?t=343060

EDIT: Converted to maxscript for you:

fn GetEncoderInfo mimeType =
(
	-- Get image codecs for all image formats
	ImageCodecInfo = dotnetclass "System.Drawing.Imaging.ImageCodecInfo"
	codecs = ImageCodecInfo.GetImageEncoders()
	
	-- Find the correct image codec
	notFound = true
	returnCodec = undefined
	for codec in codecs while notFound where matchpattern codec.MimeType pattern:mimeType do
	(
		returnCodec = codec
		notFound = false
	)

	returnCodec
)

fn SaveJPEG filename img quality =
(
	-- clamp quality between 0 and 100
	quality = amax 0 (amin 100 quality)
	-- Encoder parameter for image quality
	Encoder = dotnetclass "System.Drawing.Imaging.Encoder"
	qualityParam = dotnetobject "System.Drawing.Imaging.EncoderParameter" Encoder.Quality quality
	-- JPEG image codec
	jpegCodec = GetEncoderInfo "image/jpeg"
	
	encoderParams = dotnetobject "System.Drawing.Imaging.EncoderParameters" 1
	encoderParams.Param = #(qualityParam)

	img.Save filename jpegCodec encoderParams
)

Hi Matthew,

Nice work joel! I think i may have have assumed matthew wanted to resize a screen grab.
I have used the interpolation modes with the graphics object as part of a thumbnailing function in one of my controls. If you are creating a new bitmap, and using GDI to draw the image onto a smaller bitmap, you can choose the interpolation method that it draws onto the new bitmap. This is useful if you are drawing in memory rather than saving.

like here – http://www.bobpowell.net/highqualitythumb.htm

Wow… thanks Gravey, I didn’t expect that…

I guess I should have looked around more… idk why I didn’t search for straight up JPEG compression stuff. Although, if I found anything in C++ or C# or whatnot, I would have no idea how to convert it.

Many many thanks! :applause:

Thank you too LoneRobot, I’m sure I will need to know that info. at some point.