|
-
Nov 26th, 2012, 05:58 AM
#1
Thread Starter
PowerPoster
How to reduce JPEG Image size
Hi Guys,
Im using this code to resize images:
Code:
Public Class RJpeg
Private mFileName As String
Private mWidth As Integer
Private mHeight As Integer
Public Property FileName() As String
Get
Return mFileName
End Get
Set(ByVal Value As String)
mFileName = Value
End Set
End Property
Public Property NewWidth() As Integer
Get
Return mWidth
End Get
Set(ByVal value As Integer)
mWidth = value
End Set
End Property
Public Property NewHeight() As Integer
Get
Return mHeight
End Get
Set(ByVal value As Integer)
mHeight = value
End Set
End Property
Public Sub New()
mHeight = 0
mWidth = 0
mFileName = ""
End Sub
Public Sub ResizeImage()
Dim imgOriginal As Image
Dim OriginalHeight As Single
Dim OriginalWidth As Single
Dim NewWidth As Integer
Dim NewHeight As Integer
Dim ResizedBitmap As Bitmap
Dim ResizedImage As Graphics
Dim quality As Long = 40
Dim qualityparam As New EncoderParameter(Encoder.Quality, quality)
Dim jpegCodec As ImageCodecInfo = GetEncoderInfo("image/jpeg")
Dim encoderParams As New EncoderParameters(1)
encoderParams.Param(0) = qualityparam
imgOriginal = Image.FromFile(mFileName)
OriginalHeight = imgOriginal.Height
OriginalWidth = imgOriginal.Width
' Finds height and width of resized image
If (OriginalHeight > OriginalWidth) Then
NewHeight = mHeight
NewWidth = mWidth '((OriginalWidth / OriginalHeight) * NewImageSize)
Else
NewWidth = mWidth
NewHeight = mHeight '((OriginalHeight / OriginalWidth) * NewImageSize)
End If
' Create new bitmap that will be used for resized image
ResizedBitmap = New Bitmap(NewWidth, NewHeight)
ResizedImage = Graphics.FromImage(ResizedBitmap)
' Resized image will have best possible quality
ResizedImage.InterpolationMode = InterpolationMode.HighQualityBicubic
ResizedImage.CompositingQuality = CompositingQuality.HighQuality
ResizedImage.SmoothingMode = SmoothingMode.HighQuality
' Draw resized image
ResizedImage.DrawImage(imgOriginal, 0, 0, NewWidth, NewHeight)
' Save thumbnail to file
imgOriginal.Dispose()
ResizedBitmap.Save(mFileName, jpegCodec, encoderParams)
' It is important to take care of memory, especially in cases
' when code works with graphics
ResizedBitmap.Dispose()
ResizedImage.Dispose()
End Sub
Private Function GetEncoderInfo(ByVal mimeType As String) As ImageCodecInfo
Dim codecs As ImageCodecInfo() = ImageCodecInfo.GetImageEncoders
For i As Integer = 0 To codecs.Length - 1
If codecs(i).MimeType = mimeType Then
Return codecs(i)
End If
Next
Return Nothing
End Function
when I use my class I pass 208 as the width and height. My images are resized fine. Say I get an image with the dimensions 394x394 and a size of 97kb.
After I resize it's dimensions are 208x208 and the size is 56kb.
I need to reduce the size of the jpeg even more to say 15kb. How can I do this?
Also, when I open the jpeg in photoshop it says "Invalid JPEG marker type". Any idea why this would happen? Please help
-
Nov 26th, 2012, 12:26 PM
#2
Re: How to reduce JPEG Image size
Can I suggest that you take a look at this thread here, particularly my last post, as this is basically a whole lot easier than you're making it!
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|