Solved

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

Posted on 2004-09-19
14
1,620 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
ID: 12098351
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
ID: 12098472
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 50

Expert Comment

by:Ryan Chong
ID: 12098640
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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 32

Accepted Solution

by:
Erick37 earned 500 total points
ID: 12098894
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
ID: 12098951
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
ID: 12099200
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
ID: 12099299
-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
 
LVL 32

Expert Comment

by:Erick37
ID: 12102764
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
ID: 12102812
:) ...
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
ID: 12389299
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
ID: 12389552
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
ID: 12389993
Erick37 is right ...
see the Accepted answer... it's an Excellent working method :)
OHDev
0
 
LVL 1

Expert Comment

by:HyMaX_2003
ID: 12390085
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
ID: 12390190
:) ... yeah Thank you Erick :)
Cheers
OHDev
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

776 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