Important alert: (current site time 10/24/2014 2:24:22 PM EDT)
 

winzip icon

JPEG Encoder Class

Email
Submitted on: 11/23/2003 5:29:43 AM
By: korejwa 
Level: Advanced
User Rating: By 163 Users
Compatibility: VB 6.0
Views: 72833
author picture
(About the author)
 
     Save a picture as a baseline JPG file using only vb code. Open source, NO THIRD PARTY DEPENDENCIES, and as fast as typical commercial software when compiled. All JPEG compression code is contained in a single class that can be added to existing projects. There is also an Integer DCT version of this class written by Ron van Tilburg available on PSC. (CodeId=50351)
 
winzip iconDownload code

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. Afterdownloading it, you will need a program like Winzip to decompress it.Virus note:All files are scanned once-a-day by Planet Source Code for viruses, but new viruses come out every day, so no prevention program can catch 100% of them. For your own safety, please:
  1. Re-scan downloaded files using your personal virus checker before using it.
  2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.
  3. Scan the source code with Minnow's Project Scanner

If you don't have a virus scanner, you can get one at many places on the net including:McAfee.com

 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.


Other 5 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

4/26/2004 3:50:47 PM

Excellent work, man! Really.

(If this comment was disrespectful, please report it.)

 
5/3/2004 2:11:18 AM

Excellent Sir...
(If this comment was disrespectful, please report it.)

 
5/23/2004 12:45:57 PM

Unbelievable! I have never been into imaging at this level and needed a quick understanding by a supercharged, organized, genius. (At least I think that's what I think I need...) - Very technical, EXTREMELY well documented, very much appreciated. Class act. Thanks, Joe
(If this comment was disrespectful, please report it.)

 
6/6/2004 11:02:51 PM

thank god i found what i want
(If this comment was disrespectful, please report it.)

 
6/10/2004 3:13:48 PM

thats wonderfull! thanks.
(If this comment was disrespectful, please report it.)

 
6/22/2004 5:49:28 AM

This code is Wonderfull, congratulations sir.
(If this comment was disrespectful, please report it.)

 
7/6/2004 1:56:02 AM

Congratulations Mr.
it is very wonderful Code.
(If this comment was disrespectful, please report it.)

 
7/6/2004 11:34:44 PMkertz

Hi
I saw that code and it's pretty good. Can I use it for my project?
(If this comment was disrespectful, please report it.)

 
7/10/2004 1:03:16 PMPetko Petkov

Very good code. 5 globes.
Not work with 32 bit images. Not a big problem. Must only touch up cImage.cls.

(If this comment was disrespectful, please report it.)

 
7/25/2004 7:43:11 AMJeroen van de Ven

Nice!

Can I use it in my project? It will be used to save a backup copy of settings or something.
(If this comment was disrespectful, please report it.)

 
7/25/2004 9:58:43 PMdonn

Thanks man...I've been looking for this everywhere. With this, there's no need for external dll's and its nightmare. It's also a proof that it can be done in VB with people like you...Anyway, 5 globes from me....Long live VB...
(If this comment was disrespectful, please report it.)

 
7/26/2004 2:25:22 AM

i use win98 and it works not well. but the idea is very well!
(If this comment was disrespectful, please report it.)

 
7/26/2004 7:23:01 AMFabio La Vitola

Extremely fast with most of common used image sizes. Even if with 32bit images doesn't works, a Photoshop retouch 8-) will set useful images in 24bit (maybe a VB's picture obj limit ???).
Simply great, 5 globes (sorry but 6 globes is impossible !) !
Thanks !
(If this comment was disrespectful, please report it.)

 
7/26/2004 7:37:40 AMFabio La Vitola

If we soubstitute
if xxxxxxxx <> 24 then...

with
if xxxxxxxx <= 24 then...

we can use either 24 or 32 bit input impage, but we will save 24bit only.

With this "turbo patch" now we can correctly transform 32 bit images in jpg.

(If this comment was disrespectful, please report it.)

 
7/26/2004 12:29:21 PMal boz

wow, keep up the good job...
(If this comment was disrespectful, please report it.)

 
7/26/2004 8:45:02 PMPaul Turcksin

I'm speechless. Thanks for sharing this pearl. 5 from me.
(If this comment was disrespectful, please report it.)

 
7/27/2004 1:48:43 PM

Excellent Code Man..
(If this comment was disrespectful, please report it.)

 
7/31/2004 2:47:29 AMApeiron

I didn't think I'd ever see this in VB. I don't know how I missed your fine project until now. *****
(If this comment was disrespectful, please report it.)

 
8/10/2004 4:47:23 PMzoom

This is without a dout some of the most perfect working code on here its usefull and works awsome thanks bro. darn this is good code
(If this comment was disrespectful, please report it.)

 
9/18/2004 9:17:41 AMValentin Mihai

Too complicated, but the ideea is great!
(If this comment was disrespectful, please report it.)

 
9/18/2004 6:15:22 PMkorejwa

Too complicated?? All the code is in a single class. Saving a picture requires only two lines of code. How could I make this less complicated to use?
(If this comment was disrespectful, please report it.)

 
9/24/2004 1:56:24 PMVesa Piittinen

Well, it is a bit complicated for users who are not familiar with it. To make it easier you could make just a form with a picture and a single button doing the saving. Btw, I'm continuously getting "expression too complex" at CollectStatisticsDCInterleaved on the h = ... \ 8 line. Still wondering why.
(If this comment was disrespectful, please report it.)

 
9/24/2004 1:58:36 PMVesa Piittinen

Oh yes, and I got it resolved by moving \ 8 into a new line (h = h \ 8)
(If this comment was disrespectful, please report it.)

 
9/24/2004 3:50:00 PMkorejwa

"expression too complex"? You're pulling my leg, right? That line has 2 multiplies, 2 negates, 1 shift, and 1 add. This should not be too complex for any compiler. What version of VB are you using? How are you getting this error?
(If this comment was disrespectful, please report it.)

 
9/25/2004 1:20:16 PMVesa Piittinen

I have no idea why it did that. By splitting to two lines it started working. VB6 Pro is what I have. I called it from an usercontrol I was making and I just kept getting that. Then I split the line and it worked. Hmm... I might even post the project here.
(If this comment was disrespectful, please report it.)

 
9/25/2004 1:30:01 PMVesa Piittinen

Hmm, I now fixed the line back as it was and it is working. I guess I might have doodled with CopyMemory and it caused some weird memory problem.
(If this comment was disrespectful, please report it.)

 
10/26/2004 5:08:18 PMKeith Stanier

Its a great app, pity the rotation doesn't work. You just get a black picture. I've tried Refresh and AutoRedraw but it still doesn't show anything.

5 globes anyway thanks.
(If this comment was disrespectful, please report it.)

 
11/4/2004 5:04:43 PM

Congratulations, it's a excellent work.

In Windows 98 works fine, but why not in Windows XP? Appears the error
(If this comment was disrespectful, please report it.)

 
11/4/2004 5:13:32 PM

Ohh, my previous comment is wrong, continue... In Windows XP appears the error Subscript out of range in the line n = UBound(data) + 1 of CollectStatisticsDCInterleaved sub. Thanks.
(If this comment was disrespectful, please report it.)

 
11/5/2004 9:24:21 PM

I do appreciate reading these lines of code, really. Goog job !
I learn that we can declare Properties in a form ! I've never think to try it.
And your module is a pretty usefull !
(If this comment was disrespectful, please report it.)

 
12/21/2004 5:47:43 PM

Great code, but when i tried to implement in my own proyect appears the error Subscript out of range in the line
n = UBound(data) + 1 of
CollectStatisticsDCInterleaved sub

I used this code:

Dim MyPic As Picture
Dim m_Image As cImage

Set MyPic = CaptureActiveWindow()

Set m_Image = New cImage
m_Image.CopyStdPicture MyPic
Set MyPic = Nothing

Set m_Jpeg = New cJpeg

m_Jpeg.Quality = 75
m_Jpeg.SetSamplingFrequencies 2, 2, 1, 1, 1, 1
m_Jpeg.SampleHDC m_Image.hDC, m_Image.Width, m_Image.Height
m_Jpeg.SaveFile
(If this comment was disrespectful, please report it.)

 
12/21/2004 5:48:06 PM

Your code isn'n to complex as other users says, but can be mor simple if you put into a single module, calling a function, some like this:

Enum SampFreq
Samp_1_1 = 11
Samp_1_2 = 12
Samp_1_3 = 13
Samp_1_4 = 14
Samp_2_1 = 21
Samp_2_2 = 22
Samp_2_3 = 23
Samp_2_4 = 24
Samp_3_1 = 31
Samp_3_2 = 32
Samp_3_3 = 33
Samp_3_4 = 34
Samp_4_1 = 41
Samp_4_2 = 42
Samp_4_3 = 43
Samp_4_4 = 44
End Enum

Enum TheColor
Color = 0
Greyscale = 1
End Enum

Public Function SaveJPEGfile(TheImage As Picture, Quality As Integer, FileName As String, Optional SamplingFrequencies As SampFreq = 22, Optional Color As TheColor = 0)

What do you think?

(If this comment was disrespectful, please report it.)

 
12/27/2004 1:03:15 PMkorejwa

Mr Noname1 - CaptureActiveWindow() did not return a StdPicture object to MyPic. Try "MyPic = CaptureActiveWindow()" instead of "Set MyPic = CaptureActiveWindow()".
(If this comment was disrespectful, please report it.)

 
12/27/2004 1:03:55 PMkorejwa

Mr Noname2 - Sampling frequencies are almost always done at 2:2,1:1,1:1, which are the default values for the class. There is no reason to use the SetSamplingFrequencies() procedure unless you want to change them. But you are right - Sampling Frequencies and Quality settings should be part of the same procedure that does Sampling.
(If this comment was disrespectful, please report it.)

 
12/29/2004 6:27:17 AM

I just can't download the code though I'd like to see it and try it out. Could you send it to me?
(If this comment was disrespectful, please report it.)

 
1/5/2005 3:24:44 AM

out of the world code ! one thousand globes
(If this comment was disrespectful, please report it.)

 
1/28/2005 5:50:24 AM

crashes when saving very large jpegs >12mb. apart from that, works fine.
(If this comment was disrespectful, please report it.)

 
2/8/2005 6:20:53 AM

Congratulations man! Very useful code. But i have a problem...Works perfect in Win98. But don´t work in Win2000. Do you known any problem with Terminal Server?
(If this comment was disrespectful, please report it.)

 
2/26/2005 8:32:30 AMAlfred

Exceptional! This is the code of the century! I'm using it in several projects - without any problems. Thousand thanks, dear korejwa!
(If this comment was disrespectful, please report it.)

 
2/27/2005 6:58:11 AMAlfred

Hi Korejwa; found a harmless bug. If the input files are of type .ico, there are errors as described above incl. "Bad Huffman Table" in the functions ExpandHuffman, CollectStatisticsAC and/or CollectStatisticsDCInterleaved. It makes no sense to convert icons to JPGs and i'm filtering this now - but Picture-Bulks contains often icons - and a bug is a bug...
(If this comment was disrespectful, please report it.)

 
3/5/2005 11:51:34 PM

Congratulation !
i'm french and i very glad to discover your production.

Bests regards
(If this comment was disrespectful, please report it.)

 
3/27/2005 2:19:23 PMBehrooz Sangani

Good implementation of the famous compression method :)
Thank you very much!
(If this comment was disrespectful, please report it.)

 
4/6/2005 9:06:23 AM

Your program ROCKS!! You are the king! Really good work sir
(If this comment was disrespectful, please report it.)

 
4/26/2005 4:33:09 AMRichard T

THANK YOU!
Superb app you wrote, here. Definately worthy of some awards.
(If this comment was disrespectful, please report it.)

 
5/7/2005 7:03:36 PM

Excellent Code !!!
(If this comment was disrespectful, please report it.)

 
5/17/2005 6:17:52 PM

Great Code really compact, quick and very well documented. Thanks a lo
(If this comment was disrespectful, please report it.)

 
6/28/2005 9:19:11 AMSoorya

Excellent Code...
Thanks a ton for sharing this.
soorya
(If this comment was disrespectful, please report it.)

 
10/3/2005 11:22:42 PMle6end4

excellent work!!
(If this comment was disrespectful, please report it.)

 
3/9/2006 10:14:50 AMPANKUSOFT

very fine code.thanks for sharing it with us.
(If this comment was disrespectful, please report it.)

 
5/6/2006 1:19:59 PMTony

Been searching all over for code like this and finally....
Truly excellent - thanks!
(If this comment was disrespectful, please report it.)

 
6/26/2006 11:39:08 AMSam

Great stuff, but is there a solution to the Subscript out of range in the line n = UBound(data) + 1 of CollectStatisticsDCInterleaved sub ?
(If this comment was disrespectful, please report it.)

 
8/4/2006 6:51:15 AMDenzil

You saved my bacon!
Why on earth VB6 doesn't support JPG save I don't know...
Thanks a bunch!!!
(If this comment was disrespectful, please report it.)

 
8/4/2006 6:55:04 AMDenzil

BTW noticed some bug reports above - but I'm using Windows XP sp2, and it works 100% of the time so far.
(If this comment was disrespectful, please report it.)

 
3/11/2007 10:01:50 PMHerman CK

Author John Korezwa mentioned that
there is also an Integer DCT version available on PSC (the original modified by another coder). Readers please be ware that this Integer DCT version has serious defects (To see error, load a blank 640x480, or just fill it with any color, save as JPG. On XP, LoadPicture of the saved file would fail with assorted error messages depending on how you run your program. If you open the saved file with PhotoShop, you will see "File damaged.." flag.)
(If this comment was disrespectful, please report it.)

 
4/26/2007 11:07:30 AMMeribah

Sir, marvellous, excellent.. what words shld i use? i was really new to vb6, but into the toughest work of images... i searched desparately!!! while saving my images with paintpicture my file size increased heavily... i saw urs... have to study that still but when i tried to save it decreased my file size.... i really owe many thanks!!!
(If this comment was disrespectful, please report it.)

 
7/9/2007 7:22:30 AMFrank Frieswijk

Excellent work, sir! So easy to understand; nor to beginners nor to experts.
(If this comment was disrespectful, please report it.)

 
7/10/2007 12:19:15 AMNius

that is a great program..
but i want to ask you something..
do you have program like that but this time the "open jpeg" don't use loadpicture???
(If this comment was disrespectful, please report it.)

 
8/5/2007 2:20:04 PMFergo

Very nice!! But I'm trying to use the class to get the data from the clipboard, but it isn't working ( it works only with LoadPicture() )
(If this comment was disrespectful, please report it.)

 
9/13/2007 9:26:50 PMyunus emre özkara

Walla Billa Thanks
çok başarılı bi çalışma iyi işime yaradı tebrikler....
(If this comment was disrespectful, please report it.)

 
11/8/2007 8:43:26 PMWillian Rodrigues de Moura

Very good code,
It's exactly I need
(If this comment was disrespectful, please report it.)

 
12/28/2007 9:31:01 PMVery good

Very good,thanks
(If this comment was disrespectful, please report it.)

 
7/11/2008 6:32:46 AMNapuleone

good software but:
I have the problem with 32 bit format if i create MyPic from PictureBox or other image the program crashed in CollectStatisticsDCInterleaved
I use
Set MyPic = Picture1.Image
(If this comment was disrespectful, please report it.)

 
9/1/2008 5:59:00 PMGeorge Garven

this code is a bit too complicated for us ol men, procedures jumping all over the place so its hard to follow, can u just send me the code to convert bmp`s to jpg`s without anything else as I`ve got a prog that lets u select files from a folder and resize and save them as bmp`s, all I need it to be able to save as Jpg`s too
(If this comment was disrespectful, please report it.)

 
10/14/2008 2:11:58 AMFRANK


I would like to write a program in VB6 to save a webpage as a JPG, or BMP image.

Can YOU HELP ME?
(If this comment was disrespectful, please report it.)

 
3/7/2011 12:23:10 PMChaker

All thanks very good
(If this comment was disrespectful, please report it.)

 
11/13/2011 10:09:51 AMUFO

i am using ezvidcap for webcam how to save jpeg out of ezvidcap control on my form?
(If this comment was disrespectful, please report it.)

 
3/1/2012 6:24:57 PMChaker

Very nice Code all Thanks ..
(If this comment was disrespectful, please report it.)

 
5/20/2012 3:55:01 PMmscir

Great job, thanks!
(If this comment was disrespectful, please report it.)

 
4/5/2013 10:15:44 AMRobert

I would like to read jpeg files from an SD card the compress those images before writing them to a server drive. I am unsure on which of your routines i would use for file compression. Help would be appreciated
(If this comment was disrespectful, please report it.)

 
12/18/2013 5:37:03 AMManjeet_Sonu

Excellent!! but cud you please work for detecting image with in the image...?
(If this comment was disrespectful, please report it.)

 
2/13/2014 5:11:56 PMLOfaday

Awesome coding and proves the power of VB6 at the same time. Also, perfect for a simple "no-nonsense" (no dependencies) application that needs to do some simple image resizing.
(If this comment was disrespectful, please report it.)

 
2/24/2014 5:25:42 PMLOfaday

Again, absolutely awesome, but some pointers for those using API & *.image sources in modern 32 bit colour environment -- look in the cImage class for all uses of "24" and modify adjacent code to allow for 32 bits (which can be treated as near identical except in line length scans). You might also want to look for any instances of fault check conditionals and insert "Debug.Assert 0" before any "Exit Function" (otherwise, odd things happen later as a consequence of earlier unknown errors). I hope that helps, and thanks again for the exceptional coding effort. Viva VB6!
(If this comment was disrespectful, please report it.)

 
6/13/2014 5:50:13 PMDavid Stephens

As impress as this project is, it's not very well code. The save mechanism creates a form and saves through that created form. It would be nice to have a simply "SaveJPG" sub which the parameters are assigned in code. I was not able to figure out your code after an hours work to create this in order to use your class in my own project where I don't want a form popping up on every image save.
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.