Results 1 to 4 of 4

Thread: Run Time Image Resizing

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2005
    Posts
    76

    Run Time Image Resizing

    hi,

    I have prepared a vb application in which i have got a scan command button.
    As i press "Scan" button the document in the scanner gets scanned and is visible on the image area or picture area defined by me on the form. This part is working perfectly fine.

    My problem is, the area defined in the form for scanned image is very small, as there are other input fields. For example the image area on the form is 1300 X 1400, and the image which i scanned is around 4000X 3000.

    Can i resize the image in run time, so that the whole image appears in the area defined by me.

    I am using VS TWAIN Code for scanning feature.



    Regards,
    Kaushik

  2. #2
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Run Time Image Resizing

    If you want to add a picture in a Picturebox or in the form using the control's size, you can do it using PaintPicture.

    Using a PictureBox, you can do it like this:
    VB Code:
    1. Private Sub Form_Load()
    2. Dim lPic As Picture
    3.  
    4.     Me.Picture1.AutoRedraw = True
    5.     Set lPic = LoadPicture("C:\YourPicture.jpg") 'Use the correct path and filename here
    6.     ResizePicture Me.Picture1, lPic
    7. End Sub
    8.  
    9. Private Sub ResizePicture(pBox As PictureBox, pPic As Picture)
    10. Dim lWidth      As Single, lHeight    As Single
    11. Dim lnewWidth   As Single, lnewHeight As Single
    12.  
    13.     'Clear the Picture in the PictureBox
    14.     pBox.Picture = Nothing
    15.    
    16.     'Clear the Image  in the Picturebox
    17.     pBox.Cls
    18.    
    19.     'Get the size of the Image, but in the same Scale than the scale used by the PictureBox
    20.     lWidth = pBox.ScaleX(pPic.Width, vbHimetric, pBox.ScaleMode)
    21.     lHeight = pBox.ScaleY(pPic.Height, vbHimetric, pBox.ScaleMode)
    22.    
    23.     'If image Width > pictureBox Width, resize Width
    24.     If lWidth > pBox.ScaleWidth Then
    25.         lnewWidth = pBox.ScaleWidth              'new Width = PB width
    26.         lHeight = lHeight * (lnewWidth / lWidth) 'Risize Height keeping proportions
    27.     Else
    28.         lnewWidth = lWidth                       'If not, keep the original Width value
    29.     End If
    30.    
    31.     'If the image Height > The pictureBox Height, resize Height
    32.     If lHeight > pBox.ScaleHeight Then
    33.         lnewHeight = pBox.ScaleHeight                   'new Height = PB Height
    34.         lnewWidth = lnewWidth * (lnewHeight / lHeight)  'Risize Width keeping proportions
    35.     Else
    36.         lnewHeight = lHeight                            'If not, use the same value
    37.     End If
    38.    
    39.     'add resized and centered to Picturebox
    40.     pBox.PaintPicture pPic, (pBox.ScaleWidth - lnewWidth) / 2, _
    41.                             (pBox.ScaleHeight - lnewHeight) / 2, _
    42.                             lnewWidth, lnewHeight
    43.                            
    44.     'Update the Picture with the new image if you need it
    45.     Set pBox.Picture = pBox.Image
    46. End Sub
    You can add ResizePicture sub in a module (declare it Public) if you want.
    The VB.NET version is here.
    Last edited by jcis; Jan 13th, 2007 at 09:40 PM.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2005
    Posts
    76

    Re: Run Time Image Resizing

    Quote Originally Posted by jcis
    If you want to add that resized picture in a Picturebox or in the form, you can use PaintPicture.

    Using a PictureBox, you can do it like this:
    VB Code:
    1. Private Sub Form_Load()
    2. Dim lPic As Picture
    3.  
    4.     Me.Picture1.AutoRedraw = True
    5.     Set lPic = LoadPicture("C:\YourPicture.jpg") 'Use the correct path and filename here
    6.     ResizePicture Me.Picture1, lPic
    7. End Sub
    8.  
    9. Private Sub ResizePicture(pBox As PictureBox, pPic As Picture)
    10. Dim lWidth      As Single, lHeight    As Single
    11. Dim lnewWidth   As Single, lnewHeight As Single
    12.  
    13.     'Clear the Picture in the PictureBox
    14.     pBox.Picture = Nothing
    15.    
    16.     'Clear the Image  in the Picturebox
    17.     pBox.Cls
    18.    
    19.     'Get the size of the Image, but in the same Scale than the scale used by the PictureBox
    20.     lWidth = pBox.ScaleX(pPic.Width, vbHimetric, pBox.ScaleMode)
    21.     lHeight = pBox.ScaleY(pPic.Height, vbHimetric, pBox.ScaleMode)
    22.    
    23.     'If image Width > pictureBox Width, resize Width
    24.     If lWidth > pBox.ScaleWidth Then
    25.         lnewWidth = pBox.ScaleWidth              'new Width = PB width
    26.         lHeight = lHeight * (lnewWidth / lWidth) 'Risize Height keeping proportions
    27.     Else
    28.         lnewWidth = lWidth                       'If not, keep the original Width value
    29.     End If
    30.    
    31.     'If the image Height > The pictureBox Height, resize Height
    32.     If lHeight > pBox.ScaleHeight Then
    33.         lnewHeight = pBox.ScaleHeight                   'new Height = PB Height
    34.         lnewWidth = lnewWidth * (lnewHeight / lHeight)  'Risize Width keeping proportions
    35.     Else
    36.         lnewHeight = lHeight                            'If not, use the same value
    37.     End If
    38.    
    39.     'add resized and centered to Picturebox
    40.     pBox.PaintPicture pPic, (pBox.ScaleWidth - lnewWidth) / 2, _
    41.                             (pBox.ScaleHeight - lnewHeight) / 2, _
    42.                             lnewWidth, lnewHeight
    43.                            
    44.     'Update the Picture with the new image if you need it
    45.     Set pBox.Picture = pBox.Image
    46. End Sub
    You can add ResizePicture sub in a module (declare it Public) if you want.


    Thanks man i will try it out. Sorry for replying a little late

    Regards,
    Kaushik

  4. #4
    New Member TManto's Avatar
    Join Date
    Sep 2006
    Posts
    6

    Re: Run Time Image Resizing

    VB Code:
    1. Private Sub Form_Load()
    2. Image1.Width = Screen.Width
    3. image1.Stretch = true
    4. End Sub
    Whatever you give, that's you will get


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