CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Aug 2007
    Posts
    445

    [RESOLVED] Image Resize and save problem

    hi,
    i have a problem with image.
    i have loaded an image in the
    Code:
    Image1.Picture = loadpicture("c:\abc.jpg")
    but when i change its size height and width by
    Code:
    Image1.height=200
    image1.width=2000
    and save it by its new size by this code
    Code:
    savepicture()
    the image size is not changes to its new size.
    how i can do it?
    i want to save the picture with its new size i have set by my code

    please rate the post if this is useful. And also never forget to mark the thread as [Resolved] when your problem is solved

  2. #2
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: Image Resize and save problem

    The picture loaded always stays the same. It is loaded into a bitmap and paintet to the image controls "screen" whenever it is refreshed.
    If the image control is resized, and stretch is on, then the image from the bitmap is painted bigger or smaller, but the bitmap itself remains unchanged.

    Therefore on the first view your task is not too trivial.
    I'd say there could be some API calls which you might use...

    I hope one of the graphic gurus is taking over

  3. #3
    Join Date
    Apr 2003
    Posts
    1,755

    Re: Image Resize and save problem

    You can use the PaintPicture member of PictureBox to resize the image and save it to a file
    Code:
       Dim pic As StdPicture
       Set pic = LoadPicture("<SOURCE FILENAME>")
       With Picture1
          .AutoRedraw = True
          Call .PaintPicture(pic, 0, 0, .ScaleX(200, vbPixels, .ScaleMode), .ScaleY(200, vbPixels, .ScaleMode))
          .AutoRedraw = False
          Call SavePicture(.Image, "<DESTINATION FILENAME>")
       End With
    Hope it will help you

  4. #4
    Join Date
    Apr 2003
    Posts
    1,755

    Re: Image Resize and save problem

    I have to make changes to the code. The .image returns only the part within the rect of the picturebox so we have to resize the control to our desired picture size. Here's my code that works with any scalemode of the picturebox. It also takes into account if the picturebox has a border.
    Code:
       Dim pic As StdPicture
       Const sizeX As Integer = 200, sizeY As Integer = 200
       Set pic = LoadPicture("<SOURCE FILENAME HERE>")
       With Picture1
          .Width = (sizeX * Screen.TwipsPerPixelX) + .Width - .ScaleX(.ScaleWidth, .ScaleMode, vbTwips)
          .Height = (sizeY * Screen.TwipsPerPixelY) + .Height - .ScaleY(.ScaleHeight, .ScaleMode, vbTwips)
          .AutoRedraw = True
          .Cls
          Call .PaintPicture(pic, 0, 0, .ScaleWidth, .ScaleHeight)
          .AutoRedraw = False
          Call SavePicture(.Image, "<DESTINATION FILENAME HERE>")
       End With
    You can make the picturebox invisible so users won't see it

  5. #5
    Join Date
    Mar 2009
    Posts
    15

    Re: Image Resize and save problem

    And even without any extra control because they are made on the go.

    the only thing you need is a form thats called frmMain or if you wish you can change that to another name
    Code:
    Public Function Create_Thumb(sImagePath As String,sDestPath as string, Optional lPixWidth As Long = 95, Optional lPixHeight As Long = 95) As String
    Dim iTwipHeight As Long, iTwipWidth As Long
    iTwipWidth = ConvertPixelsToTwips(lPixWidth)
    iTwipHeight = ConvertPixelsToTwips(lPixHeight, 1)
    
    Dim picTemp As Control, picImage As Control
    Set picTemp = frmMain.Controls.Add("vb.picturebox", "picTemp", frmMain)
    Set picImage = frmMain.Controls.Add("vb.picturebox", "picImage", frmMain)
    picTemp.AutoSize = True
    picImage.AutoSize = True
    picImage.AutoRedraw = True
    picTemp.Picture = LoadPicture(sImagePath)
    picImage.Picture = LoadPicture()
    
    If picTemp.Width > iTwipWidth Or picTemp.Height > iTwipHeight Then
        Dim cRatio As Currency
        picImage.Width = iTwipWidth
        picImage.Height = iTwipHeight
        If picTemp.Width > picTemp.Height Then
            cRatio = picTemp.Width / picImage.Width
        Else
            cRatio = picTemp.Height / picImage.Height
        End If
        picImage.Width = picTemp.Width / cRatio
        picImage.Height = picTemp.Height / cRatio
        picImage.PaintPicture picTemp.Picture, 0, 0, picImage.Width, picImage.Height
        picImage.Picture = picImage.Image
    Else
        picImage.Width = picTemp.Width
        picImage.Height = picTemp.Height
        picImage.Picture = picTemp.Picture
    End If
    picImage.Refresh
    picTemp.Picture = LoadPicture
    frmMain.Controls.Remove "picTemp"
    
    Call SavePicture(picimage.picture,sDestPath)
    
    '-clear data
    ClearData:
    picImage.Picture = LoadPicture
    frmMain.Controls.Remove "picImage"
    End Function

  6. #6
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: Image Resize and save problem

    you could also look at the Crop and Zoom project...

    Theres plenty on code there to help ..

    Gremmy...
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  7. #7
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Image Resize and save problem

    Is there any way short of 3rd party addin to save the image as a Jpeg?

  8. #8
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Image Resize and save problem

    Will FREE do?
    Attached Files Attached Files
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  9. #9
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Image Resize and save problem

    Free will be just fine

    Thanks

  10. #10
    Join Date
    Aug 2007
    Posts
    445

    Re: Image Resize and save problem

    thanks so much

    please rate the post if this is useful. And also never forget to mark the thread as [Resolved] when your problem is solved

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

Featured