Solved

Change file size of image

Posted on 2016-08-11
4
57 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 250 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 250 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

749 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