?
Solved

Change file size of image

Posted on 2016-08-11
4
Medium Priority
?
65 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 44

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 44

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 two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
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…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

752 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