Results 1 to 18 of 18

Thread: Reduce dimension of JPEG without losing quality

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2022
    Location
    Italy
    Posts
    56

    Reduce dimension of JPEG without losing quality

    Hello,

    How can I scale down a JPEG without losing quality with vb6?

    Example: an image from 1024x768 to 640x480, but I don't want to lose quality.
    Photoshop already does this well.

    Is there a vb6 algorithm to do this?

    Many Thanks

  2. #2
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,652

    Re: Reduce dimension of JPEG without losing quality

    It's impossible to not lose quality when reducing image size; it's just a question of how much quality you lose.

    GDIPlus has different interpolation modes that will produce better quality than e.g. just letting the VB picturebox do it. There's lots of sample code for gdip in VB6; use GdipSetInterpolationMode HighQualityBicubic.

  3. #3
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Reduce dimension of JPEG without losing quality

    So much missing in the requirements above. Is this meant to create a new file compressed in JPEG format, or are you merely scaling for display?

    Most of the time you can simply use the PaintPicture method and get perfectly good results.

  4. #4

    Thread Starter
    Member
    Join Date
    Feb 2022
    Location
    Italy
    Posts
    56

    Re: Reduce dimension of JPEG without losing quality

    I need to resize photos for my website.
    I've got a list of vehicle
    https://www.labycar.com/veicoli

    Actually, I use this resolution: 640x480 but is too small and there is a low quality

    I need to resize the photo with vb6.

    Which size do you advise?

  5. #5

    Thread Starter
    Member
    Join Date
    Feb 2022
    Location
    Italy
    Posts
    56

    Re: Reduce dimension of JPEG without losing quality

    I need a lossless scaling

  6. #6
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,458

    Re: Reduce dimension of JPEG without losing quality

    By definition scaling down is a lossy operation, if you have fewer pixels then you can't have as much information visible as when you had more pixels.

  7. #7
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,121

    Re: Reduce dimension of JPEG without losing quality

    Quote Originally Posted by Attilio View Post
    I need a lossless scaling
    Bet after this you would ask for saving the result in a lossless JPG, wouldn't you? :-))

    cheers,
    </wqw>

  8. #8
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,652

    Re: Reduce dimension of JPEG without losing quality

    And I need a lossless video compression algorithm to store a blu-ray in a 10kb file. Also some vibranium.

  9. #9

    Thread Starter
    Member
    Join Date
    Feb 2022
    Location
    Italy
    Posts
    56

    Re: Reduce dimension of JPEG without losing quality

    Sorry for the misunderstanding.
    I badly explained myself.

    Actually I use GOGO Picture Viewer Pro for scale down.
    I know there is a loss of quality but is too much.

    I have done the scale down by javascript at the same dimension of 640x480. There is a lose of quality but not so much.
    My question is: there is a way to do a scale down but without lose much quality?

  10. #10
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,458

    Re: Reduce dimension of JPEG without losing quality

    Quote Originally Posted by fafalone View Post
    And I need a lossless video compression algorithm to store a blu-ray in a 10kb file. Also some vibranium.
    10Kb, why do that when you can store an entire mkv file in 10 bytes https://www.vbforums.com/showthread....=1#post5331577

  11. #11
    The Idiot
    Join Date
    Dec 2014
    Posts
    2,721

    Re: Reduce dimension of JPEG without losing quality

    why bother create a tool yourself. u can find tool that can batch-resize for u using good-enough algorithm.
    also, most of the time u can go to settings and decide the compression quality (that is important as well)

    Attachment 186163
    https://i.postimg.cc/3wf14ngB/Image5.jpg

  12. #12
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,121

    Re: Reduce dimension of JPEG without losing quality

    Quote Originally Posted by Attilio View Post
    I have done the scale down by javascript at the same dimension of 640x480. There is a lose of quality but not so much.
    My question is: there is a way to do a scale down but without lose much quality?
    You have to understand that JPG is lossy format. When you are saving a picture in JPG format usually there is a quality setting you can choose from 1 to 99 where 1 means low-quality (but small) and 99 means high-quality (but large file). There is no lossless version of JPG i.e. you have some loss of quality even w/ 99 quality setting.

    Now consider this scenario:
    1. you open your Picture1.JPG and save it to Picture2.JPG with 80 quality setting which is the most common (average) quality used for web images.
    2. then you open Picture2.JPG and save it to Picture3.JPG with 80 quality setting
    3. then you repeat the same until you save Picture10.JPG with 80 quality setting

    Now it is very possible on Picure10 you will hardly recognize the original Picture1 because the 10 fold re-compression would have introduced significant artifacts in the compressed image.

    Coming back to your original question: you have some complaints about the quality of the scaling/resizing with whatever application you are using to do it.

    This is hardly the problem in your case because even mspaint.exe can down-scale very well because down-scaling is easy and so every image processing program does it very well.

    Where do you think then loss of quality must creep in your final image in JPG format? Could it be the JPG re-compression being detrimental? Could it be you are using default (too low) quality setting on JPG save?

    cheers,
    </wqw>

  13. #13

    Thread Starter
    Member
    Join Date
    Feb 2022
    Location
    Italy
    Posts
    56

    Re: Reduce dimension of JPEG without losing quality

    Quote Originally Posted by baka View Post
    why bother create a tool yourself. u can find tool that can batch-resize for u using good-enough algorithm.
    also, most of the time u can go to settings and decide the compression quality (that is important as well)

    Attachment 186163
    https://i.postimg.cc/3wf14ngB/Image5.jpg
    Attach file doesn't work

  14. #14
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,652

    Re: Reduce dimension of JPEG without losing quality

    Here's an example of using the Windows Imaging Component to resize using higher quality algorithms with the ability to specify the quality setting of the JPG it's saved to.

    The Demo uses WICBitmapInterpolationModeFant as it's meant to support Win7, but if you're just supporting Windows 10+, WICBitmapInterpolationModeHighQualityCubic is also an option.

  15. #15

    Thread Starter
    Member
    Join Date
    Feb 2022
    Location
    Italy
    Posts
    56

    Re: Reduce dimension of JPEG without losing quality

    Many Thanks fafalone,
    sorry for previous misandestanding.
    I don't speak english very well and many times I'm not able to explain what I want.

    Thanks to all inside this special community

  16. #16
    Member
    Join Date
    Jul 2020
    Posts
    38

    Re: Reduce dimension of JPEG without losing quality

    Hi, Attilio.
    Here's an excellent class to load and save JPGs files on a perfect quality.
    Korejwa JPEG Encoder

    When you save a JPG using GDI+, the final colors seems to look washed (Poor quality). That's because GDI+ use an standar Sampling frequencies (Chroma Subsampling like 4:2:0). So, you need a none Sampling frequencies when you save your JPG file.

    Put this two classes in to your project:
    cImage.cls & cJpeg.cls

    Then, add this two functions (subs) in to your project:

    Code:
    Public Sub SaveJPG(ByVal vImageFile As String, ByVal vNewImageFile As String, Optional ByVal vNewWidth As Long, Optional ByVal vNewHeight As Long, Optional ByVal vQuality As Long = 100)
        Dim vIMG As cImage
        Dim vNewIMG As cImage
        Dim vJPG As cJpeg
        
        Set vIMG = New cImage
        Set vJPG = New cJpeg
        
        vIMG.CopyStdPicture LoadPicture(vImageFile), 24
        If Not ((vNewWidth > 0) And (vNewHeight > 0)) Then ScaledSize vIMG.Width, vIMG.Height, vNewWidth, vNewHeight
        Set vNewIMG = vIMG.Resample(vNewWidth, vNewHeight)
        vJPG.Quality = vQuality
        vJPG.SetSamplingFrequencies 1, 1, 1, 1, 1, 1
        vJPG.SampleHDC vNewIMG.hDC, vNewWidth, vNewHeight
        vJPG.SaveFile vNewImageFile
        
        Set vNewIMG = Nothing
        Set vIMG = Nothing
        Set vJPG = Nothing
    End Sub
    
    Public Sub ScaledSize(ByVal vOldWidth As Long, ByVal vOldHeight As Long, vNewWidth As Long, vNewHeight As Long)
        Dim vRatio As Double: vRatio = vOldWidth / vOldHeight
        
        If vNewWidth > 0 Then
            vNewHeight = vNewWidth / vRatio
        ElseIf vNewHeight > 0 Then
            vNewWidth = vNewHeight * vRatio
        End If
    End Sub
    You can call it in this way:

    Code:
    SaveJPG "C:\My Folder\My Image.jpg", "C:\My Folder\My NEW Image.jpg", 640, 480
    Thats it!

    The function autodetects if the resized image needs to preserve the aspect ratio. If you need a image with a fixed Width and Height, then use boths parameters (vNewWidth & vNewHeight). If you need a specific Width but you don't know the final Height (and needs to preserve the aspect ratio), put your desired Width and leave empty the vNewHeight parameter. Something like this:

    Code:
    SaveJPG "C:\My Folder\My Image.jpg", "C:\My Folder\My NEW Image.jpg", 640
    Or...

    Code:
    SaveJPG "C:\My Folder\My Image.jpg", "C:\My Folder\My NEW Image.jpg", , 480
    See ya!


    (Tested on Windows XP and Windows 10)


    The class cJpeg.cls accepts the following file types:
    .jpg, .jpeg, .gif, .bmp, .wmp, .rle, .cur, .ico, .emf

  17. #17
    Hyperactive Member
    Join Date
    Dec 2008
    Location
    Argentina
    Posts
    439

    Re: Reduce dimension of JPEG without losing quality

    Excuse me if I say nonsense, I am not very expert in web pages, but this type of thing is not usually done with php?, that is, http://www.yourweb.com/Photos.php?Im...4&Size=640x480

    and then with Photos.php you create your own image on the fly, this will now create a copy of another size.
    leandroascierto.com Visual Basic 6 projects

  18. #18
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,652

    Re: Reduce dimension of JPEG without losing quality

    Doing it in PHP doesn't really make sense for designing the website itself... php is more something you'd use to automatically handle user uploads.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width