Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1734
  • Last Modified:

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

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
OHDev2004
Asked:
OHDev2004
  • 5
  • 5
  • 2
  • +2
1 Solution
 
Erick37Commented:
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
 
OHDev2004Author Commented:
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
 
Ryan ChongCommented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Erick37Commented:
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
 
Erick37Commented:
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
 
anvCommented:
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
 
OHDev2004Author Commented:
-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
 
Erick37Commented:
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
 
OHDev2004Author Commented:
:) ...
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
 
HyMaX_2003Commented:
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
 
Erick37Commented:
The Resample method is where you set the height and width (I believe it's in pixels)

Set cDibSample = cDib.Resample(Height, Width)
0
 
OHDev2004Author Commented:
Erick37 is right ...
see the Accepted answer... it's an Excellent working method :)
OHDev
0
 
HyMaX_2003Commented:
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
 
OHDev2004Author Commented:
:) ... yeah Thank you Erick :)
Cheers
OHDev
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 5
  • 5
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now