Results 1 to 5 of 5

Thread: please help with images

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2000
    Posts
    142
    Two questions regarding resizing images:

    #1
    I need to do some dynamic thumbnailing, basically, just resize a picture and save it on the fly....

    Here is what I have so far:

    Code:
    Private Sub Form_Load()
    
        Image1.Visible = True
        Image1.Stretch = True
        Image1.Picture = LoadPicture("c:\myPic.jpg")     
    
        widt = Image1.Width
        higt = Image1.Height
    
        newWidt = 1500
        scaler = widt / higt
        newHigt = CInt(1500 / scaler)
    
        Image1.Width = newWidt
        Image1.Height = newHigt
            
        SavePicture Image1.Picture, "c:\temp1\test.jpg "
    
    End Sub
    This correctly resizes the picture in the window, but does not save it right. I know there has been some posts on this topic before, except I can't seem to get it right...

    Using Image I can set Stretch = True and the image stretches correctly, but does not save with new dimensions (I cant do the following line, which is what I would need):
    Code:
    SavePicture Image1.Image, "c:\temp1\test.jpg"
    Using PictureBox, I can save the new dimensions, but since there is not Stretch property, I can't get the image to look right. For instance:

    Code:
    Private Sub Form_Load()
    
        Picture1.Visible = True
        Picture1.Picture = LoadPicture("c:\myPic.jpg")
        
        widt = Picture1.Width
        higt = Picture1.Height
    
        newWidt = 1500
        scaler = widt / higt
        newHigt = CInt(1500 / scaler)
    
        Picture1.Width = newWidt
        Picture1.Height = newHigt
            
        SavePicture Picture1.Image, "c:\temp1\test.jpg "
    
    End Sub
    this grabs only the top corner of the image, instead of resizing it correctly....

    What's the correct way to do it??

    #2
    This will eventually have to become a component for ASP. My problem is that it seems that the PictureBox or Image needs the form to exist... simply doing the following:

    Code:
    Dim Pict As PictureBox
    Does not allow me to use any of PictureBox's methods... So my question is, how will have to modify this code in order for it to work as a component (without a form..) ??

    Thanks very much

    dvst8
    Secret to long life:
    Keep breathing as long as possible.

  2. #2
    Addicted Member Mih_Flyer's Avatar
    Join Date
    Mar 2000
    Location
    some place there
    Posts
    241
    for # 1:
    use this
    Code:
    Picture1.PaintPicture MyPicture, Top, Left, Width, Height
    it will help u, first thing add the picture to a image and stretch it as you like, then just copy it to the picturebox using PainPicture, it would be something like this:
    Code:
    Picture1.PaintPicture Image1.Picture, 0, 0, Image1.Width, Image1.Height
    then save the picturebox.image, Hope this helps.

  3. #3
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    Unhappy ...walk around....

    'here is how I would do it...
    'load your picture into a picture box that is set to invisible
    'save your picture and then load the saved picture into the image
    'control.

    'One big drawback...VB controls save the picture as a bmp
    'regardless of the format of the original picutre..
    '
    'there is an article on saving jpgs here
    ' http://vbaccelerator.com/codelib/gfx/vbjpeg.htm

    'good luck...


    Option Explicit

    Const MAX_SIZE = 1500 'Twips

    Private Sub Command1_Click()
    Dim h As Long
    Dim w As Long
    Dim pic As StdPicture
    On Error GoTo THUMBERR
    'Get the picture loaded in picSrc
    'or load a picture using the "Open" dialog box
    Set pic = picDst.Picture
    'Make sure we have a picture
    If pic = 0 Then Exit Sub
    'Convert the height and width to twips
    h = ScaleY(pic.Height, vbHimetric, vbTwips)
    w = ScaleX(pic.Width, vbHimetric, vbTwips)
    'Scale the destination picturebox to maintain
    'the same aspect ratio as original picture
    'but have max dimension of MAX_SIZE
    With picDst
    If w > h Then
    .Width = MAX_SIZE
    .Height = (h / w) * MAX_SIZE
    Else
    .Height = MAX_SIZE
    .Width = (w / h) * MAX_SIZE
    End If
    'Clear previous image and picture
    .Cls
    .Picture = LoadPicture("")
    'Scale the picture into the destination picbox
    .PaintPicture pic, 0, 0, .Width, .Height, 0, 0, w, h, vbSrcCopy
    'Convert the drawn image to the picture
    Set .Picture = .Image
    'Optionally save with "Save As" dialog box:
    SavePicture .Picture, "c:\anewpicture.bmp"
    End With
    'Clear the pic object
    Set pic = Nothing
    Set Image1.Picture = LoadPicture("c:\anewpicture.bmp")
    Exit Sub
    THUMBERR:
    MsgBox Err.Description
    Err.Clear
    End Sub

    Private Sub Form_Load()
    picDst.Visible = False
    picDst.ScaleMode = 1 'Twip
    picDst.AutoRedraw = True
    End Sub

    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    May 2000
    Posts
    142
    nice...

    hey thanks for that code wayne! works flawlessly!
    resizes the image and saves it in bmp format.... (i'm willing to accept bmp as output of thumbnail)

    Question now is, how do I do everything programatically, and not involve the form at all..?
    Now, I only know a little about VB, and all that I've done involves objects on forms. No reference book I have read has indicated to me the following: can the object that I embed in a form exist only in code, independent of the form? It seems to me that the answer is no. What I am getting at, is that I need to instantiate a Picture box and use it in my code, without ever having it on the form. Can it be done????

    The StdPicture Object seemed like a candidate for me, but it doesn't allow you to set width, height properties...

    I was thinking about using StretchBlt... but it needs an input and output device context... I'm not sure what that means.

    Many thanks.
    dvst8
    Secret to long life:
    Keep breathing as long as possible.

  5. #5
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    ...haven't played in that ares

    haven't any experience in that field but I've seen'
    postings on using sub Main and running apps without a
    form...not sure it it was just code or functional objects.

    try looking up sub Main and if not post another question..

    all the best...and you are welcome for what little I can offer....it's how I learn...looking up stuff for others..
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

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