Solved

Change file size of image

Posted on 2016-08-11
4
39 Views
Last Modified: 2016-08-15
Hi
I wish to shrink the file size of jpg images, using VB.Net 2015.
So the user would select an image, then a smaller image would be saved. These images are usually photographs of 2Mb, can be up to 8Mb.
The smaller photograph:
           
  • should be 200k or less.
  •             The width/height ratio remain the same.
  •             Save to the same folder, but with a new name

I've seen some code which resizes by a percentage. However I want to make sure the size is always under 200k, no matter the size of the original.
What would be the best way to do this in VB.Net?
Thanks
0
Comment
Question by:jdhackett
  • 2
4 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
>>I've seen some code which resizes by a percentage.
Use that code and keep reducing the percentage until the file size is what you desire.

Logic.
percentreduction = 90   90 % of original is target
while filesize > 200000
  percentreduction = percentreduction - 10
  reducefile percentreduction
end while
0
 
LVL 1

Author Comment

by:jdhackett
Comment Utility
Not a bad idea Andy, but won't it be slow to save the file multiple times and then get the file size?
I was hoping there was a setting within the vb.net image control that lets you specify the file size.
0
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 250 total points
Comment Utility
>> setting within the vb.net image control that lets you specify the file size.

Sorry, but there isn't because one doesn't know in advance how much a jpg image will compress.  You can improve that algorithm by making guesses instead of just decrementing the percentage by a regular amount.
0
 
LVL 12

Assisted Solution

by:funwithdotnet
funwithdotnet earned 250 total points
Comment Utility
When reducing JPEG files there are three settings: size, resolution and compression quality.  ALl will affect file size.

On a side note, when reducing an image, it helps to sharpen the image after the reduction. Unfortunately, I found the .NET Image library didn't do a good job. Also, for some reason I do not recall, the results were better when the image was rotated 180 degrees. Naturally, I would perform that step twice.

I used the excellent AForge.Imaging library for sharpening. Almost the entire library is available under LGPL v3 license.

Good luck!
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video discusses moving either the default database or any database to a new volume.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now