Solved

Smooth picture Resizing ...(using Pixel sampling maybe)

Posted on 2004-09-19
14
1,603 Views
Last Modified: 2008-01-09
Hey there ....
i need a way to resize Pictures .. but to resize them smoothly ... as all the methods i tried do the same .. like .paintpicture and stretchblt

so i heard i can resize a Picture using Pixel sampling or resampling .. donnow really ... this way it's resized with a smooth look...
So any help would be appreciated ...
OHDev
0
Comment
Question by:OHDev2004
  • 5
  • 5
  • 2
  • +2
14 Comments
 
LVL 32

Expert Comment

by:Erick37
Comment Utility
This sample contains a lot of image processing effects and filters, along with smooth image resizing:

"What you can do with this sample
Image Processing
    Blurring and softening
    Sharpening
    Embossing
    Customised filters
    Minimum, Maximum and Average Rank filters for impressionistic effects
Colour Manipulation
    Colourise images
    Darken and Lighten images
    Gray scale images
    Floyd-Stucci Black and White conversion
    Decrease colour depth by dithering and matching to a specified palette.
Image Combination
    Add, subtract with offsets or take the darkest/lightest pixels
Resample Images
    Demonstrating how to use interpolation to create a smooth resized version of an image."


Image Processing Using DIB Sections
http://vbaccelerator.com/home/VB/Code/vbMedia/Image_Processing/Image_Processing_Using_DIB_Sections/article.asp

0
 
LVL 7

Author Comment

by:OHDev2004
Comment Utility
Thank you very much for your help ..
but i already know this one ...
i would like something simpler ...or in other words this example makes you crazy because it contains so much codes and routines ...
so is there any simplier way ... or like
Public Function ResamplePic(Pic as stdpicture,XPixels as integer,YPixels as Integer) as StdPicture
'blabla code
'
'
end Function

Thank you
OHDev
0
 
LVL 49

Expert Comment

by:Ryan Chong
Comment Utility
Also at:

Resampling with Alpha
http://vbaccelerator.com/home/VB/Code/vbMedia/DIB_Sections/Resampling_With_Alpha/article.asp

I used this for resampling for my previous project and the result is good too, nah.. code may be a bit long too..
0
 
LVL 32

Accepted Solution

by:
Erick37 earned 500 total points
Comment Utility
It is a lot of code, but you only need one class file from the sample project to resample images.

Make a copy of the file cDIBSection.cls and copy it to your project folder.  Add the file to your project using Project->Add Class Module.

The Resample() method of the class does the work for you.  Here is a sample using the cDIBSection class from VBAccelerator:

'
'On a form place two pictureboxes and one command button
'

Option Explicit

Private cDib As New cDIBSection
Private cDibSample As New cDIBSection

Private Sub Command1_Click()
   
    'Disable the command button to prevent re-entrance
    Command1.Enabled = False
   
    On Error GoTo errhandler
   
    'Load a picture from file
    Set Picture1.Picture = LoadPicture("c:\bparty0402.jpg")
   
    'Create the DIB from out picture
    cDib.CreateFromPicture Picture1.Picture
    'Resample the image to another DIB
    Set cDibSample = cDib.Resample(cDib.Height / 4, cDib.Width / 4)
   
    '
    'Normally I would use a With Block to reference Picture2 in the code
    'which follows, but I did not here for clarity.
    '
    'Resize the picturebox to be the size of the DIB (scaling to pixels)
    Picture2.Width = Me.ScaleX(cDibSample.Width, vbPixels, Me.ScaleMode)
    Picture2.Height = Me.ScaleY(cDibSample.Height, vbPixels, Me.ScaleMode)
    Picture2.AutoRedraw = True '<< This can be set at design time
   
    'Paint the resampled picture to picture2
    cDibSample.PaintPicture Picture2.hdc
    Picture2.Refresh
   
    'save the new picture
    SavePicture Picture2.Image, "c:\test2.bmp"
   
    Command1.Enabled = True
    Exit Sub
errhandler:
    MsgBox "Error: " & CStr(Err.Number) & " " & Err.Description, vbCritical, App.Title
    Command1.Enabled = True
End Sub


'Hope it helps!  Ask if you need more help on this one.
0
 
LVL 32

Expert Comment

by:Erick37
Comment Utility
Oh, if you need a Picture object of the result add this ...

    'Paint the resampled picture to picture2
    cDibSample.PaintPicture Picture2.hdc
    Picture2.Refresh
    Set Picture2.Picture = Picture2.Image '<<< Now you have a new picture object of the resampled image
0
 
LVL 10

Expert Comment

by:anv
Comment Utility
hi OHDev2004

u can use image object instead of picture object , as images object has a property called stretch,,

when its false the the image will be resize itself to the size of the image the user has displayed..
0
 
LVL 7

Author Comment

by:OHDev2004
Comment Utility
-Thank you Erick37 for your great help ...
-and thank you ryancys for ur help but Erick's answer was more what i wanted .... i think they are kinda the same but the example which erick gave in the end was the right one :)
-thank you "any" but this wasn't what i wanted.

Erick37:
the code works OK ... but i have a small question ..
the resampling code is kinda Blurring the image when it resizes it .. right ?? .. isn't there a Value which i can change for that Blurring rate ??? .. i mean if it's too Blurry i can give it a small value so it's kinda close to the original ..
I don't know if you understood this or not ..
it should be something in the "ResampleDib" function .. i think it's a fixed value now .. so i need to know it to be able to specify the Blurring . Or "Smoothing" value ..

thanx a Lot for all of you
OHDev
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 32

Expert Comment

by:Erick37
Comment Utility
Hi OHDev

I took a look again at the code from VBAccelerator.  What they do to resample the image is this:

1:  Loop through each pixel position in the NEW image and find the corresponding pixel in the original image.

2: Read the original pixel and the pixel to the right, below, and below right.

3: Interpolate the 4 pixels in the x axis resulting in 2 pixels.

4:  Interpolate the 2 newly interpolated pixels in the y axis leaving the final pixel to be written in the new image.

To illustrate:
X <- X
X <- X

Then

X
î
X

So, as you can see, there is not a way to set any parameters to control the sharpness of the resulting image.  I think that the "blurring" effect is just a result of the scaling process.  I notice it too when using IrfanView to rescale pictures.  Sometimes I have to apply a sharpen filter to the picture to make it snap.

0
 
LVL 7

Author Comment

by:OHDev2004
Comment Utility
:) ...
thank you a Lot for making this clear to me ..
LOL ... so the main thing i wanted to resample the resized image ... and that definitly must blur the image to make it smooth .. now i'll need to apply a SHarpen effect to it .. Funny isn't it  ... LOL
naa .. it isn't that important .. it's good as it is now .. just kidding
Thank you a lot
OHDev
0
 
LVL 1

Expert Comment

by:HyMaX_2003
Comment Utility
Hi I liked the code, but I got a question. I want the picture to resize to a specific value, like 640x480 or other. How can I set it? Thanks!
0
 
LVL 32

Expert Comment

by:Erick37
Comment Utility
The Resample method is where you set the height and width (I believe it's in pixels)

Set cDibSample = cDib.Resample(Height, Width)
0
 
LVL 7

Author Comment

by:OHDev2004
Comment Utility
Erick37 is right ...
see the Accepted answer... it's an Excellent working method :)
OHDev
0
 
LVL 1

Expert Comment

by:HyMaX_2003
Comment Utility
Ok perfect! This was an excellent code, and I want to say congratulations for Erick. Now I can do what I want to do! :)
0
 
LVL 7

Author Comment

by:OHDev2004
Comment Utility
:) ... yeah Thank you Erick :)
Cheers
OHDev
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

762 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now