Question Saving Jpg

Zexor

Well-known member
Joined
Nov 28, 2008
Messages
520
Programming Experience
3-5
I have a jpg file that is around 400k, 96dpi and 24bit. And my program loaded it into img as bitmap. then i just saved it back with img.save the file size became 1.6 Mb and 32bit. How do i change those dpi and bit depth value and/or change the file size with he jpg quality setting? also when i try to open the new save file with photoshop, it give me an unknown or invalid jpeg marker type is found. I could open the file in a viewer but not photoshop
 
Original image was 218KB and resultant image was 193KB at 24-bit depth on both. Lowering the quality setting from 100 to 75 resulted in a 49KB image.

VB.NET:
Imports System.Drawing.Imaging

Public Class Form1

	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		Dim inImg As Image = Image.FromFile("C:\Temp\Images\DarkStout.jpg")
		SaveImage("C:\Temp\Images\DarkStout1.jpg", inImg, "image/jpeg", 75, 24L)
	End Sub

	Private Sub SaveImage(ByVal filePath As String, ByVal img As Image, ByVal mimeType As String, ByVal quality As Long, ByVal bitDepth As Long)
		If quality > 100 OrElse quality < 0 Then
			Throw New ArgumentException("Valid qualities are between 0 and 100")
		End If

		Dim qualParam As New EncoderParameter(Encoder.Quality, quality)
		Dim colorParam As New EncoderParameter(Encoder.ColorDepth, bitDepth)
		Dim encoderParams As New EncoderParameters(2)
		encoderParams.Param(0) = qualParam
		encoderParams.Param(1) = colorParam

		Dim ici As ImageCodecInfo = GetImageCodec(mimeType)
		img.Save(filePath, ici, encoderParams)

	End Sub

	Private Function GetImageCodec(ByVal mimeType As String) As ImageCodecInfo

		For Each ici As ImageCodecInfo In ImageCodecInfo.GetImageEncoders()
			If ici.MimeType = mimeType Then
				Return ici
			End If
		Next
		Return Nothing

	End Function

End Class
 
Read data

Thanks that worked great. Can you read the original file's bit depth and set the target file's depth to be the same?

can i put inImg.PixelFormat as bitDepth somehow?

VB.NET:
Dim colorParam As New EncoderParameter(Encoder.ColorDepth, bitDepth)
 
Last edited:
Back
Top