Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Change file size of image

Posted on 2016-08-11
4
Medium Priority
?
69 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 41751811
>>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
ID: 41751870
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 45

Accepted Solution

by:
AndyAinscow earned 1000 total points
ID: 41751885
>> 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 1000 total points
ID: 41752073
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

610 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